I wanted a throwaway controller that I could use with something that had binding, validation and form and successviews, for purposes of a searching dialog. I didn't want to create a needless command object class for a search form. So here's the product... don't yell at me if it was obvious all along or inherently flawed
Here's an example bean configuration and class that extends the abstract class.Code:package com.xx.xxx.web.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.web.servlet.mvc.SimpleFormController; /** * <p> * Controller that extends <code>SimpleFormController</code> where command * object is instance of itself. Class properties of controller can be binded to * form fields and validated. * </p> * * <p> * Form should be configured as a non-singleton bean. There is no need to set * configuration property commandClass property as command always refers to the * controller's class. In fact, setting it to the wrong class will cause an a * thrown <code>ServletException</code> when * {@link #checkCommand checkCommand()} is called to match the command object's * class and the configured commandClass property. Configuration property * commandName can still be set to refer to command object. * </p> */ public abstract class ThrowawayFormController extends SimpleFormController { /** * Finalized method to only return this instance of controller. */ protected final Object formBackingObject(HttpServletRequest request) throws Exception { return this; } }
Code:package com.xx.xxx.web.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.criterion.Expression; import org.hibernate.criterion.MatchMode; import org.springframework.util.StringUtils; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import com.xx.xxx.persistence.SubjectDao; /** * Controller accepts criteria parameters that are used to search for subjects * and return list in a search results view. */ public class SearchSubjectController extends ThrowawayFormController { private SubjectDao subjectDao; /** * Bind variable for subject's givenName search criteria */ private String givenName; /** * Bind variable for subject's surname search criteria */ private String surname; /** * Takes criteria and creates a collection of criterion to pass to * dao/manager to find subjects. * */ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { ArrayList criteria = new ArrayList(); List subjects = null; if (StringUtils.hasText(this.givenName)) { criteria.add(Expression.like("givenName", this.givenName, MatchMode.ANYWHERE).ignoreCase()); } if (StringUtils.hasText(this.surname)) { criteria.add(Expression.like("surname", this.surname, MatchMode.ANYWHERE).ignoreCase()); } subjects = subjectDao.findSubjectsByCriteria(criteria); return new ModelAndView(getSuccessView(), "subjects", subjects); } public void setSubjectDao(SubjectDao subjectDao) { this.subjectDao = subjectDao; } public String getGivenName() { return givenName; } public void setGivenName(String givenName) { this.givenName = givenName; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } }Code:<bean name="/searchSubjects.htm" class="com.xx.xxx.web.controller.SearchSubjectController" singleton="false"> <property name="commandName"> <value>subjectSearch</value> </property> <property name="subjectDao"> <ref bean="subjectManager" /> </property> <property name="formView"> <value>subjectSearchForm</value> </property> <property name="successView"> <value>subjectSearchResults</value> </property> <property name="validator"> <ref bean="beanValidator" /> </property> </bean>



Reply With Quote
