SpringMvc環境搭建圖文詳解——入門教程及demo

基於jsp頁面

目錄結構

在這裏插入圖片描述

1.配置座標pom.xml文件

<properties>
  ···
    <!--版本鎖定-->
    <spring.version>5.0.2.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>
  </dependencies>

2.在resource目錄下新建Spring的xml文件

在這裏插入圖片描述

2.1 修改文件頭及配置文件

<?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">
    <!--配置掃描包-->
  <context:component-scan base-package="edu.xiaoying"/>

   <!--視圖解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--文件所在目錄-->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!--文件的後綴名-->
        <property name="suffix" value=".jsp"/>
    </bean>
      <!--開啓SpringMVC註解支持-->
        <mvc:annotation-driven/>
</beans>

3.配置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>
    <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!--初始化spring配置-->
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <!--設置啓動時機,當Tomcat服務器啓動時加載,不寫默認是負數,當服務器使用是才加載-->
      <load-on-startup>1</load-on-startup>

    </servlet>
    <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
  </web-app>


4.配置第一個程序,歡迎頁代碼

跳轉到success.jsp頁面

package edu.xiaoying;

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

/*配置器類*/
@Controller
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("hello,SpringMvc!");
        /*返回字符串的默認是去找這個字符串爲名字的文件*/
        return "success";
    }
}

5.其他頁面

5.1 success頁面

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/4/3
  Time: 14:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>你好,SpringMVC</h3>
</body>
</html>

5.2 index頁面

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/4/3
  Time: 13:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>入門程序</h3>
		<%--注意這裏沒有/--%>
    <a href="hello">入門程序</a>
</body>
</html>

6.效果

6.1 index頁面

在這裏插入圖片描述

6.2 跳轉到success頁面

在這裏插入圖片描述

7.分析結構

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章