1. 程式人生 > 實用技巧 >spring快速整合springMvc框架(一)

spring快速整合springMvc框架(一)

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簡易整合完成。