Ai
1 Star 0 Fork 1

汪少棠/jpaTransactional

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ThymeleafController.java 6.69 KB
一键复制 编辑 原始数据 按行查看 历史
package com.wmx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
/**
* Spring Boot 集成 Thymeleaf 快速入门 & Spring MVC 数据响应
*
* @author wangMaoXiong
* @version 1.0
* @date 2022/4/9 8:33
*/
@Controller
public class ThymeleafController {
/**
* http://localhost:8080/thymeleaf/home
* 1、org.springframework.ui.Model 是专门用于封装返回的数据的,可以直接 new,也可以直接放在参数中自动注入
*
* @param responseMap
* @return
*/
@RequestMapping("thymeleaf/home")
public String goHome(Map<String, Object> responseMap, Model model) {
// 向页面返回数据方式1:默认 responseMap 的内容会放到请求域中,调整后的新页面上可以直接使用 Thymeleaf 表达式取值
responseMap.put("name", "张三");
responseMap.put("age", 35);
// 向页面返回数据方式2:使用 org.springframework.ui.Model 向页面返回数据
model.addAttribute("code", 200);
model.addAttribute("msg", "管理员向你表示祝贺!");
// 全部基于 Spring Boot 给 Thymeleaf 的默认配置
// 自动跳转到默认的 classpath:/templates/home.html 页面
return "home";
}
/**
* http://localhost:8080/modelAndView/v1/toHome
* 1、org.springframework.web.servlet.ModelAndView:Web MVC 框架中模型和视图的持有者,以便控制器可以在一个返回值中同时返回模型和视图
* * Model :模型,用于封装返回的数据
* * View :视图,用于展示数据的视图(如 html)
*
* @return
*/
@GetMapping("modelAndView/v1/toHome")
public ModelAndView toHome() {
Map<String, Object> responseMap = new HashMap<>(8);
responseMap.put("name", "李四");
responseMap.put("age", 33);
responseMap.put("code", "1000");
responseMap.put("msg", "登陆成功。");
// ModelAndView(String viewName, @Nullable Map<String, ?> model)
ModelAndView modelAndView = new ModelAndView("home", responseMap);
return modelAndView;
}
/**
* http://localhost:8080/modelAndView/v2/toHome
*
* @param modelAndView :可以在方法里面直接 new modelAndView(),也可以放在参数中自动注入
* @return
*/
@GetMapping("modelAndView/v2/toHome")
public ModelAndView toHome(ModelAndView modelAndView) {
Map<String, Object> responseMap = new HashMap<>(8);
responseMap.put("name", "王五");
responseMap.put("age", 34);
responseMap.put("code", "1001");
responseMap.put("msg", "登陆成功。");
// 设置此ModelAndView的视图名称
modelAndView.setViewName("home");
// 将提供的映射中包含的所有属性添加到模型中
modelAndView.addAllObjects(responseMap);
return modelAndView;
}
/**
* http://localhost:8080/request/home
* 最原始的 HttpServletRequest 方式像页面返回数据
*
* @param request
* @return
*/
@RequestMapping("request/home")
public String goHome(HttpServletRequest request) {
request.setAttribute("name", "张三哥");
request.setAttribute("age", 45);
request.setAttribute("code", 2000);
request.setAttribute("msg", "管理员向你表示祝贺.");
// 全部基于 Spring Boot 给 Thymeleaf 的默认配置
// 自动跳转到默认的 classpath:/templates/home.html 页面
return "home";
}
/**
* http://localhost:8080/response/body
* 1、前后台分离的项目,不再需要后台进行页面跳转,后台只需要专注数据处理即可,直接将数据返回给前端
*
* @return
*/
@GetMapping("response/body")
@ResponseBody
public Map<String, Object> body() {
Map<String, Object> responseMap = new HashMap<>(8);
responseMap.put("name", "王老五");
responseMap.put("age", 37);
responseMap.put("code", "1001");
responseMap.put("msg", "登陆成功。");
return responseMap;
}
/**
* http://localhost:8080/print/writer
* 通过输出流的方式返回数据,此时同样不需要在指定跳转视图,通常用于文件下载,验证码图片输出等等
*
* @param response
* @throws IOException
*/
@GetMapping("print/writer")
public void body(HttpServletResponse response) throws IOException {
// 指示服务器响应数据的类型以及编码,告诉浏览器以此种编码进行解码,下面两种方式结果一致
response.setHeader("Content-Type", "text/html;charset=utf-8");
// response.setContentType("text/html;charset=utf-8");
Map<String, Object> responseMap = new HashMap<>(8);
responseMap.put("name", "王老吉");
responseMap.put("age", 39);
responseMap.put("code", "1001");
responseMap.put("msg", "登陆成功");
// response 获取的流浏览器端的编码
PrintWriter printWriter = response.getWriter();
printWriter.print(responseMap.toString());
printWriter.flush();
printWriter.close();
}
/**
* http://localhost:8080/print/outputStream
* 通过输出流的方式返回数据,此时同样不需要在指定跳转视图,通常用于文件下载,验证码图片输出等等
*
* @param response
* @throws IOException
*/
@GetMapping("print/outputStream")
public void outputStream(HttpServletResponse response) throws IOException {
// 指示服务器响应数据的类型以及编码,告诉浏览器以此种编码进行解码,下面两种方式效果一致
// response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
Map<String, Object> responseMap = new HashMap<>(8);
responseMap.put("name", "王老吉");
responseMap.put("age", 40);
responseMap.put("code", "1001");
responseMap.put("msg", "登陆成功");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(responseMap.toString().getBytes());
outputStream.flush();
outputStream.close();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/wangmx1993/jpa-transactional.git
git@gitee.com:wangmx1993/jpa-transactional.git
wangmx1993
jpa-transactional
jpaTransactional
master

搜索帮助