views:

504

answers:

3

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.

A: 

Look at the String id = request.getParameter("id");. There is no such field in your form, so probably you get an error there during submit process, maybe, getProjectByOutsideId returns null.

P.S. It's strange that your formBackingObject is executing when you press submit, it shouldn't if you really set setSessionForm(true).

axtavt
getProjectByOutsideId is working, the jsp shows the values of the command class returned by getProjectByOutsideId.
Jeune
A: 

Try turning the spring debugging up. It provides a lot of information, which can be helpful. Do this by editing the log4j.properties file.

log4j.logger.org.springframework=DEBUG

Have you added logging to make sure the formBackingObject is returning something?

System.out.println("@formbacking object method is returning: " + project);

It will make sure something is being returned. In general the formBackingObject should always return something.

EDIT:

Id is not being passed during submission in the snippet. Maybe it is during the load, e.g. /page.do?id=4, but it doesn't appear in the form.

Add <form:hidden path="id"/> to your form during on submit. Otherwise the id will not be a parameter and the getProjectByOutsideId will fail.

jon077
I know the formBackingObject is returning something because its populating the form in the jsp with values. The problem is that when I hit the submit button, it doesn't submit. And this should work whether you use the populated values or the ones you input.
Jeune
Ooops.. looks like the tag was not showing.
jon077
Why do I need to put a hidden field with the form? If I am not mistaken the formBackingObject method is called when the client requests for the form view and not after the client hits submit. When I call this form I of course include the id with it. For example http://localhost:8888/createproject?id=[some id here]. After that the form shows. The problem is it won't submit/go to the success view when I click submit.
Jeune
I think the id field is not being passed onsubmit. Without the hidden field, you are calling localhost:8888/createproject, not localhost:8888/createproject... maybe add System.out.println("id: " + id) below String id= request.getParameter("id");to be sure.
jon077
A: 

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).

Droo
Hahaha. since all of you here are trying to tell me same thing, I think I'll give your suggestion a try. Cheers! :D
Jeune