SpringMvc—域对象共享数据和视图

一、向request域创建对象

先创建首页:

在testController这个类中:

java 复制代码
package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class testController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

index.html:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>

1.使用servletAPI向request域对象共享数据

在scopeController这个类中:

java 复制代码
package com.pon.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class scopeController {
@RequestMapping("/scope")
    public String Scope(HttpServletRequest request){
    request.setAttribute("GetscopeObject","Hello Servlret");
        return "success";
    }
}

在success.html中:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

在首页中要跳转到success界面,index.html:

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="@{/scope}">Servlet API域对象共享</a>
</body>
</html>

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

在scopeController中:

java 复制代码
@Controller
public class scopeController {
@RequestMapping("/mv")
    public ModelAndView testmv(){
     ModelAndView mav=new ModelAndView();
        //向请求域request共享数据
        mav.addObject("GetscopeObject","Hello ModelandView");
        //设置视图名称
        mav.setViewName("success");
        return mav;
    }
}

在success.html中:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

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

在scopeController中:

java 复制代码
@Controller
public class scopeController {
 @RequestMapping("/m")
    public String testModel(Model model){
    model.addAttribute("GetscopeObject","hello model");
    return "success";
    }
}

success.html:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

4.使用Map向request域对象共享数据

在scopeController中:

java 复制代码
@Controller
public class scopeController {
@RequestMapping("/map")
    public String testMap(Map<String, Object> map){
    map.put("GetscopeObject","hello map");
    return "success";
    }
}

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

在scopeController中:

java 复制代码
@Controller
public class scopeController {
 @RequestMapping("/modelmap")
    public String testMM(ModelMap modelMap){
    modelMap.addAttribute("GetscopeObject","hello ModelMap");
    return "success";
    }
}

6.Model、ModelMap、Map三者之间的关系

二、向session域共享数据

在scopeController中:

java 复制代码
@Controller
public class scopeController {
 @RequestMapping("/testsession")
    public String testMM(HttpSession session){
      session.setAttribute("testSession","hello session");
      return "success";
    }
}

在success.html:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${session.testSession}"></p>
</body>
</html>

三、向application域共享数据

在scopeController中:

java 复制代码
@Controller
public class scopeController {@RequestMapping("/application")
    public String testap(HttpSession session){
     ServletContext application= session.getServletContext();
     application.setAttribute("testapplication","hello,servletcontext=application");
     return "success";
    }
}

success.html:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${application.testapplication}"></p>
</body>
</html>

四、SpringMvc的视图

1.ThymeleafView

在viewController类中:

java 复制代码
package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class viewController {
    @RequestMapping("/view")
    public String view(){
        return "view";
    }
    //超链接的路径
    @RequestMapping("/testview")
    public String testview(){
        return "hi";
    }
}

view.html:

html 复制代码
<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
</body>
</html>

hi.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
你好
</body>
</html>

2.转发视图

在viewController类中:

java 复制代码
package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class viewController {
    @RequestMapping("/view")
    public String view(){
        return "view";
    }
    @RequestMapping("/testview")
    public String testview(){
        return "hi";
    }
    @RequestMapping("/testforword")
    public String testfoeword(){
        return "forward:/testview";
    }
}

view.html:

html 复制代码
<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
<a th:href="@{/testforword}">转发视图</a>
</body>
</html>

hi.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
你好
</body>
</html>

3.重定向视图

在viewController类中:

java 复制代码
@Controller
public class viewController {
    @RequestMapping("/view")
    public String view(){
        return "view";
    }
    @RequestMapping("/testview")
    public String testview(){
        return "hi";
    }
    @RequestMapping("/testforword")
    public String testfoeword(){
        return "forward:/testview";
    }
    @RequestMapping("/testredirect")
    public String testRedirect(){
        return "redirect:/testview";
    }
}

view.html:

html 复制代码
<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
<a th:href="@{/testforword}">转发视图</a>
<a th:href="@{/testredirect}">重定向视图</a>
</body>
</html>


4.视图控制器view-controller

相关推荐
无尽的大道3 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力12 分钟前
Java类和对象(下篇)
java
binishuaio15 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE17 分钟前
【Java SE】StringBuffer
java·开发语言
老友@17 分钟前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
superman超哥25 分钟前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
颜淡慕潇27 分钟前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
wrx繁星点点33 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui35 分钟前
Aop+自定义注解实现数据字典映射
java
zzzgd81636 分钟前
easyexcel实现自定义的策略类, 最后追加错误提示列, 自适应列宽,自动合并重复单元格, 美化表头
java·excel·表格·easyexcel·导入导出