专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Spring MVC 教程(五)使用 Serlvet 原生的 API 作为目标方法的参数

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

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

代码实战:

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

 <!--开发JSP需要的依赖,里面有HttpServletRequest,
     HttpServletResponse等原生参数 begin  -->
     <dependency>
       <groupId>org.apache.tomcat</groupId>
       <artifactId>jsp-api</artifactId>
       <version>6.0.53</version>
     </dependency>

     <dependency>
       <groupId>org.apache.tomcat</groupId>
       <artifactId>servlet-api</artifactId>
       <version>6.0.48</version>
     </dependency>
     <!--开发JSP需要的依赖,里面有HttpServletRequest,
     HttpServletResponse等原生参数 end  -->

2、 编写index.jsp

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

3、 编写handle

 package com.study.springmvc.handlers;

 import java.io.IOException;
 import java.io.Writer;

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

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

 @RequestMapping("/servletAPITest")
 @Controller
 public class ServletAPITest {

     public static final String SUCCESS="success";

     /**
      * 可以使用 Serlvet 原生的 API 作为目标方法的参数 具体支持以下类型
      * 
      * HttpServletRequest 
      * HttpServletResponse 
      * HttpSession
      * java.security.Principal 
      * Locale InputStream 
      * OutputStream 
      * Reader 
      * Writer
      * @throws IOException 
      */
     @RequestMapping("/testServletAPI")
     public void testServletAPI(HttpServletRequest request,
             HttpServletResponse response, Writer out) throws IOException {
         System.out.println("testServletAPI, " + request + ", " + response);
         out.write("hello springmvc");
 //        return SUCCESS;
     }
 }

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

72_1.png

https://www.cnblogs.com/leeSmall/category/1093236.html

文章永久链接:https://tech.souyunku.com/13919

未经允许不得转载:搜云库技术团队 » Spring MVC 教程(五)使用 Serlvet 原生的 API 作为目标方法的参数

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们