SpringMVC系列(五)使用 Serlvet 原生的 API 作为目标方法的参数

SpringMVC的Handler方法可以接受哪些 ServletAPI 类型的参数

• HttpServletRequest
• HttpServletResponse
• HttpSession
• java.security.Principal
• Locale
• InputStream
• OutputStream
• Reader
• Writer

代码实战:

1.在pom.xml里面引入使用 Serlvet 原生的 API需要的依赖

 1 <!--开发JSP需要的依赖,里面有HttpServletRequest,
 2     HttpServletResponse等原生参数 begin  -->
 3     <dependency>
 4       <groupId>org.apache.tomcat</groupId>
 5       <artifactId>jsp-api</artifactId>
 6       <version>6.0.53</version>
 7     </dependency>
 8     
 9     <dependency>
10       <groupId>org.apache.tomcat</groupId>
11       <artifactId>servlet-api</artifactId>
12       <version>6.0.48</version>
13     </dependency>
14     <!--开发JSP需要的依赖,里面有HttpServletRequest,
15     HttpServletResponse等原生参数 end  -->

2. 编写index.jsp

1 <br/><br/>
2 <p><b>使用 Serlvet 原生的 API 作为目标方法的参数 begin</b></p>
3 <a href="servletAPITest/testServletAPI">testServletAPI</a>
4 <p><b>使用 Serlvet 原生的 API 作为目标方法的参数</b></p>

3. 编写handle

 1 package com.study.springmvc.handlers;
 2 
 3 import java.io.IOException;
 4 import java.io.Writer;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 
12 @RequestMapping("/servletAPITest")
13 @Controller
14 public class ServletAPITest {
15 
16     public static final String SUCCESS="success";
17     
18     /**
19      * 可以使用 Serlvet 原生的 API 作为目标方法的参数 具体支持以下类型
20      * 
21      * HttpServletRequest 
22      * HttpServletResponse 
23      * HttpSession
24      * java.security.Principal 
25      * Locale InputStream 
26      * OutputStream 
27      * Reader 
28      * Writer
29      * @throws IOException 
30      */
31     @RequestMapping("/testServletAPI")
32     public void testServletAPI(HttpServletRequest request,
33             HttpServletResponse response, Writer out) throws IOException {
34         System.out.println("testServletAPI, " + request + ", " + response);
35         out.write("hello springmvc");
36 //        return SUCCESS;
37     }
38 }

4.输入地址http://127.0.0.1:8080/SpringMVC/访问进入index.jsp点击第二部在index.jsp编写的超链接查看效果

原文地址:https://www.cnblogs.com/leeSmall/p/7818652.html