Request域对象共享的五种方式

4 篇文章 0 订阅
4 篇文章 0 订阅

一:写入控制器方法

1.通过ServletAPI向Request域对象进行共享

 

    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){//调用ServletAPI请求作为形参

        request.setAttribute("testRequestScope","Hello ServletAPI!!");//传递参数(第一个对应参数名,第二个是传递的参数内容)

        return "success";  //跳转到对应的前端页面

    }

2.通过ModelAndView向Request域对象进行共享

public ModelAndView testModelAndView(ModelAndView modelAndView){

           modelAndView.addObject("testAndView","hello,ModelAndView");

           modelAndView.setViewName("success");

           return modelAndView;

       }

3.通过Model向Request域对象进行共享

       @RequestMapping("/testModel")
       public String testModel(Model model){
        model.addAttribute("testModel","Hello,Model");
        return "success";
       }

4.通过Map向Request域对象进行共享

       @RequestMapping("/testMap")
      public String testMap(Map<String,Object> map){

           map.put("testMap","hello,Map!");

           return "success";

       }

5.通过ModelMap向Request域对象进行共享

            @RequestMapping("/testModelMap")
            public String testMap(ModelMap modelMap){
        
            modelMap.addAttribute("modelMap","hello,modelMap!");
        
            return "success";
        
           }

二:编写对应的前端页面(JSP为例)

 1.index.jsp

<%@ taglib prefix="th" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 86159
  Date: 2022/4/16
  Time: 15:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index</title>
</head>
<body>
<a href="/testRequestByServletAPI.do">ServletAPI方法</a><br>
<a href="/testModelAndView.do">ModeAndView方法</a><br>
<a href="/testModel.do">Model方法</a><br>
<a href="/testMap.do">Map方法</a><br>
<a href="/testModelMap.do">ModelMap方法</a><br>
</body>
</html>

  2.success.jsp

<%--@elvariable id="testRequestScope" type="com.zhan.HelloController.ContextController"--%>
<%@ taglib prefix="th" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 86159
  Date: 2022/4/16
  Time: 15:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试域对象共享</title>
</head>
<body>
<h1>域对象共享成功!</h1>

<%-- 获取request请求中参数 --%>
<%=request.getAttribute("testRequestScope")%><br>
<%=request.getAttribute("testAndView")%><br>
<%=request.getAttribute("testModel")%><br>
<%=request.getAttribute("testMap")%><br>
<%=request.getAttribute("testModelMap")%>
</body>
</html>

三:结果

点击对应的方法(即不同的地址映射请求),就会在对应请求中出现参数, 如:

 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛变涡流

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值