spring快速集成springMvc框架(一)
本文章將介紹兩種快速簡易搭建ssm框架的方法,分別是:
- xml方式
- java配置方式
一、xml方式
1.使用idea創建maven project

點擊next

點擊next -->finish。最終生成的工程目錄結構如下:

2.修改pom.xml文件
添加
<packaging>war</packaging>

3.添加webapp目錄和web.xml
光標放在項目名上,按F4打開project Structure modules ,選中web。

然后雙擊 Web Resource Dicretory,打開如下圖

點擊ok-->yes,創建webapp目錄。然后點擊加號,點擊web.xml,如下圖:


剪切\WEB-INF\web.xml放入webapp目錄下

點擊ok。此時web結構建好了。如下圖:

4 在pom里添加依賴
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
5 添加xml配置文件
選中resource 右鍵 new-->XML Configuration File -->Sprin Config 創建 applicationContext.xml和spring-servlet.xml

6.applicationContext.xml配置內容
先在src.main.java下創建包 com.feiyuxuy.controller、 com.feiyuxuy.service
在applicationContext.xml里配置如下:
<?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"
xsi:schemaLocation="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.xsd">
<context:component-scan base-package="com.feiyuxuy" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
掃描spring管理包 不包含controller
7.spring-servlet.xml 配置
<context:component-scan base-package="com.feiyuxuy" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven/>
springMVC掃描Controller包,不掃描其他的包。
8.在web.xml加載配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
9.編寫controller、service代碼
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
public String hello(){
return helloService.sayHello();
}
}
@Service
public class HelloService {
public String sayHello() {
return "hello world ! 哈哈";
}
}
10.部署到tomcat


運行tomcat,瀏覽器輸入地址 http://localhost:8080/hello 效果如下,則集成成功。

源碼下載地址:https://pan.baidu.com/s/1k5PbvF_pbz_wWSR4WgsYRQ 提取碼:xsyu
至此 spring和springMvc簡易集成完成。
