View Full Version : Which controller when I don't need submit or a success view?
dgrossen
Aug 12th, 2009, 06:29 PM
What controller is the best fit for an application that doesn't need to process a submission and render a success view?
What I have in mind is displaying a table of data from database tables based on a query string paramater.
Thanks in advance!
CaptainCrunch
Aug 12th, 2009, 06:47 PM
If you're handling a request parameter you're most likely processing a "submission" of some sort. It sounds like a request with a method of GET rather than a form POST is what you're referring to. Either way, if you want to display the information you'll probably end up forwarding the request to some sort of view to display the information that you've populated the model with. If you're returning XML or JSON you can always write the output directly to the output stream in HttpServletResponse...
As to which controller to use, you could extend AbstractController or use Spring MVC annotations on a POJO (Plain Old Java Object) to markup the implementing class as a controller.
debarshim
Aug 12th, 2009, 07:28 PM
Actually I had a similar question. I guess what the OP requires is that the table will be displayed in the same page as the form (probably below the search criteria). This will enable the user to quickly change the existing search criteria and view the results in the same page in the table below.
For e.g. for booking a hotel you got to enter several search criteria like date, star rating, location within city, etc. Once the user selects these , the data should be displayed at the bottom of the page and IMPORTANT: the existing search criteria should remain intact (bound). This way the user can immediately tweak one odd search parameter to get a new result (instead of having to enter all the search parameters all over again).
I could always use an AbstractController and code everything manually, but is there a better way?
CaptainCrunch
Aug 12th, 2009, 07:47 PM
By the way, if you're planning on returning JSON or some other content type than html, you might want to consider implementing a View to convert the populated ModelAndView to your desired content type.
Assuming you have a BeanNameViewResolver configured in your context
<bean id="beanNameViewResolver"
class="org.springframework.web.servlet.view.BeanNameViewR esolver">
<property name="order" value="1" />
</bean>
you could implement a view as follows:
@Component("jsonView")
public class JSONView extends AbstractView {
public static final String NAME = "jsonView";
@Override
@SuppressWarnings("unchecked")
protected void renderMergedOutputModel(Map model, HttpServletRequest req, HttpServletResponse resp) throws Exception {
JsonConfig config = new JsonConfig();
resp.getOutputStream().print(JSONObject.fromObject (model, config).toString());
resp.getOutputStream().flush();
}
}
Returning "jsonView" from your controller would forward the request to the view class defined above for conversion of the model into JSON and then flushing the content to the output stream. Note that the view is annotated with @Component("jsonView"), this allows the framework to resolve the "jsonView" path returned from your controller (again, this is assuming that you have properly configured a BeanNameViewResolver in your context). You could of course avoid annotations altogether, and register the bean in your context with an id of "jsonView".
On a side note, the view above uses json-lib to generate the JSON:
http://json-lib.sourceforge.net
CaptainCrunch
Aug 12th, 2009, 07:55 PM
debarshim:
You probably want to check out the SimpleFormController in the reference documentation. You basically define what is referred to as a "Command" object (why it's called this I have no idea), that will be populated with the bound input form fields in your search form. After retrieving the search parameters from the populated command object you can perform your search, populate the model and forward the user back to the input page (search form in this case). The input fields should retain the original values if you use the spring form: taglib to bind them to the backing command object.
It's definitely more complicated than simply extending the AbstractController, but the ref docs should guide you through it.
Search for SimpleFormController
http://static.springsource.org/spring/docs/2.5.x/reference/
dgrossen
Aug 13th, 2009, 12:11 AM
Great insight. I would definitely prefer to work with a command object. Maybe I approached this all bassackwards...
Above I mentioned I was thinking about using a query string parameter. The reason being I can integrate an existing search app by inserting a link with each result.
So maybe what I want is a controller that does handle a submission and a success view. And for now leave out the form view (or at least build a simplified search form until the second phase of the project)
Thoughts?
dgrossen
Aug 13th, 2009, 01:52 PM
I've decided to use an AbstractCommandController. Thanks for the discussion
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.