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 分钟前
昆虫学(书籍学习资料)
数据库·数据挖掘·数据分析
Wang's Blog30 分钟前
Webpack: 持久化缓存大幅提升构建性能
前端·缓存·webpack
程序员储物箱32 分钟前
Vue报错:Module not found: Error: Can‘t resolve ‘less-loader‘ in ‘文件地址‘
前端·vue
乐吾乐科技33 分钟前
【国产开源可视化引擎Meta2d.js】锚点
前端·编辑器·web·数据可视化·大屏端
Her...1 小时前
electron教程(一)创建项目
前端·javascript·electron
logstach2 小时前
QML-Grid和OpacityMask
前端·qml
C+ 安口木3 小时前
前端代码规范 - 日志打印规范
前端·代码规范
虫小宝3 小时前
如何在Java中实现批量数据处理
java·开发语言
king888866663 小时前
Java中的AQS
java
冰暮流星3 小时前
软设之类的继承与泛化,多重继承
java·开发语言