Validation with Spring MVC SimpleFormController (1)

Validation with Spring MVC SimpleFormController

 

Spring MVC provides few options of the controller that you could apply to your application. The choice should be specified by your application requirement and that is the reason I pick up SimpleFormController for form application.

 

Controller Hierarchy

controller.JPG   

Properties Configuration

As shown in the preceding diagram, six properties are introduced for SimpleFormController.

  • sessionForm: Enable or disable the httpSession for the form. You have to set the value to true, if you want to bind the CommandClass to spring tag
  • commandClass: The Form Bean class that used to bind to the form.
  • commandName: The name that will be used in the spring tag to reference the CommandClass. (Default value is command)
  • validator: The class that implement the validation of the form
  • formView: The JSP page that you have your form. (Not the one to trigger the controller, ie send_email.html in my example)
  • successView: The JSP page that need to be displayed after successful submission. (It has to be different from fromView. Otherwise infinite circling exception will be caught)

 

Obviously, you also need to define the validation class in the application context configuration file.

 

In my email application example, we should have the following configuration in the mvc-servlet.xml.

 

    <bean id="emailController" class="hongliang.spring.controller.EmailController">

        <property name="sessionForm" value="true"/>

        <property name="commandName" value="emailCommandName"/>

        <property name="commandClass" value="hongliang.formBean.EmailForm"/>

        <property name="validator" ref="emailValidator"/>

        <property name="formView" value="email_basket.jsp"/>

        <property name="successView" value="email_feedback.jsp"/>

   </bean>

   <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="mappings">

            <props>

                        <prop key="/send_email.html">emailController</prop>

            </props>

        </property>

    </bean>

 

For your convenience, I have also attached the associated configuration in the web.xml. To learn more about basic MVC, you need a proper tutorial, which I will write one later.

 

    <!—locate context configuration file –>

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:ioc/*,WEB-INF/mvc-servlet.xml</param-value>

</context-param>

 

    <servlet>

        <servlet-name>mvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>mvc</servlet-name>

        <url-pattern>*.html</url-pattern>

    </servlet-mapping>

 

Binding Form Bean (CommandClass)

If you are familiar with Struts, you might find a bit easier to understand the MVC form Validation. One of the common parts is that both of them using a java POJO bean class to hold the information of the submitting form, but the difference is MVC binding the bean property to the form in the JSP page.

 

public class EmailForm {

    private String recipientEmail;

    private String senderEmail;

    private String message_optional;

 

    public String getMessage_optional() {

        return message_optional;

    }

 

    public void setMessage_optional(String message_optional) {

        this.message_optional = message_optional;

    }

 

    public String getRecipientEmail() {

        return recipientEmail;

    }

 

    public void setRecipientEmail(String recipientEmail) {

        this.recipientEmail = recipientEmail;

    }

   

    public String getSenderEmail() {

        return senderEmail;

    }

 

    public void setSenderEmail(String senderEmail) {

        this.senderEmail = senderEmail;

    }

}

 

 

Advertisements
This entry was posted in Spring. Bookmark the permalink.

One Response to Validation with Spring MVC SimpleFormController (1)

  1. ali serdar says:

    If we need another reference to a custom class in the commandClass(arac) like ;private Marka marka;How do we make spring bind to marka\’s another string field?I cannot do it like;<spring:bind path="arac.marka.id"> <input type="text" style="width:200;" name="${status.expression}" value="${status.value}" /> </spring:bind>how is the xml configuration, can we define commandClass as a ref bean?Thanx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s