Platinum Partner
java,frameworks

Tutorial: Your First Simple Spring MVC Template

In this post, I'm going to show you how to create your first simple Spring MVC template. Before we start, I assume that you already know the basics of Spring MVC. If you don't, I suggest to read first some introductions to Spring. Below are some good resources to start.

Let's start. Setup a new blank project. Set all API dependencies in your classpath then configure your web.xml like this:

<WEB-APP xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" schemalocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance">

<SERVLET>
<SERVLET-NAME>myproject</SERVLET-NAME>
<SERVLET-CLASS>org.springframework.web.servlet.DispatcherServlet</SERVLET-CLASS>
<LOAD-ON-STARTUP>1</LOAD-ON-STARTUP>
</SERVLET>

<SERVLET-MAPPING>
<SERVLET-NAME>myproject</SERVLET-NAME>
<URL-PATTERN>/index.html</URL-PATTERN>
</SERVLET-MAPPING>

<SERVLET-MAPPING>
<SERVLET-NAME>myproject</SERVLET-NAME>
<URL-PATTERN>*.html</URL-PATTERN>
</SERVLET-MAPPING>

<WELCOME-FILE-LIST>
<WELCOME-FILE>index.html</WELCOME-FILE>
</WELCOME-FILE-LIST>

</WEB-APP>

I set two mappings, one for the url pattern and the other one is for the welcome page. Next is setting-up the beans. In your myproject-servlet.xml, put these configurations:

<BEANS xmlns="http://www.springframework.org/schema/beans" schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance">

<BEAN class=com.myproject.controllers.PageController name="myProjectController">
<property name="methodNameResolver" ref="actionMethodNameResolver"> </property>
</BEAN>

<BEAN class=org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver id=actionMethodNameResolver>
<property name="mappings">
<PROPS>
  <PROP key="/index.html">showIndex</PROP> 
</PROPS>
</property>
</BEAN>

<BEAN class=org.springframework.web.servlet.handler.SimpleUrlHandlerMapping id=simpleUrlMapping>
<property name="mappings">
  <PROPS>
     <PROP key="*.html">myProjectController</PROP>
</PROPS>
</property>
</BEAN>

<BEAN class=org.springframework.web.servlet.view.ResourceBundleViewResolver id=viewResolver>
<property name="basename" value="views"></property>
</BEAN>
</BEANS>

 I used the ResourceBundleViewResolver to configure the views using properties file in order for us to still edit the configurations of views without recompiling the project. The bean actionMethodNameResolver is where all your url requests will be dispatched to its proper controller. Since the prop /index.html has a value of showIndex, Your PageController should contain this method:

package com.myproject.controllers;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageController extends MultiActionController {

public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(index-page, "model", "Hello World!");
}
}

Next step is to create the view index-page. Create a properties file named views. It should contain the class and the url of your jsp. Take note that you need the jstl lib in your classpath.

index-page.class=org.springframework.web.servlet.view.JstlView
index-page.url=WEB-INF/index.jsp

Create the index.jsp under WEB-INF. To get the model from our controller, we can use this code.

<H1>${model}</H1>
Set up your Tomcat server. Deploy and run the project. Access your local server (http://localhost:[port]) and your done. You should see the "Hello World!". You can now add other page requests on your actionMethodNameResolver's props and configure the proper method in your PageController. Don't forget to add your views configurations in the views.properties. You can download the source code here. I created the project using Idea IntelliJ and included the required libraries so you don't have to download them. You can also import the project using Apache Ant. If you have questions, post in comments.

From http://benpad.blogspot.com

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}