SpringMVC 使用Servlet原生API作为参数

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88895469

SpringMVC支持直接使用ServletAPI 作为目标方法的入参。

MVC 的 Handler 方法可以接受如下ServletAPI 类型的参数:

• HttpServletRequest

• HttpServletResponse

• HttpSession

• java.security.Principal

• Locale

• InputStream

• OutputStream

• Reader

• Writer

例如:

index.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=gbk" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <a href="HelloWorld3/testServletAPI">testServletAPI</a>
  </body>
</html>

HelloWorld3.class类

package com.helloworld;

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

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

@Controller
@RequestMapping("HelloWorld3")
public class HelloWorld3 {
    private String success = "success";

    @RequestMapping("testServletAPI")
    public String testServletAPI(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request + "  " + response);
        return success;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88895469
今日推荐