IDEA社群版搭建Spring工程(03-Spring MVC搭建)

Andy_lu020發表於2024-07-10
  1. 新建一個基於 Maven 的"webapp"模板的基礎工程

  1. 在 main 資料夾下新建java原始碼資料夾

  1. 將自動生成的index.jsp移入webapp的view資料夾下,在java下新建一個controller資料夾

  1. 新增Spring MVC框架所需的POM配置
  <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>
    <spring.version>4.3.30.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <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>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  1. 新增 tomcat 執行環境支援
<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <port>8080</port>
            <path>/</path>
            <uriEncoding>UTF-8</uriEncoding>
            <server>tomcat7</server>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  1. 配置tomcat7啟動命令

  1. resources下新增 Spring MVC 環境配置檔案 springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 開啟springMVC註解掃描(controller層) -->
    <context:component-scan base-package="controller包的目錄"/>
    
    <!-- 配置檢視解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 掃描的目錄 -->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!-- 以jsp結尾的檔案 -->
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 配置springMVC開啟註解支援 -->
    <mvc:annotation-driven/>
    
    <!-- 放行靜態資源 (web.xml也可配置,配置其一即可)-->
    <!-- static為靜態資源目錄 (自行修改) -->
    <mvc:resources mapping="/static/**" location="static/"/>
</beans>
  1. 配置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">

  <display-name>Archetype Created Web Application</display-name>

  <!-- 配置springMVC前端控制器 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化 springMVC的配置檔案 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 配置servlet啟動時載入物件 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 配置解決中文亂碼問題 -->
  <filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  1. 新建IndexController檔案
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    /**
     * 訪問地址預設為index
     * @return 返回viewName
     */
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    /**
     * 訪問地址/hello
     * @return 攜帶引數的ModelAndView物件
     */
    @RequestMapping("/hello")
    public ModelAndView hello(){
        // 例項ModelAndView物件
        ModelAndView modelAndView = new ModelAndView();
        // 繫結引數
        modelAndView.addObject("message", "Hello SpringMVC!");
        // 設定頁面
        modelAndView.setViewName("index");
        // 返回ModelAndView物件
        return modelAndView;
    }
}
  1. 啟動服務檢視效果

相關文章