In QQ group there are many children's shoes often ask some questions, I built the Spring framework example how to start not up? Spring Getting Started sample who has? And so on. So I wrote this article, absolutely hands-on explanation ah!
First, prepare the article
Spring Version: Spring-framework-3.1.1.release
Jar Package: All jar packages under the Dist folder under the spring package
Third-party jar packages:
JSP Tag pack: Jstl-api-1.2.jar, Standard.jar
Log pack: Commons-logging-1.0.4.jar
Second, the environment construction
1. New Dynamic WEB Project projects: Spring
2. Add the following configuration to the Web. xml file
<servlet> <servlet-name>spring</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <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> < !--Load-on-startup: Indicates that the servlet is initialized when the container is started-- <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <!-- Url-pattern: Indicates which requests are given to spring Web MVC processing, and "/" is used to define the default servlet mappings. You can also block all requests that have an action extension, such as "*.action". - <url-pattern>*.action</url-pattern> </servlet-mapping>
3. Create the Spring-mvc.xml file under the Web-inf directory: (Here the file needs to be configured in the Web. xml file, see step 2nd)
<?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" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <bean class=" Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/> <!-- Indicates that all beans that implement the Org.springframework.web.servlet.mvc.Controller interface can act as a processor in spring Web MVC. If other types of processors are required, they can be resolved by implementing hadleradapter. --<bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--Intern Alresourceviewresolver: Used to support SERVLET, JSP view resolution, viewclass:jstlview means the JSP template page needs to use the JSTL tag library, classpath must contain the relevant jar package jstl; prefix and suffix: find the prefix and suffix of the view page (previous Prefix (logical view name suffix), such as a logical view called Hello, the JSP view page should be stored in "view/hello.jsp";
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= " Viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value="/view /"/> <property name=" suffix "value=". jsp "/> </bean> <bean name="/hello "class=" Com.spring.mvc.HelloWorldController "/> <span style=" font-family:arial, Helvetica, Sans-serif; " ><!--Note that the classpath here is not wrong, and if you don't use annotations, you'll need to configure--></span></beans> here whenever you create a new controller class.
4. Add controller class under SRC: Helloworldcontroller.java
package Com.spring.mvc;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.context.request.requestcontextholder;import Org.springframework.web.context.request.servletrequestattributes;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.controller;public Class Helloworldcontroller implements Controller {public Modelandview handlerequest (HttpServletRequest arg0, Httpservle Tresponse arg1) throws Exception {HttpServletRequest request = ((servletrequestattributes) requestcontextholder.get Requestattributes ()). Getrequest (); Modelandview mv = new Modelandview (); Add model data, which can be any Pojo object Mv.addobject ("message", "Hello world!"); Sets the logical view name, and the view resolver resolves to the specific view page mv.setviewname ("Hello") based on that name; return MV; } }
5. Start the Tomcat server and visit
/hello.action is included in our access URL, and when the access path with the. Action suffix is filtered by the Dispatcherservlet intercept (already configured in the Web. xml file), then/ The hello prefix will be mapped to Helloworldcontroller controller processing (already configured in the Spring-mvc.xml file)
The next article explains how to create and access a controller using annotations
spring3.1.1 Introductory Lecture one (non-annotated article)