First, build SpringMVC
1, introduced in the spring-webmvc dependent project maven
<!-- springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.8.RELEASE</version> </dependency>
2, web.xml configuration file, add the following code in web.xml
<servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </ The servlet-class > <-! The springmvc core profile to modify the position of src / main / resources File -> < the init-param > < param-name > the contextConfigLocation </ param-name > < param-value > CLASSPATH: springmvc.xml </ param-value > </ the init-param > < Load-ON-Startup > . 1 </ Load-ON-Startup > </ the servlet > < the servlet-Mapping > < the servlet-name >HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3, configuration springmvc core configuration file, create a file springmvc.xml in src / main / resources file
File names must web.xml file <param-value> classpath: springmvc.xml </ param-value> the same name
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd"> <! - notes under scanning com.blb.controller -> < context: Scan-Component Base-Package = "com.blb.controller" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
4, write test classes
Creating com.blb.controller package in src / main / java file, create a class in the package TestController
package com.blb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { // return to the browser @ResponseBody // intercept address @ RequestMapping ( "the Hello" ) public String the Test () { return "the Hello world" ; } }
5, maven configure tomcat and jdk
<build> <finalName>SSM_T</finalName> <plugins> <!-- jdk配置 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 配置apache提供的tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build>
6, test, launch maven project
Access localhost: 8080 / project name / hello
Page appears hello world build the successful representation springmvc