Java – Spring form not submitting

google-app-enginejavaspringspring-mvc

I am using Spring SimpleFormController for my forms and for some reason it won't go to the onSubmit method

Here's my code:

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());
}

I know because it prints "@formbacking object method" string but not the "the success view is…" string and the :"this is the pr…" string. I see "@formback.." string in the console but not the last two whenever I hit submit. I don't know where the problem is.

This is my 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>

I am running on Google App Engine btw. Maybe the problem is there?

UPDATE: The problem seems to be with the formBackingObject method. When I removed it, the form now goes to the onSubmit when I click submit.

But I'd like to have values from of the command class from the database in my forms.

Another piece of code that doesn't work:

    @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;
}

but this does work:

    @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;
}

Now I am really confused. haha.

Best Answer

I was thinking along the same lines as axtavt. You are only going to have an id request parameter on updates, so you should add some code for creation forms:

FYI, formBackingObject requires a non-null object to be returned. To save some memory, you can have a final constant member variable that is the default return value. Your code satisfies this though since you're transferring objects, but I don't get why you're transferring data (creating an extra object) when you're not using a DTO. You could simply do this:

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);
}

You don't need a hidden id input field. You would use formBackingObject() for initializing the form input fields for updating (by navigating to page.jsp?id=111).

Related Topic