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.
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.
ReplyDeleteRegards
James
Could you provide us the source code of this tutorial?
ReplyDeleteHello,
ReplyDeletePlease 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
good work friend.
ReplyDeleteif you can join with my web www.opentec.ning.com
we can shere your ideas well.
Thank you for posting this very usable tutorial.
ReplyDeleteawesome! 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.
ReplyDeletehttp://www.adobocode.com/spring/a-spring-web-mvc-tutorial
hope you find it okay!
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.
ReplyDeletespanking 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.