Java --- SpringMVC域对象共享数据

目录

一、使用servletAPI向request域对象共享数据

二、使用ModelAndView向request域对象共享数据

三、使用Model向request域对象共享数据

四、使用Map向request域对象共享数据

五、使用ModelMap向request域对象共享数据

六、Model、Map、ModelMap三者关系

七、向session域共享数据

八、向application域共享数据


一、使用servletAPI向request域对象共享数据

@Controller
public class ScopeController {
    //使用servletAPI向request域共享数据
    @RequestMapping("/testRequest")
    public String testRequest(HttpServletRequest request){
        request.setAttribute("testRequestScope","hello");
        return "success";
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequest}">测试servletAPI共享数据</a>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功</h1><br>
<p th:text="${testRequestScope}"></p>
</body>
</html>

二、使用ModelAndView向request域对象共享数据

    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        //处理模型数据,向请求域request共享数据
        modelAndView.addObject("testRequestScope","modelAndView");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

三、使用Model向request域对象共享数据

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

四、使用Map向request域对象共享数据

 @RequestMapping("/map")
    public String getMap(Map<String,Object> map){
        map.put("testRequestScope","map");
        return "success";
    }

五、使用ModelMap向request域对象共享数据

@RequestMapping("/modelMap")
    public String getModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","modelmap");
        return "success";
    }

六、Model、Map、ModelMap三者关系

Model、Map、ModelMap类型参数本质上都是BindingAwareModelMap类型

public interface Model {}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

七、向session域共享数据

@RequestMapping("/session")
    public String getSession(HttpSession session){
        session.setAttribute("testSessionScope","session");
        return "success";
    }

前端页面:

index.html

<a th:href="@{/session}">使用session域</a>

 success.html

<p th:text="${session.testSessionScope}"></p>

八、向application域共享数据

  @RequestMapping("/application")
    public String getApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope","application");
        return "success";
    }
<p th:text="${application.testApplicationScope}"></p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鸭鸭老板

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

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

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

打赏作者

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

抵扣说明:

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

余额充值