创建maven普通项目
导入maven
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
创建目录
在目录上按一下f6
ok,然后改变目录为域名那里
配置webapp
在项目上按f4,点击web
点击这里
修改目录,点击确定
配置xml
(1)创建applicationContext.xml配置spring
<context:component-scan base-package="top.chenyp" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
(2)创建springmvc.xml配置springmvc
<context:component-scan base-package="top.chenyp" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
(3)配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
测试配置
编写controller
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/hello")
public @ResponseBody
String testHello(){
return testService.testHello();
}
}
编写service
@Service
public class TestService {
public String testHello(){
return "hello";
}
}
配置tomcat(略)