Spring Framework jar Package
1. Download Spring Source Package
Spring Address: Http://www.springsource.org/download
I'm under the Spring-framework-3.1.0.release-with-docs.zip.
Download Dependency Package: Spring-framework-3.0.5.release-dependencies.zip
Note Official on-line 3.0.3 version after the same version dependent package does not provide download
2. Import the required jar package
Introduction of the Dist directory in addition to the following three all the remaining packages
Org.springframework.web.struts-3.1.0.release.jar
Org.springframework.spring-library-3.1.0.release.libd
Org.springframework.web.portlet-3.1.0.release.jar
Introduction of Com.springsource.org.apache.commons.logging-1.1.1.jar and Com.springsource.org.aopalliance-1.0.0.jar under dependent packages
Spring Framework Configuration
1. Web. XML configuration
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <Web-app
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="Http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
- id="webapp_id"
- version="3.0">
- <context-param>
- <param-name>contextconfiglocation</param-name>
- <!--Application Context profile --
- <param-value>/web-inf/spring-servlet.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
- </Listener>
- <!--Configuring the spring core servlet --
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class >
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!--Url-pattern is configured to/, without a file suffix, which can cause other static files (JS,CSS, etc.) to be inaccessible. With *.do, it does not affect access to static files- -
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </Web-app>
2. Application Context Configuration
Spring-servlet.xml is configured to turn on the annotation-based SPRINGMVC function, as set in Web. XML, the path is Web-inf
[HTML]View PlainCopy
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:context="Http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:mvc="Http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemalocation= "Http://www.springframework.org/schema/beans
- Http://www.springframework.org/schema/beans/spring-beans-3.0.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 ">
- <!--start the annotation-driven spring MVC feature, the registration request URL and the annotation Pojo class method mapping--
- <mvc:annotation-driven />
- <!--start the package scan feature to register classes with annotations such as @controller, @Service, @repository, @Component and so on as spring beans--
- <context:component-scan base-package="com.mvc.rest" />
- <!--resolution of the name of the model view, and the model view name is added before and after the request
- <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix= "/web-inf/view/" p:suffix= ". jsp" />
- </Beans>
Demo Example
1. New Constroller based on Spring-servlet.xml configured package path (com.mvc.rest)
[Java]View PlainCopy
- Package com.mvc.rest;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Import Org.springframework.stereotype.Controller;
- Import Org.springframework.ui.ModelMap;
- Import org.springframework.web.bind.annotation.PathVariable;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.servlet.ModelAndView;
- @Controller
- Public class Restconstroller {
- Public Restconstroller () {}
- @RequestMapping (value = "/login/{user}", method = Requestmethod.get)
- Public Modelandview MyMethod (httpservletrequest request,httpservletresponse response,
- @PathVariable ("user") String user, Modelmap Modelmap) throws Exception {
- Modelmap.put ("Loginuser", user);
- return new Modelandview ("/login/hello", Modelmap);
- }
- @RequestMapping (value = "/welcome", method = Requestmethod.get)
- Public String Registpost () {
- return "/welcome";
- }
- }
2. Build JSP View
View path in Spring-servlet.xml configuration (/web-inf/view/), according to the above Restconstroller class, we set up the view directory under Web-inf, set up welcome.jsp and login/under View hello.jsp
Welcome.jsp random, the hello.jsp code is as follows:
[HTML]View PlainCopy
- <%@ 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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>my JSP ' hello.jsp ' starting page</title>
- <Meta http-equiv= "pragma" content="No-cache">
- <Meta http-equiv= "cache-control" content="No-cache">
- </head>
- <body>
- Hello:<%=request.getattribute ("Loginuser")%;, now time is <%= new date ()%>
- </body>
- </html>
3. Deployment Access
Http://localhost:8080/SpringMvcDemo/welcome
http://blog.csdn.net/linxcool/article/details/7094460
SPRING-MVC environment Construction of web framework (RPM)