Tuesday, March 3, 2009

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:




myproject
org.springframework.web.servlet.DispatcherServlet
1



myproject
/index.html



myproject
*.html



index.html



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:



 





 showIndex






 
    myProjectController







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.

${model}

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.

7 comments:

  1. Great post Benedict. This would be great to get onto JavaLobby. If you'd be interested in reposting this on JavaLobby you can contact me on james at dzone.com and we can organise it.
    Regards
    James

    ReplyDelete
  2. Could you provide us the source code of this tutorial?

    ReplyDelete
  3. Hello,

    Please add your site at http://www.sweebs.com. Sweebs.com is a place where other people can find you among the best sites on the internet!
    Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.

    Regards
    Kris

    ReplyDelete
  4. good work friend.
    if you can join with my web www.opentec.ning.com
    we can shere your ideas well.

    ReplyDelete
  5. Thank you for posting this very usable tutorial.

    ReplyDelete
  6. awesome! i wanna share too a link i found in the web. this uses the latest release from spring-framework-3.0.5.. it is a tutorial on creating web apps using spring mvc.

    http://www.adobocode.com/spring/a-spring-web-mvc-tutorial

    hope you find it okay!

    ReplyDelete
  7. I looked forward to a bright morning, energy once again flowingthrough my body, ready to guard America and the rest of the world fromthe evil of my son. I know you geeksve been sharing this apartment for weeks I knowall three of you know where Gingers got Gladys Barrett.
    spanking machine stories
    first time handjob stories
    bdsm gay rapes stories
    bare ass spanking stories
    nifty erotic gay stories archive
    I looked forward to a bright morning, energy once again flowingthrough my body, ready to guard America and the rest of the world fromthe evil of my son. I know you geeksve been sharing this apartment for weeks I knowall three of you know where Gingers got Gladys Barrett.

    ReplyDelete