1. Create a servlet project reference http://www.cnblogs.com/zno2/p/5908589.html
2. Introducing Spring MVC Dependency
<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-webmvc</Artifactid> <version>4.3.3.RELEASE</version> </Dependency>
Note: When you create a spring bean configuration file in an STS, you can load the namespaces (update project as appropriate)
Such as
3. Modify Web. xml
<Web-appversion= "3.0"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_3_0.xsd"> <Display-name>Archetype Created Web Application</Display-name> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/root-context.xml</Param-value> </Context-param> <servlet> <Servlet-name>Dispatcher</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value></Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Dispatcher</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener></Web-app>
Ps:url-pattern must be '/' cannot be '/* '
4. Create Web-inf/root-context.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd " > <Mvc:annotation-driven/> <Context:component-scanBase-package= "Zmvc" /> <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.UrlBasedViewResolver"> < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView" /> < Propertyname= "prefix"value= "/web-inf/jsp/" /> < Propertyname= "suffix"value= ". jsp" /> </Bean></Beans>
Ps:base-package is used to scan the controller under the specified package
5. Writing a Controller
Package Zmvc; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; @Controller Public class Test { = "/test") public String Test () { return " TestResult "; }}
6. Add View
Create web-inf/jsp/testresult.jsp
< HTML > < Body > < H2 > This is a test result! </ H2 > </ Body > </ HTML >
7. Turn on log
① adding dependencies
<Dependency> <groupId>Log4j</groupId> <Artifactid>Log4j</Artifactid> <version>1.2.14</version> </Dependency>
② adding log4j.properties
# Root Logger optionlog4j.rootlogger=trace, file, stdout# Direct log messages to a log filelog4j.appender.file=org.apache. Log4j. Rollingfileappenderlog4j.appender.file.file=e:\\logging.loglog4j.appender.file.maxfilesize= 10mblog4j.appender.file.maxbackupindex=10log4j.appender.file.layout= Org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%-5p%c{1}:%L- %m%n# Direct log messages to stdoutlog4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.target=system.outlog4j.appender.stdout.layout= Org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%-5p%c{1}:% L-%m%n
8. Complementary dependencies
Test needs to use JSTL to add the following dependencies
<Dependency> <groupId>Javax.servlet</groupId> <Artifactid>Jstl</Artifactid> <version>1.2</version> </Dependency>
9. Final directory structure
Quickly create a spring MVC sample