搭建一個初級SpringMVC環境

創建一個maven並添加web框架

在這裏插入圖片描述

在pom文件中添加依賴
<!--  依賴 -->
   <dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>5.2.2.RELEASE</version>
       </dependency>

       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
           <version>2.5</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
       <dependency>
           <groupId>jstl</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
   </dependencies>
在web.xml中配置攔截器
<servlet>
       <servlet-name>DispatcherServlet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>
   
   <servlet-mapping>
       <servlet-name>DispatcherServlet</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>
配置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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

   <context:component-scan base-package="cn.edu.zut.controller"/>

   <mvc:annotation-driven></mvc:annotation-driven>

   <!-- 靜態資源過濾 -->
   <mvc:default-servlet-handler/>


   <!--  視圖解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         id="internalResourceViewResolver">
       <!--  前綴  -->
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <!--  後綴  -->
       <property name="suffix" value=".jsp"/>
   </bean>

</beans>
通過註解編寫第一個Controller
package cn.edu.zut.controller;

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

@SuppressWarnings("all")
@Controller
public class HelloController {
   @RequestMapping("/test")
   public String test(Model model){
       model.addAttribute("msg","Hello SpringMVC");
       return "test";
   }
}
編寫index.jsp和test.jsp(注意根據視圖解析器創建這個test.jsp)
<%--
 Created by IntelliJ IDEA.
 User: jdq8576
 Date: 2020/1/17
 Time: 15:51
 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>
 <a href="${pageContext.request.contextPath}/test"點我試試></a>
 </body>
</html>
<%--
 Created by IntelliJ IDEA.
 User: jdq8576
 Date: 2020/1/17
 Time: 16: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>
${msg}
</body>
</html>
Tomcat發佈測試

在這裏插入圖片描述

在這裏插入圖片描述

可能存在的問題

在發佈的時候可能會存在url請求找不到的時候,這是因爲我們在發佈的時候沒有把jar引進去。解決方法很簡單,在WEB-INF下創建lib目錄,在lib下引入所有jar即可。如圖所示。

在這裏插入圖片描述

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