SpringMVC 五、域对象共享数据

搭建Springmvc框架环境链接:https://blog.csdn.net/qq_37138380/article/details/124198537

三个常用域:

1.session 一次会话

2.request 一次请求

3.servletcontext 整个应用的范围,服务器开启到服务器关闭

前五种方式向域中共享数据,最终都是被ModelAndView封装

1、使用ServletAPI向request域对象共享数据

代码演示:

1.页面控制类
package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//首页 转发访问
@Controller
public class TestContorller {
    @RequestMapping("/")//此处的/与web.xml文件中的<url-pattern>/</url-pattern> 一致
    public String getindex(){
        return "index1";
    }

}
2.页面分发
package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller//分发页面请求
public class ScoperContorller {
    //使用servletAPI想request域共享对象
    @RequestMapping("/testRequestByServletApi")
    public String testRequestByServletApi(HttpServletRequest request){
        //向request域中存放共享数据
        request.setAttribute("testRequestScope","hello,servletAPI");
        return "success";//返回的页面
    }
}
3.index1.html 首页
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析<br/>
<a th:href="@{/testRequestByServletApi}">1.使用ServletAPI向request域对象共享数据</a><br/>
</body>
</html>
4.success.html 成功页面 并输出request域中的数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功页面</title>
</head>
<body>
success<br/>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析
<p th:text="${testRequestScope}" ></p>
</body>
</html>

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

在SpringMVC框架中建议使用:ModelAndView方式

代码演示:

1.
 //使用ModelAndView向request域对象共享数据
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        /**
         * ModelAndView有Model和 View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        /*创建对象*/
        ModelAndView mav=new ModelAndView();
        //处理模型数据,即想请求域request共享数据
        mav.addObject("testRequestScope","hello,ModelAndView");
        //设置视图名称 //返回的页面
        mav.setViewName("success");
        return mav;
    }
2.index.html
<a th:href="@{/testModelAndView}">2.使用ModelAndView向request域对象共享数据</a><br/>
3.success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功页面</title>
</head>
<body>
success<br/>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析
回现数据:<p th:text="${testRequestScope}" ></p>
</body>
</html>

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

代码演示:

1.
//3、使用Model向request域对象共享数据
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,Model");
        return "success";//返回的页面
    }
2.
<a th:href="@{/testModel}">3、使用Model向request域对象共享数据</a><br/>

4、使用map向request域对象共享数据

代码:

 //4、使用map向request域对象共享数据
    @RequestMapping("/testMap")
    public String testMap(Map<String,String> map){
        map.put("testRequestScope","hello,map");
        return "success";//返回的页面
    }


<a th:href="@{/testMap}">4、使用map向request域对象共享数据</a><br/>

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

代码:

//5、使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testRequestScope","hello,map");
    return "success";//返回的页面
}

index页面超链接

<a th:href="@{/testModelMap}">5、使用ModelMap向request域对象共享数据</a><br/>

6、Model、ModelMap、Map的关系

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

7、向session域共享数据

代码:

//7、向session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope","hello,session");
    System.out.println("testRequestScope:"+session.getAttribute("testRequestScope"));
    return "success";//返回的页面
}

success.html

session 回现数据:<p th:text="${session.testSessionScope}" ></p>

index.html

<a th:href="@{/testSession}">7、向session域共享数据</a><br/>

8、向application域共享数据

//8、向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){

    ServletContext servletContext = session.getServletContext();
    servletContext.setAttribute("testApplicationScope","hello,Application");
    System.out.println(servletContext);
    return "success";//返回的页面
}

index.html

<a th:href="@{/testApplication}">8、向application域共享数据</a><br/>

success.html

servletContext 回现数据:<p th:text="${application.testApplicationScope}" ></p>

 

上述完整的代码:

首页控制:

package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//首页 转发访问
@Controller
public class TestContorller {
    @RequestMapping("/")//此处的/与web.xml文件中的<url-pattern>/</url-pattern> 一致
    public String getindex(){
        return "index1";
    }

}

 2.分发页面

package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller//分发页面请求
public class ScoperContorller {
    //使用servletAPI想request域共享对象
    @RequestMapping("/testRequestByServletApi")
    public String testRequestByServletApi(HttpServletRequest request){
        //向request域中存放共享数据
        request.setAttribute("testRequestScope","hello,servletAPI");
        return "success";//返回的页面
    }
    //使用ModelAndView向request域对象共享数据
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        /**
         * ModelAndView有Model和 View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        /*创建对象*/
        ModelAndView mav=new ModelAndView();
        //处理模型数据,即想请求域request共享数据
        mav.addObject("testRequestScope","hello,ModelAndView");
        //设置视图名称 //返回的页面
        mav.setViewName("success");
        return mav;
    }
    //3、使用Model向request域对象共享数据
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,Model");
        return "success";//返回的页面
    }
    //4、使用map向request域对象共享数据
    @RequestMapping("/testMap")
    public String testMap(Map<String,String> map){
        map.put("testRequestScope","hello,map");
        return "success";//返回的页面
    }

    //5、使用ModelMap向request域对象共享数据
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","hello,ModelMap");
        return "success";//返回的页面
    }
    //Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的

    //6、Model、ModelMap、Map的关系
    @RequestMapping("/testModelMap2")
    public String testModelMap2(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的");
        return "success";//返回的页面
    }

    //7、向session域共享数据
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope","hello,session");
        System.out.println("testRequestScope:"+session.getAttribute("testRequestScope"));
        return "success";//返回的页面
    }

    //8、向application域共享数据
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){

        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope","hello,Application");
        System.out.println(servletContext);
        return "success";//返回的页面
    }

}

 3.首页页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析<br/>
<a th:href="@{/testRequestByServletApi}">1.使用ServletAPI向request域对象共享数据</a><br/>

<a th:href="@{/testModelAndView}">2.使用ModelAndView向request域对象共享数据</a><br/>

<a th:href="@{/testModel}">3、使用Model向request域对象共享数据</a><br/>

<a th:href="@{/testMap}">4、使用map向request域对象共享数据</a><br/>

<a th:href="@{/testModelMap}">5、使用ModelMap向request域对象共享数据</a><br/>

<a th:href="@{/testModelMap2}">6、Model、ModelMap、Map的关系</a><br/>

<a th:href="@{/testSession}">7、向session域共享数据</a><br/>

<a th:href="@{/testApplication}">8、向application域共享数据</a><br/>

</body>
</html>

4.成功回现页面:

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功页面</title>
</head>
<body>
success<br/>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析<br/>
回现数据:<p th:text="${testRequestScope}" ></p>

session 回现数据:<p th:text="${session.testSessionScope}" ></p>
servletContext 回现数据:<p th:text="${application.testApplicationScope}" ></p>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值