我正在为表单使用Spring SimpleFormController,由于某种原因,它不会转到onSubmit方法

这是我的代码:

public class CreateProjectController extends SimpleFormController {

ProjectDao projectDao;

public CreateProjectController() {
    setCommandClass(Project.class);
    setCommandName("Project");
    setSessionForm(true);
}
@Override
protected Object formBackingObject(HttpServletRequest request)
        throws Exception {
    String id = request.getParameter("id");
    Project project = projectDao.getProjectByOutsideId(id);
    System.out.println("@formbacking object method");
    System.out.println("the success view is "+getSuccessView());
    return project;
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
    Project project = (Project) command;
    System.out.println("this is the project title: "+project.getTitle());
    System.out.println("the success view is "+getSuccessView());
    projectDao.insert(project);

    return new ModelAndView(getSuccessView());
}


我知道,因为它打印的是“ @formbacking对象方法”字符串,而不是“成功视图是...”字符串和:“这是pr ...”字符串。每当我点击提交时,我在控制台中看到“ @formback ..”字符串,但没有看到最后两个。我不知道问题出在哪里。

这是我的jsp

<form:form method="POST" commandName="Project">
Name: <form:input path="title"/><br/>
Description: <form:input path="description"/><br/>
Link: <form:input path="url" disabled="true"/><br/>
Tags: <form:input path="tags"/><br/>
Assessors <form:input path="assessors"/><br/><br/>
<input type="submit" value="submit"/>
</form:form>


我正在Google App Engine上运行。也许问题出在哪里?

更新:问题似乎出在formBackingObject方法上。当我删除它时,单击提交,该表单现在转到onSubmit。

但是我想从表单中的数据库获取命令类的值。

另一段无效的代码:

    @Override
protected Object formBackingObject(HttpServletRequest request)
        throws Exception {
    String id = request.getParameter("id");
    Project projectFromConsumer = projectDao.getProjectByOutsideId(id);
    Project project = new Project();
    String title = projectFromConsumer.getTitle();
    project.setTitle(title);
    project.setUrl("projectUrl");
    return project;
}


但这确实有效:

    @Override
protected Object formBackingObject(HttpServletRequest request)
        throws Exception {
    String id = request.getParameter("id");
    Project projectFromConsumer = projectDao.getProjectByOutsideId(id);
    Project project = new Project();
    String title = projectFromConsumer.getTitle();
    project.setTitle("projectTitle");
    project.setUrl("projectUrl");
    return project;
}


现在我真的很困惑。哈哈。

最佳答案

我在思考与axtavt相同的思路。您将仅在更新上具有id请求参数,因此应为创建表单添加一些代码:

仅供参考,formBackingObject要求返回非空对象。为了节省一些内存,可以有一个最终的常量成员变量,它是默认的返回值。尽管您正在传输对象,但是您的代码满足了这一点,但是当您不使用DTO时,您不知道为什么要传输数据(创建额外的对象)。您可以简单地做到这一点:

private final static Project PROJECT_INSTANCE = new Project();
static {
    PROJECT_INSTANCE.setTitle("defaultProjectTitle");
}

@Override
protected Project formBackingObject(HttpServletRequest request) throws Exception {
    String id = request.getParameter("id");
    if(id == null || id.trim().length() == 0 || !id.matches("\\d+")) {
       return PROJECT_INSTANCE;
    }
    return projectDao.getProjectByOutsideId(id);
}


您不需要隐藏的ID输入字段。您将使用formBackingObject()初始化表单输入字段以进行更新(通过导航至page.jsp?id = 111)。

07-25 21:52