javablogspot

Just another WordPress.com weblog

Tutorial: Your First Simple Spring MVC Template

Posted by damuchinni on March 10, 2009

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.

Introduction to Spring Framework 2.5
What’s new in Spring 2.5
Spring 2.0: What’s new and why it matters
Introducing the Spring Framework
Spring Framework reference manual (PDF)
Let’s start. Setup a new blank project. Set all API dependencies in your classpath then configure your web.xml like this:

view sourceprint?01.
02.

03.
04.myproject
05.org.springframework.web.servlet.DispatcherServlet
06.1
07.
08.

09.
10.myproject
11./index.html
12.
13.

14.
15.myproject
16.*.html
17.
18.

19.
20.index.html
21.
22.

23.
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:

view sourceprint?01.
02.

03.
04.
05.
06.

07.
08.
09.
10.
showIndex
11.
12.
13.
14.

15.
16.
17.

18.
myProjectController
19.
20.
21.
22.

23.
24.
25.
26.
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:

view sourceprint?01.package com.myproject.controllers;
02.

03.import org.springframework.web.servlet.ModelAndView;
04.import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
05.import javax.servlet.http.HttpServletRequest;
06.import javax.servlet.http.HttpServletResponse;
07.

08.public class PageController extends MultiActionController {
09.

10.public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
11.return new ModelAndView(index-page, “model”, “Hello World!”);
12.}
13.}
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.

view sourceprint?1.index-page.class=org.springframework.web.servlet.view.JstlView
2.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.

view sourceprint?1.

${model}


Set up your Tomcat server. Deploy and run the project. Access your local server (http://localhost:%5Bport%5D) 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

Advertisements

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

 
%d bloggers like this: