Spring MVC3 is a framework for building web applications:
In this post I will detail in the most simple way how to start with Spring MVC3. I will use Eclipse and JBoss AS 7.
Note that there are Maven archtypes which can be used for creatin Spring MVC projects. I will not use it in this post.
A spring MVC3 project is not different than any other web project. What makes is a Spring MVC3 project is the fact that requests are dispatched to a Spring DispatcherServlet instance which handles the request in terrms of:
1. Controllers - User Friendly URLs and Request Params, Session Handling
2. Handling Forms - Form Input to Objects Binding
3. Internationalization
4. Exception Handling
5. Ajax – Ajax Handling Via Controllers
6. Injecting Resources - for Example Injecting EJB3
7. Form Validation - Server Side Forms Input Validation
8. REST Services – Via Controllers
It works Great! Let’s start!
1. Create a web application in eclipse (Indigo):
Seletc file–>New–>Dynamic Web Project
2. Adding Dependencies
2.1 Download Dependencies by downloading the Spring Framework jars: Spring Framework Download
2.2 Extract the downloaded zip file and place it under: MyProject/WebContent/WEB-INF/lib
2.3 Download 3 more dependencies:
Your project after downloading all dependencies should look like this:
Now just set the build path to include the above dependencies jars you just placed under the WEB-INF/lib
3. Creating a Controller
The hurt of a Spring MVC 3 project are the controllers which are simple Java classes annotated with @Controller just above the class declaeration.
Your web project has a Java Resources directory under this directory there’s the src directory.
- Right click on src–>Create new Class–>Under package set my.com.controllers and for class name set: MyController
MyController.java
- package my.comp.controllers;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- @Controller
- public class MyController {
- @RequestMapping(value = "/hello")
- public ModelAndView pulse(HttpServletRequest request,
- HttpServletResponse response) {
- ModelAndView modelAndView = new ModelAndView("hello");
- return modelAndView;
- }
- }
3.2 Create a jsp File
Under MyProject/WebContent/jsp create a jsp file and name it: hello.jsp
hello.jsp
hello.jsp
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- Hello Spring MVC3!
4. Configuring web.xml and spring-servlet.xml to make our project a Spring MVC3 project
In the step above we created a controller and a jsp file. Now we want to make a user request of the following form:
http://localhost:8080/myapp/hello to be mapped on our controller, MyController, will map between hello.jsp file and a /hello request. This will be achieved by @RequestMapping(value = “/hello”) annotation at the controller above a certain method. The method return a ModelAndView instance which set hello as itsd view. Behibe the scenes content of hello.jsp will be returned as a response.
Now let’s see wow does this happen? It’s easy there is just two more parts:
- web.xml – declaring a listener for requests and dispatch them to a spring DispatcherServlet from here everything is behind the scenes
- spring-servlet.xml this is where we configure our spring mvc3 application.
Add the following to your web.xml file:
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/app/*</url-pattern>
- </servlet-mapping>
spring-servlet.xml file:
- <!--?xml version="1.0" encoding="UTF-8"?-->
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- JSR-303 support will be detected on classpath and enabled automatically
- Add validastion.api before hibernate validation jar in the classpath add
- slfj and slflog4j to the classpath. add jaxb-api.jar -->
- <mvc:annotation-driven>
- <mvc:interceptors>
- <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
- <property name="paramName" value="lang">
- </property></bean>
- </mvc:interceptors>
- <context:component-scan base-package="my.comp.controllers">
- <mvc:resources mapping="/resources/**" location="/resources/">
- <!--This is where we tell the dispatcher whern to look for the view files (/jsp/hello) and which prefix it should add to the view name (/jsp/hello.jsp)-->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
- <property name="prefix" value="/jsp/">
- <property name="suffix" value=".jsp">
- </property></property></property></bean>
- <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
- <property name="basename" value="classpath:messages">
- <property name="defaultEncoding" value="UTF-8">
- </property></property></bean>
- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
- </bean>
- </mvc:resources></context:component-scan></mvc:annotation-driven></beans>
Now let’s test!
In order to run the project on the server the easiest way is to right click on the project and select run on server.
OR you can export the project as a war file and deploy (Exporting as war is just a matter of right click on the project and export as war)
In order to run the project on the server the easiest way is to right click on the project and select run on server.
OR you can export the project as a war file and deploy (Exporting as war is just a matter of right click on the project and export as war)
After deploying the application your app should be available at:
http://localhost:8080/spring_mvc3_app/app/hello
http://localhost:8080/spring_mvc3_app/app/hello
Note that spring servlet dispatcher, dispatches every request from the URL template myapp/app/* as configured in the web.xml.
Src ---> commonj.com



0 commentaires:
Enregistrer un commentaire