May 21, 2009

Spring MVC - Example

« Create a simple RESTful Webservice with Jersey and Spring | Main | Spring Localization Example »

What is Spring MVC?
Spring MVC is a clearcut solution of the model view controller pattern. Apart from them the Spring MVC framework has a lot of additional useful functions. For example the validation of formulars.

In this article the basic Spring MVC principles will be described on the basis of the following example.
Steps are a login formular, validation of the username/password entries and a result page.

1. web.xml configuration:

First you have to add the ContextLoaderListener at web.xml file:

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

After that additional application contexts should be added:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:web-ctx.xml</param-value>
</context-param>

Then you add the DispatcherServlet and the servlet mapping. That's the base Spring MVC servlet:

<servlet>
  <servlet-name>web</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>web</servlet-name>
  <url-pattern>/*.html</url-pattern>
</servlet-mapping>

2. Web Application Context:

The prefix of the application context is the servlet name of the dispatcher servlet definition at the web.xml file. That's why the file name is web-servlet.xml.

<!-- Data Validation -->
<bean id="loginFormValidator" class="org.developers.blog.examples.spring.mvc.LoginFormValidator"/>

<!-- Page Controller -->
<bean id="loginFormController" class="org.developers.blog.examples.spring.mvc.LoginFormController">
  <property name="validator" ref="loginFormValidator"/>
  <property name="formView" value="LoginForm.jsp"/>
  <!-- jump page, defined at urlMapping bean -->
  <property name="successView" value="ResultPage.html"/>
  <!-- alias name for command data object for accessing at the jsp page -->
  <property name="commandName" value="loginData"/>
  <!-- accessing data object -->
  <property name="commandClass" value="org.developers.blog.examples.spring.mvc.LoginFormData"/>
</bean>

<!-- controller of the result page -->
<bean id="resultPageController" class="org.developers.blog.examples.spring.mvc.ResultPageController">
</bean>

<!-- Mapping request URL to controller bean -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="urlMap">
    <map>
      <entry key="Login.html"><ref bean="loginFormController"/></entry>
      <entry key="ResultPage.html"><ref bean="resultPageController"/></entry>
    </map>
   </property>
</bean>

<!-- view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
  <property name="prefix" value="/jsp/"/>
  <property name="suffix"><value></value></property>
</bean>

3. Beans, Controller und Validator

3.1 LoginFormData

public class LoginFormData {
    private String username;
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUsername() {
        return this.username;
    }

    private String password;
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPassword() {
        return this.password;
    }
}

3.2 FormController

public class LoginFormController extends SimpleFormController {

    @Override
    public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        LoginFormData formData = (LoginFormData)command;
        request.getSession().setAttribute("FORM_DATA", formData);
        ModelAndView modelAndView = new ModelAndView(new RedirectView(getSuccessView()));
        return modelAndView;
    }
    
}

3.3 ResultPageController

public class ResultPageController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("ResultPage.jsp");
    }

}

3.4 LoginFormValidator

public class LoginFormValidator implements Validator {

   public void validate(Object obj, Errors errors) {
      LoginFormData loginData = (LoginFormData)obj;

      if (loginData.getUsername() == null || loginData.getUsername().length() == 0) {
        errors.rejectValue("username", "error.empty.field", "empty username");
      } else if (!loginData.getUsername().equals("admin")) {
        errors.rejectValue("username", "unknown.user", "unknown user");
      }

      if (loginData.getPassword() == null || loginData.getPassword().length() == 0) {
        errors.rejectValue("password", "error.empty.field", "empty password");
      } else if (!loginData.getPassword().equals("password")) {
        errors.rejectValue("password", "wrong.password", "wrong password");
      }

   }

    public boolean supports(Class clazz) {
        return clazz.equals(LoginFormData.class);
    }
}

4. Definition of JSP pages - The View Components

4.1 LoginForm.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<html>
    <body>
        <form:form commandName="loginData" method="POST">
            Username:<form:input path="username"/><form:errors path="username"/><br/>
            Password:<form:input path="password"/><form:errors path="password"/><br/>
            <input type="submit" value="login"/>
        </form:form>
    </body>
</html>

4.2 ResultPage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <body>
        <h1>Successful Login!</h1>
    </body>
</html>

The full Spring MVC example as maven project can be downloaded here. You can start the project with the maven jetty plugin. The command is "mvn clean install jetty:run".

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 7:24 PM in Spring

 

[Trackback URL for this entry]

Comment: Mediocre-Ninja.blogSpot.com at Do, 18 Jun 2:57 AM

Thank you for sharing, but I didn't manage to run your provided project. The steps seem OK, but Jetty response was:

HTTP ERROR 404

Problem accessing /index.html. Reason:

NOT_FOUND


Am I missing some thing?

Comment: Rafael at Do, 18 Jun 7:42 AM

You have to run the following url: http://localhost:8080/Login.html.
Have a look at the "urlMapping" bean.

Comment: Mediocre-Ninja.blogSpot.com at Fr, 19 Jun 4:37 AM

After downloading your project and running maven command like above, the Jetty response was:

HTTP ERROR 404
Problem accessing /index.html. Reason:
NOT_FOUND


Am I missing something ?

Comment: Mediocre-Ninja.blogSpot.com at Fr, 19 Jun 4:40 AM

Thank you!
It works like a charm now !

Comment: hemant chouhan at Sa, 13 Feb 9:57 AM

when i run this above given app on netbeans5.0 the it show the error C:\Documents and Settings\hemant\Spring-Mvc1\nbproject\build-impl.xml:479: Deployment error:
and one more in the LoginForm.jsp like absolute uri uri="http://java.sun.com/jsp/jstl/core" cannt resolve in web.xml or jar files

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« May »
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
24252627282930
31