View Poll Results: Is the throwaway form controller useful?

Voters
4. You may not vote on this poll
  • yes

    2 50.00%
  • no

    1 25.00%
  • why'd you even bother

    1 25.00%
Results 1 to 3 of 3

Thread: ThrowawayFormController: the throwaway with a form

  1. #1
    Join Date
    Jul 2005
    Posts
    28

    Default ThrowawayFormController: the throwaway with a form

    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

    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
     * &#123;@link #checkCommand checkCommand&#40;&#41;&#125; 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 &#123;
    
        /**
         * Finalized method to only return this instance of controller.
         */
        protected final Object formBackingObject&#40;HttpServletRequest request&#41;
                throws Exception &#123;
            return this;
        &#125;
    &#125;
    Here's an example bean configuration and class that extends the abstract class.

    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 &#123;
    
        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&#40;HttpServletRequest request,
                HttpServletResponse response, Object command, BindException errors&#41;
                throws Exception &#123;
            ArrayList criteria = new ArrayList&#40;&#41;;
            List subjects = null;
            if &#40;StringUtils.hasText&#40;this.givenName&#41;&#41; &#123;
                criteria.add&#40;Expression.like&#40;"givenName", this.givenName,
                        MatchMode.ANYWHERE&#41;.ignoreCase&#40;&#41;&#41;;
            &#125;
            if &#40;StringUtils.hasText&#40;this.surname&#41;&#41; &#123;
                criteria.add&#40;Expression.like&#40;"surname", this.surname,
                        MatchMode.ANYWHERE&#41;.ignoreCase&#40;&#41;&#41;;
            &#125;
    
            subjects = subjectDao.findSubjectsByCriteria&#40;criteria&#41;;
            return new ModelAndView&#40;getSuccessView&#40;&#41;, "subjects", subjects&#41;;
        &#125;
    
        public void setSubjectDao&#40;SubjectDao subjectDao&#41; &#123;
            this.subjectDao = subjectDao;
        &#125;
    
        public String getGivenName&#40;&#41; &#123;
            return givenName;
        &#125;
    
        public void setGivenName&#40;String givenName&#41; &#123;
            this.givenName = givenName;
        &#125;
    
        public String getSurname&#40;&#41; &#123;
            return surname;
        &#125;
    
        public void setSurname&#40;String surname&#41; &#123;
            this.surname = surname;
        &#125;
    
    &#125;
    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>

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    interesting

    I'm not sure I get the benefit of what you have done though If you don't want the overhead of creating/using a strongly typed form, then why not simply use a Map as your command object?
    AFAIK spring will happily bind to [code]<spring:bind name="command[key]"/>[code]

    Also, I don't think you have *changed* what is happening, rather you have moved where it is happening. You still have a strongly typed form, but it is held on the controller, not in a seperate class

    I haven't had my coffee this morning so I might be being mean

  3. #3
    Join Date
    Jul 2005
    Posts
    28

    Default

    Yatesco,

    Yer right about moving things around. My intention was to avoid creation of extra classes, and avoid clutter. The idea of a map would definitely meet the same need. One of the other reasons for using the controller as the command object was to validate with commons validator. I haven't looked into validating map objects in commons validator, but if it was possible the concept of using a map would meet all my requirements for writing the ThrowawayFormController.

    Adam

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 6
    Last Post: Sep 22nd, 2006, 09:08 AM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Replies: 1
    Last Post: Aug 30th, 2005, 10:51 PM
  5. Replies: 0
    Last Post: Jun 10th, 2005, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •