SpringMVC學(xué)習(xí)

前言

很久之前學(xué)習(xí)過一點php里的MVC(模型-視圖-控制器)架構(gòu)。在學(xué)習(xí)java時遇到了SpringMVC框架,這里做一個記錄。

Servlet

首先idea創(chuàng)建一個Servlet項目。創(chuàng)建一個空的maven工程,在pom.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>org.example</groupId>
    <artifactId>springmvc-01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</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>

</project>

添加成功后,在項目添加web Framework support。使其成為一個web項目。

image.png

編寫一個Servlet類,用來處理用戶的請求。

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲得參數(shù)
        String method = req.getParameter("method");
        if (method.equals("add")&&method!=null){
            req.getSession().setAttribute("msg","執(zhí)行了add方法");
        }
        //視圖跳轉(zhuǎn)部分
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

代碼會在當前會話中設(shè)置了一個名為 "msg" 的屬性,值為 "執(zhí)行了add方法"。然后跳轉(zhuǎn)到/WEB-INF/jsp/test.jsp文件。
在web.xml里面配置servlet如下

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

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <session-config>
        <!--配置session30分鐘超市-->
        <session-timeout>30</session-timeout>
    </session-config>
    <!--配置歡迎頁-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

當訪問hello這個servlet時,代碼就會走到HelloServlet,觸發(fā)doGet方法。通過傳入method方法,跳轉(zhuǎn)到/WEB-INF/jsp/test.jsp頁面。

image.png

我們會發(fā)現(xiàn)用 Servlet 處理用戶的請求會非常麻煩。每個 Servlet 都要繼承 HttpServlet,會產(chǎn)生非常多的重復(fù)性代碼。所以SpringMVC就產(chǎn)生了。

初始SpringMVC

首先還是創(chuàng)建一個maven項目,導(dǎo)入依賴

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.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>

在web.xml 中創(chuàng)建前置控制器 DispatcherServlet。

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

    <!--1.注冊DispathcerServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--2. 綁定spring配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--3. 啟動級別-->
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

創(chuàng)建一個controller請求

@Controller
public class HelloController{
    @RequestMapping("/hello")
    public ModelAndView hello(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // ModelAndView 模型和視圖
        ModelAndView mv = new ModelAndView();
        //封裝對象,放在ModelAndView中,Model
        mv.addObject("msg","HelloSpringMVC!");
        //封裝要跳轉(zhuǎn)的視圖,放在ModelAndView中
        mv.setViewName("test"); //WEB-INF/jsp/test.jsp
        return mv;
    }
}

然后在src/resources 資源目錄下創(chuàng)建 SpringMVC的配置文件springmvc-servlet.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--注冊組件掃描器-->
    <context:component-scan base-package="com.kuang.controller"/>
    <!-- 這個標簽啟用了基于注解的 Spring MVC 支持-->
    <mvc:annotation-driven/>


    <!-- 
        配置視圖解析器
        作用:將prefix + 視圖名稱 + suffix 確定最終要跳轉(zhuǎn)的頁面
    -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>

在/WEB-INF/jsp/下創(chuàng)建test.jsp

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

配置tomcat啟動即可。
需要注意的是在發(fā)布的時候?qū)aven依賴也要導(dǎo)入進去。

image.png
image.png

當訪問hello這個Servlet時,就會執(zhí)行HelloController里的hello方法,將數(shù)據(jù)HelloSpringMVC返回到視圖test.jsp。

SpringMVC流程分析

image.png

根據(jù)SpringMV執(zhí)行的流程圖,我們簡要梳理一下。

  • 1、用戶在瀏覽器提交請求到前置的中央處理器控制器DispatcherServlet進行處理。
  • 2、前置控制器DispatcherServlet收到請求后,將請求轉(zhuǎn)給處理器映射器HandlerMapping。
  • 3、處理器映射器HandlerMapping根據(jù)request請求的URL等信息查找能夠進行處理的Handler,并構(gòu)造HandlerExecutionChain執(zhí)行鏈,然后返回給前置控制器DispatcherServlet,執(zhí)行鏈包含一個處理器對象和一或多個攔截器。
  • 4、前置控制器DispatcherServlet根據(jù)處理器執(zhí)行鏈的處理器,能夠找到其對應(yīng)的處理器適配器HandlerAdapter。
  • 5、處理器適配器HandlerAdapter調(diào)用相應(yīng)的處理器Handler,即具體的Controller執(zhí)行方法。
  • 6、Controller處理完后返回ModelAndView給HandlerAdapter
  • 7、處理器適配器HandlerAdapter將Controller執(zhí)行結(jié)果ModelAndView返回給前置控制器DispatcherServlet。
  • 8、前置控制器DispatcherServlet調(diào)用視圖解析器ViewReslover處理ModelAndView。
  • 9、視圖解析器ViewReslover解析后根據(jù)邏輯視圖名解析成具體的頁面地址,生成并返回具體對象View。
  • 10、前置控制器DispatcherServlet根據(jù)對象View進行視圖渲染。
  • 11、返回渲染的視圖結(jié)果到前置控制器DispatcherServlet。
  • 12、最后前置控制器DispatcherServlet向用戶返回響應(yīng),至此就全部完成了。

注解開發(fā)SpringMVC

在剛才的HelloController存在以下注解

  • @Controller 注解用來標識一個控制器類
  • @RequestMapping("/hello") 注解用來指定處理哪些 URL 請求
    @RequestMapping 還存在以下屬性
@RequestMapping(value = "/hello",method = RequestMethod.GET)

value 表示請求的url,method表示請求方法。
那如何接受參數(shù)呢?

@RequestParam 注解可以把請求參數(shù)傳遞給請求方法。

修改一下HelloController,測試看看

@Controller
public class HelloController{
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String test3(@RequestParam("username") int a, int b, Model model){
        int sum = a+b;
        model.addAttribute("msg","結(jié)果為:"+sum);
        return "test";
    }
}
image.png

當默認訪問hello這個Servlet時,沒有傳遞username參數(shù)值。

image.png

當傳遞username,而缺少b參數(shù)時,依然會告訴你b值可選擇。

image.png

Spring MVC 會按請求參數(shù)名和屬性值自動匹配。

@PathVariable 注解,可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅?

修改一下HelloController,測試看看

@Controller
public class HelloController{
    @RequestMapping("/add/{a}/")
    public String test4(@PathVariable int a, @PathVariable int b, Model model){
        int sum = a+b;
        model.addAttribute("msg","結(jié)果為:"+sum);
        return "test";
    }
}

此時訪問add這個serlvet,顯示404。

image.png

當輸入完整路徑,可回顯結(jié)果。

image.png

RestFul風(fēng)格

RestFul是一種風(fēng)格,可以更加簡潔、更有層次、更易于實現(xiàn)緩存的機制。該風(fēng)格被廣泛應(yīng)用,可以幫助我們理解平時遇到的地址和傳參
在傳統(tǒng)方式里,用的最多的是GET和POST。而使用RESTFul風(fēng)格,可以通過不同的請求方式來實現(xiàn)不同的效果。
如剛才我們實驗的
http://localhost:8080/springmvc_02_war_exploded/add/145/2
再次修改一下HelloController,測試看看

@Controller
public class HelloController{
    @RequestMapping(path = "/add/{a}/",method= RequestMethod.HEAD)
    public String test4(@PathVariable int a, @PathVariable int b, Model model){
        int sum = a+b;
        model.addAttribute("msg","結(jié)果為:"+sum);
        return "test";
    }
}

method必須使用HEAD方法來請求,而在傳統(tǒng)請求方式里基本用不到。
此時瀏覽器默認訪問即405,方法不允許。

image.png

改用burpsuite可正常執(zhí)行。

image.png

因為 Spring MVC 默認不會執(zhí)行HEAD方法來計算結(jié)果,所以這里返回空值。
小結(jié):在RestFul風(fēng)格下,訪問某個地址404,添加參數(shù)后200,是合理的;訪問某個地址405,更改方法以后200,是合理的;使用PUT方法來更新數(shù)據(jù)是合理的;使用OPTIONS方法來獲取數(shù)據(jù)也是合理的。

重定向和轉(zhuǎn)發(fā)

修改一下HelloController,測試看看

@Controller
public class HelloController{
    @RequestMapping("/m1/t1")
    public String test1(Model model){
        //轉(zhuǎn)發(fā)
        model.addAttribute("msg","m1_t1_springmvc");
        return "/WEB-INF/jsp/test.jsp";
    }

    @RequestMapping("/m2/t2")
    public String test2(Model model){
        //重定向
        model.addAttribute("msg","m2_t2_springmvc");
        return "redirect:/index.jsp";
    }
}
轉(zhuǎn)發(fā)
重定向

很明顯,

  • 轉(zhuǎn)發(fā)是發(fā)生在服務(wù)器端,客戶端無感知。且url地址不會有變化。轉(zhuǎn)發(fā)后也可以獲取原始請求信息。
  • 重定向是瀏覽器發(fā)起的新請求,url地址有明顯變化。原始請求中的參數(shù)、屬性等信息也不會被重定向后的請求獲取到。

文件上傳

首先導(dǎo)入依賴

<!--文件上傳-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <!--servlet-api導(dǎo)入包-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

在springmvc-servlet.xml進行配置文件上傳

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--請求的編碼格式,必須和jsp的pageEncoding屬性一致,以便正確讀取表單的內(nèi)容,默認是ISO-885901-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--文件上傳的大小上限,單位為字節(jié)(10485760=10M)-->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

前端頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%--上傳文件表單--%>
    <form action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data" method="post">
      <input type="file" name="file"/>
      <input type="submit" value="upload" />
    </form>
  </body>
</html>

編寫Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws IOException {
        /* 1. 獲取文件名 */
        String uploadFileName = file.getOriginalFilename();
        if ("".equals(uploadFileName)) {
            redirectAttributes.addFlashAttribute("msg", "文件名為空!");
            return "redirect:/index.jsp";
        }
        System.out.println("上傳文件名為:" + uploadFileName);

        /* 2. 設(shè)置上傳文件保存路徑 */
        String uploadPath = "/Users/cseroad/IdeaProjects/studykuangshen/springmvc-02/";
        File realPath = new File(uploadPath);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上傳文件保存地址: " + realPath);

        /* 3. 將上傳文件保存到指定路徑 */
        file.transferTo(new File(realPath, uploadFileName));
        redirectAttributes.addFlashAttribute("msg", "文件上傳成功!");
        return "redirect:/index.jsp";
    }
}

不再需要自己寫上傳文件過程,而采用file.Transto 來保存上傳的文件。該方法本身沒有任何漏洞。

image.png

上傳成功以后客戶端只是一個302重定向。

文件下載

創(chuàng)建一個前端頁面

<h1>測試下載</h1>
<a href="${pageContext.request.contextPath}/download">點擊下載</a>

編寫一個controller

 @RequestMapping("/download")
    public String downloads(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileName = "123.png";
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
        File file = new File("/Users/cseroad/IdeaProjects/studykuangshen/springmvc-02/", fileName);
        InputStream fin = new FileInputStream(file);
        OutputStream out = response.getOutputStream();

        byte[] buff = new byte[1024];
        int len =0;
        while ((len=fin.read(buff))!=-1){
            out.write(buff,0,len);
            out.flush();
        }

        out.close();
        fin.close();
        return null;
    }

點擊下載的時候前端無感知。

總結(jié)

學(xué)習(xí)了SpringMVC的一些操作,忽然就理解了部分滲透時遇到的奇奇怪怪的現(xiàn)象。

參考資料

https://www.bilibili.com/video/BV1aE41167Tu?p=1&vd_source=0627d2723fb97773126096556cc98e0d
https://blog.csdn.net/m0_69305074/article/details/124619703
https://github.com/ljingen/kuangstudy/blob/main/%E7%8B%82%E7%A5%9EJAVA-16-SpringMVC%E5%AD%A6%E4%B9%A0.md

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容