建立一個簡單的初級SpringMVC專案(非註解版)

沒物件new一個.發表於2020-10-01

新建一個maven專案(JavaWeb)
匯入依賴

<!--匯入依賴-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

加入防止匯出檔案出錯問題
加在pom.xml中

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

在web.xml(4.0版本以上)中寫入如下內容

<?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">

    <!--配置DispatcherServlet:這個是SpringMVC的核心,請求分發器,前段控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--DispatcherServlet要繫結Spring的配置檔案-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--這裡的springmvc-servlet.xml 會在後面說到不用擔心-->
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--啟動級別:1  伺服器已啟動他就啟動 數字越低驅動順序越靠前-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--
    在SpringMVC中,/    /*
    /:只會匹配所有請求,不會去匹配jsp頁面
    /* :匹配所有請求,包括jsp頁面
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

在你的專案檔案下 這個位置建立 (src\main\resources)springmvc-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--<mvc:annotation-driven/>-->
    <!--處理器對映器 :BeanNameUrlHandlerMapping-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--處理器介面卡 :SimpleControllerHandlerAdapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--檢視解析器  :模板引擎  :InternalResourceViewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--BeanNameUrlHandlerMapping: bean-->
    <bean id="/hello" class="com.dong.controller.HelloController"/>

</beans>

最後一步建立一個類繼承Controller

package com.dong.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        ModelAndView mv = new ModelAndView();

        //業務程式碼
        String result="HelloSpringMVC";

        mv.addObject("msg",result);

        //檢視跳轉
        mv.setViewName("test");


        return mv;
    }
}

建立測試的jsp頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

之後將專案新增到Tomcat中進行測試即可

相關文章