SpringBoot封装响应数据怎么实现

今天小编给大家分享的是SpringBoot封装响应数据怎么实现,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

业务处理

这是通过 Spring 在 Controller中注入Service模型层

而在 Service模型层 结合 Mybatis / Mybatis-Plus 进行数据加工, 数据持久化

封装响应值

将 业务处理得到数据封装到 Model作用域中, 伴随着转页将信息传递到页面

传值容器

Model

在Controller中新建立 方法 test08, 并在参数中增加 Model, 注意导包

通过 Model 的 .addAttribute(key, value); 封装数据

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
@Controller
public class TestController {
    @RequestMapping("/test/test08")
    public String test08(Model model){
        // 封装数据
        model.addAttribute("data", "这是要响应的动态信息");
        System.out.println(" controller 中的测试方法 test 08 ");
        return "ref";
    }
}

修改ref.html页面 使用 thymeleaf 接值

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    hello spring boot <br>
    <span th:text="${data}"></span>
</body>
</html>

在 浏览器中测试, 页面显示接收到的信息

ModelMap

Model 类 有个简化版本 ModelMap ,

因为此类是继承自 HashMap, 所以可以使用.put( "key", value);进行数据封装

当然还是可以使用 .addAttribute("key", value); , 推荐使用这个方法 , 相比 put()方法, 这个方法增加验证代码

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.ModelMap;
@Controller
public class TestController {
    @RequestMapping("/test/test09")
    public String test09(ModelMap modelMap){
        // 封装数据
        // modelMap.put("data", "这是要响应的动态信息");
        modelMap.addAttribute("data", "这是要响应的动态信息");
        System.out.println(" controller 中的测试方法 test 09 ");
        return "ref";
    }
}
HttpServletRequest

本质上 Model 相当于 Request 作用域 , SpringBoot 也提供了 Request的使用

同样 可以使用参数传入 , 如 : test10

也可以通过 Spring 的依赖注入方式 , 如 : test11

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class TestController {
    @Autowired
    private HttpServletRequest request;
    @RequestMapping("/test/test11")
    public String test11(){
        // 封装数据
        request.setAttribute("data", "这是要响应的动态信息");
        System.out.println(" controller 中的测试方法 test 11 ");
        return "ref";
    }
    @RequestMapping("/test/test10")
    public String test10(HttpServletRequest request){
        // 封装数据
        request.setAttribute("data", "这是要响应的动态信息");
        System.out.println(" controller 中的测试方法 test 10 ");
        return "ref";
    }
}

重定向传值

在SpringBoot 中 重定向 就是 一个方法执行完, 再对另一个方法发请求

这时通过 Model 就不能传递值, 可以通过 RedirectAttributes 传值

从 test12 重定向 到 test13 以 data 为标识进行传值

    @RequestMapping("/test/test12")
    public String test12(RedirectAttributes redirectAttributes){
        // 封装数据
        redirectAttributes.addAttribute("data", "这是重定向传递的信息");
        System.out.println(" controller 中的测试方法 test 12 ");
        return "redirect:test13";
    }
    @RequestMapping("/test/test13")
    public String test13(String data){
        System.out.println("data = " + data);
        System.out.println(" controller 中的测试方法 test 13 ");
        return "ref";
    }

关于SpringBoot封装响应数据怎么实现就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

相关推荐:

Springboot如何查询MySQL DATE字段?

springboot日期查询mysql date字段时的问题 在使用springboot查询mysql date字段时,可能会遇到日期类型不匹配的问题。这是因为springboot接收时间时默认为timestamp类型,而mysql date字段是日期类型。 解决方案 解决方法是将前端传来的日期字符串直接转换成date类型,然后使用mybatis-plus进行查询。代码示例如下:// 后端参数接收...

springboot怎么集成shiro框架

要在Spring Boot项目中集成Apache Shiro框架,可以按照以下步骤进行操作: 添加依赖:在pom.xml文件中添加Shiro和Spring Boot Shiro相关的依赖。例如: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot...

SpringBoot CommandLine如何配置

SpringBoot CommandLine是一个用于构建命令行应用的工具。要配置SpringBoot CommandLine,可以按照以下步骤进行操作: 添加依赖:在项目的pom.xml文件中添加SpringBoot CommandLine的依赖。 <dependency> <groupId>org.springframework.boot</groupId>...

SpringBoot CommandLine的优势何在

Spring Boot CommandLine 的优势主要有以下几点: 简化开发流程:Spring Boot CommandLine 可以帮助开发者快速构建命令行应用程序,简化了配置和开发流程。开发者只需要关注业务逻辑的实现,而不需要花费大量的时间去配置项目。 集成了常用功能:Spring Boot CommandLine 提供了许多常用的功能和组件,例如参数解析、命令行选项、日志记录等,开发者可...

使用SpringBoot CommandLine需要注意什么

在使用Spring Boot CommandLine时,需要注意以下几点: 引入必要的依赖:在pom.xml文件中添加Spring Boot CommandLine的依赖,如spring-boot-starter,spring-boot-starter-web等。 创建主应用程序类:创建一个包含main方法的主应用程序类,并使用@SpringBootApplication注解标记它。 实现命令行应...

SpringBoot CommandLine可以做哪些事情

Spring Boot CommandLine 可以用于以下几个方面: 运行Spring Boot应用程序:使用CommandLine可以直接在命令行中启动Spring Boot应用程序,而不需要使用IDE或者构建工具。 执行自定义命令:通过CommandLine可以执行自定义命令,例如初始化数据库、调用外部API、生成报告等。 与操作系统交互:可以通过CommandLine与操作系统交互,执行系...

SpringBoot CommandLine怎样解析参数

SpringBoot应用程序可以通过CommandLineRunner接口来解析命令行参数。以下是一个简单的示例: 首先,创建一个CommandLineRunner接口的实现类,并实现run方法: import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @...

SpringBoot CommandLine与Shell脚本如何交互

要在SpringBoot应用中与Shell脚本交互,可以使用Java中的ProcessBuilder类来执行Shell命令,并通过标准输入输出流来与Shell脚本交互。 以下是一个简单的示例代码,演示了如何在SpringBoot应用中执行Shell脚本并与其交互: import java.io.BufferedReader; import java.io.IOException; import j...