View Full Version : How to put two forms on one page in spring?
yanv
May 1st, 2006, 03:20 PM
Hi folks,
i'm new in spring and have a problems putting two forms on one page, i want each form will be bind to some SimpleFormController
any help will be appreciate
thanks in advance
socotech
May 1st, 2006, 04:10 PM
i'm new in spring and have a problems putting two forms on one page
Can you tell us more about the problem?
Colin Yates
May 2nd, 2006, 04:03 AM
There are a number of posts about this, try searching.
For my two cents, I would probably approach this by having each form post to a seperate controller. Have both controllers have the same success page.
yanv
May 2nd, 2006, 01:36 PM
Can you tell us more about the problem?
hey guys, first of all thanks for reply.
i'm trying to have two separate forms with two distinct controllers on one JSP page but already on GET i receive following error:
javax.servlet.ServletException: Neither Errors instance nor plain target
object for bean name 'userInfo' available as request attribute
I found that i can solve it by having two pages for each controller and when using include tag in my main page, i didn't try it yet and still optimistic to find more elegant way.
tobysaville
May 2nd, 2006, 02:54 PM
I dont think this issue is related to having two forms in one JSP. This just means you're calling
<spring:bind path="userInfo">...</spring:bind>
with no object named userInfo in your page context. Implement the method formBackingObject in your controller to return the object that you want to acess via the spring:bind tag, and call setCommandName("userInfo") in your controller's contstructor.
alternatively, if you are building the page using a model, ensure the userInfo object is in your model.
cheers, toby
yanv
May 2nd, 2006, 07:42 PM
But my page has two forms and hence formBackingObject has to return two objects, i guess that when i redirect to my aforementioned page using SimpleUrlHandlerMapping i actually activate only one controller which is return only one object to access.
is this the cause of the problem?
Thank you.
tobysaville
May 3rd, 2006, 04:28 AM
You have two options:
I would use an AbstractCommandController implementation to build the formView page model, placing the two objects you need as command objects into the model.
public ModelAndView handle(...) {
return new ModelAndView("formViewPage.jsp")
.addObject("cmdObjectOne" userInfo)
.addObject("cmdObjectTwo" companyInfo);
}
The alternative is to use the referenceData method of one of your form controllers, but this would make one controller depend on the other being present in the same page.
cheers, toby
yanv
May 3rd, 2006, 12:43 PM
thanks,
it may be a stupid question but which object should be the CommandClass?
tobysaville
May 3rd, 2006, 01:10 PM
whatever the class of your userInfo object is. if you do this:
MyUserInfo userInfo = new MyUserInfo();
Then do this in you constructor:
setCommandClass(MyUserInfo.class);
The command class is just whatever you want spring to bind the form fields to automatically. It can be any java bean you like.
yanv
May 3rd, 2006, 06:06 PM
Thank you.
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.