View Full Version : Help A Newb With Spring Controllers
netvampire
Jul 17th, 2007, 12:03 PM
Hello,
I am new to working with spring, and have run into a bit of a stag. I tried using google and a few spring books but i have not had any luck. Hopefully this forum will do the trick.
On my main page for my site, the information to be displayed is stored in the database. I have a controller that extends AbstractController, that has a method called handleInternalRequest() which I use to read the information from the database and create a page variable to read from it. This works great.
On another page, I have a simple form. The controller extends SimpleFormController, and the onSubmit(command) method is used to store the information to a database.
I want to be able to merge these two pages. However, it does not seem that I can have 2 controllers for one view. What can i do to have a form, and the ability to read from the database on load time and create page variables using ModelAndView.addObject().
I really appreciate any help. Thanks
pmularien
Jul 17th, 2007, 02:21 PM
It sounds like what you want is that you want a controller that will be used to populate the contents of the view, along with a form that, when submitted, will invoke the onSubmit in your SimpleFormController subclass.
You haven't posted code (if you do, please use [ code ] tags), but you _should_ be able to do something like this.
netvampire
Jul 17th, 2007, 02:56 PM
thanks for the reply.
You said in the last message that "i should be able to do something like this". Can you please explain how.
Essentially how you described it is correct. Think of it like a message forum board. The posts in the forum are retrieved from the database, and at the bottom there is a reply text field to fill in.
Thanks for the information.
pmularien
Jul 17th, 2007, 03:24 PM
Well, not really knowing anything about your application, this is going to be very hypothetical. There are a couple ways you could do it, but I'll focus on the simplest way. You mentioned something about a forum, so I'll use forum-like business objects in my example.
You'd create a subclass of SimpleFormController. You'll use a DAO or something to retrieve the list of forum posts to display. This will be done in the referenceData method of the controller:
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("postList", postDao.getAllPosts());
return model;
}
Note that "postDao" would be a field, populated via regular Spring dependency injection. Hopefully you know how to do this already, and if not, I'd suggest reading the first few sections in the Spring docs.
You'll create a JSP that will look roughly like the following, and map it to your controller's formView property:
<c:forEach items="${postList}" var="post">
... display contents of existing post ...
</c:forEach>
Let's assume you have a com.foo.Post business object which represents a forum post. This will be the command object for your form. I'm assuming you aren't needing to load and/or edit existing posts, so you shouldn't have to override formBackingObject.
You'll add a form to the JSP page, with the form action pointing back to the same URL you mapped to the JSP, with a method type of POST:
<form method="POST" action="<c:url value="/myapp/postlist.jsp"/>">
... insert form fields here ...
</form>
Finally, in your onSubmit method, you'll have a Post object that's been populated from the form fields. You'll use your DAO to save it to the database and redirect to the same view.
That should give you an outline of what you should be doing - if you didn't grok it, I'd strongly suggest reading the MVC portion of the Spring 2.x manual, and working through the Petclinic sample. Post back if you have further questions!
netvampire
Jul 17th, 2007, 03:52 PM
thanks pmularien.
I understood every word you mentioned in your previous post. i don't know why i did not think about using the referenceData() method. brilliantly simple, and very clearly explained.
i appreciate it. take care.
netvampire
Jul 17th, 2007, 04:04 PM
i tried it and it works great. thanks.
pmularien
Jul 17th, 2007, 04:23 PM
Glad to hear it!
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.