1. 程式人生 > >從無到有整合SpringMVC-MyBatis專案(2):搭建SpringMVC專案

從無到有整合SpringMVC-MyBatis專案(2):搭建SpringMVC專案

       前言:本次搭建SpringMVC專案,建立在已完成從無到有整合SpringMVC-MyBatis專案(1):搭建JavaWeb專案 的基礎上,本篇的重點在於如何將SpringMVC框架引入到普通的JavaWeb專案中去,專案基於SpringMVC4.3.18版本進行搭建,所有配置親測可用;

        疑難點分析:搭建SpringMVC專案的難點在於如何建立起各種XXX.xml配置檔案的關係,以及每個配置檔案中的每行配置是什麼作用,搞清楚了這些,針對自己不懂得點有針對性的學習,會事半功倍。先交代下本次搭建用到的工具和框架:

  1. 開發工具:Idea 2018.2.2
  2. spring框架版本:4.3.18.release
  3. maven版本:3.5.2

首先看看搭建完成之後的專案結構:

新增了HelloController和HelloService類,在WEN-INF下新增SpringMVC的配置檔案,applicationContext.xml和SpringMVC-servlet.xml,在本次搭建中,採用父子容器的概念,即SpringMVC-servlet.xml只配置controller相關的,業務交給applicationContext.xml;

好了,開始搭建:

1、引入SpringMVC和切面依賴,最終pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dongnao</groupId>
    <artifactId>SpringMVC-Sample</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <name>SpringMVC-Sample</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- 引入SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <!-- 引入切面依賴 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <!-- 引入Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- 引入JSP -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!-- 引入JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2、找到web.xml,進行如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3、SpringMVC-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 配置自動掃描controller包下面帶有@Controller註解的類 -->
    <context:component-scan base-package="com.dongnao.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!-- 開啟MVC註解驅動,請求分發必須 -->
    <mvc:annotation-driven />
    <!-- 配置靜態資源處理 -->
    <mvc:resources mapping="/static/**" location="/resources/" />
    <!-- 配置檢視解析器 將邏輯檢視名對映為物理檢視名 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value="/WEB-INF/views/"></property>
        <property name = "suffix" value = ".jsp"></property>
    </bean>
</beans>

4、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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 掃描業務程式碼 -->
    <context:component-scan base-package="com.dongnao">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Service"></context:include-filter>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>
    <!-- 開啟aop支援 -->
    <aop:aspectj-autoproxy />
</beans>

5、HelloController類如下,就是一個普通的Controller,程式碼如下:

import com.dongnao.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/helloMvc")
    public ModelAndView helloMvc(String msg) {
        helloService.sayHello(msg);
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "搭建SpringMVC專案成功");
        mv.setViewName("helloMvc");
        return mv;
    }
}

6、HelloService類如下:

import org.springframework.stereotype.Service;

@Service
public class HelloService {
    public void sayHello(String msg){
        System.out.println(msg);
    }
}

7、helloMvc.jsp程式碼如下: