Spring MVC is a follow-on product of springframework and has been integrated into spring Web flow. The Spring framework provides a full-featured MVC module for building WEB applications. Using the spring pluggable MVC architecture, you can choose to use a built-in spring web framework or a WEB framework such as Struts.
"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "
The spring version used in this example is SPRING-3.2.0.M1. Click to download!
Operation Steps:
1. Create a Web Project,springmvc. Will spring-3.2.0.m1 the jar file under the Lib folder. In addition to this position need to introduce Commons-logging-1.1.3.jar.
2. Create the package path COM.MAHAOCHEN.SPRINGMVC and create the Hellocontroller.java.
Here you need to introduce the annotation classes required by SPRINGMVC.
@Controller annotation identifies a controller,
@RequestMapping annotations to mark an Access path (/index.jsp)
@Controller in SPRINGMVC, controller controllers are responsible for processing requests distributed by Dispatcherservlet, which encapsulates the data requested by the user into a model after processing it through the business processing layer, and then the model Return to the corresponding view for display. A very simple way to define a controller is provided in SPRINGMVC, where you do not need to inherit a particular class or implement a specific interface, just use @controller to mark a class as a controller and then use the @requestmapping And @requestparam some annotations are used to define the mapping between URL requests and Controller methods, so that the controller can be accessed by outsiders. In addition, the controller does not directly depend on HttpServlet objects such as HttpServletRequest and HttpServletResponse, which can be obtained flexibly through the controller's method parameters.
@RequestMapping to map the URL to the controller class, or to the handler controller's processing method. When @requestmapping is marked on the controller class, the request address of the method that uses the @requestmapping tag is relative to the @requestmapping on the class; when the controller @requestmapping annotations are not marked on a class, the @requestmapping on the method are absolute paths. The final path of this combination of absolute and relative paths is relative to the root path "/".
Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.PathVariable; Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.servlet.modelandview;@ Controllerpublic class Hellocontroller {/** * Spring MVC URL Jump * @return */@RequestMapping ("/test") public Modelandview tes T () {String str = "Spring MVC example"; return new Modelandview ("message", "str", str);}}
3. Configure the Web. xml file.
When Dispatcherservlet is initialized, the framework looks for a name in the Web application Web-inf directory named [servlet-]- The Servlet.xml file, and where the relevant beans is defined, overrides any beans defined in the global, like the code in the following Web. XML, corresponding to the dispatcher-servlet.xml;
Of course, you can also use the <init-param> element, manually specify the path of the configuration file;
"Custom profile Location"
<init-param>
<description> load all the XML in the/web-inf/spring-mvc/directory as a spring MVC configuration file </description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
Web. xml file
<?xml Version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name></display-name> <servlet> < Servlet-name>springmvc</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> & lt;url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> Index.jsp</welcome-file> </welcome-file-list></web-app>
4, from the previous operation, You need to create a springmvc-servlet.xml file under Web-inf, and of course you can create a springmvc-servlet.xml file under SRC.
<?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:context= "Http://www.springframework.org/schema/context" 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/cont Ext/spring-context.xsd "> <!--enables spring to auto-detect components such as annotated controllers and so-<context:component-scan Base-pac Kage= "Com.mahaochen.springmvc"/> <bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.UrlBasedViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value="/web-inf/jsp/"/> <prop Erty name= "suffix" value= ". jsp" ></property> </bean></beans>
5, modify the index.jsp, create message.jsp under/web-inf/jsp/.
index.jsp add
<body> <div style= "Margin:auto; width:300px; text-align:left;" ><a href= "test.do" >spring MVC example </a><br/><a href= "test" >spring MVC example </A><BR/ > </div> </body>
message.jsp
<%@ page Language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
6. Publish the application to the Tomcat server and test.
"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "
Spring MVC Environment Construction and basic operation