SpringMVC---IDEA 搭建SpringMVC工程

♂???木發表於2018-12-07

一、新建工程


1、使用idea新建maven專案,步驟如圖:

SpringMVC---IDEA 搭建SpringMVC工程
2、填寫專案座標資訊
SpringMVC---IDEA 搭建SpringMVC工程
3、選擇專案儲存位置,點選完成
SpringMVC---IDEA 搭建SpringMVC工程
4、新建以後專案目錄如下圖
SpringMVC---IDEA 搭建SpringMVC工程
5、完善目錄結構
SpringMVC---IDEA 搭建SpringMVC工程

二、搭建springmvc環境


1、pom.xml匯入依賴包,配置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.nanshen</groupId>
  <artifactId>springmvc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>5.0.8.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--springmvc 核心包依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!--servlet依賴-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <!--mysql資料庫連線依賴-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.46</version>
    </dependency>

    <!--mybatis依賴-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
  </dependencies>
</project>

複製程式碼

2、配置spring applicationContext.xml檔案
  a、建立applicationContext.xml檔案:

SpringMVC---IDEA 搭建SpringMVC工程
  b、配置applicationContext.xml檔案(只配置springmvc的話這個檔案暫時可以不配置,後續ssm框架搭建會配置此檔案)   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">


</beans>
複製程式碼

3、配置springmvc.xml檔案
  a、建立springmvc.xml檔案:跟建立application.xml檔案操作一樣
  b、配置springmvc.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置包掃描器 -->
    <context:component-scan base-package="com.nanshen.demo.controller"/>
    <!-- 配置註解驅動 -->
    <mvc:annotation-driven/>
    <!-- 檢視解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/WEB-INF/views/"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--返回資料編碼設定-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>
複製程式碼

Ps:此處需要配置返回資料編碼格式為UTF-8
4、配置web.xml檔案,用如下配置程式碼替換idea生成的web.xml檔案程式碼

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--Spring MVC 配置 並新增監聽-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/*</param-value>
  </context-param>

  <!-- 字元過濾器 傳值亂碼-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置前端控制器 進行請求分發 DispatcherServlet本質也是一個Servlet -->
  <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/springmvc.xml</param-value>
    </init-param>
    <!--標記容器啟動的時候就啟動這個servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!--攔截所有-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
複製程式碼

至此,springmvc環境已經搭建完成,接下來是測試:

三、測試:


1、建立一個TestController

SpringMVC---IDEA 搭建SpringMVC工程

package com.nanshen.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/create")
    public String test(HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        return "springmvc建立成功";
    }
}

複製程式碼

測試結果圖:

SpringMVC---IDEA 搭建SpringMVC工程
Ps:此處需要在springmvc.xml檔案中配置返回資料編碼格式為UTF-8

相關文章