SpringMVC 使用Servlet原生API作为参数

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

  
    $Title$
  
  
    testServletAPI
  

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;
    }
}

 

你可能感兴趣的:(Spring,MVC)