ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java秒杀系统方案优化1-2 集成Thymeleaf,Result结果封装

2022-03-26 21:31:41  阅读:190  来源: 互联网

标签:code java Thymeleaf CodeMsg thymeleaf Result msg hello


前言

   thymeleaf 跟 JSP 一样,就是运行之后,就得到纯 HTML了。 区别在与,不运行之前, thymeleaf 也是 纯 html ...
所以 thymeleaf 不需要 服务端的支持,就能够被以 html 的方式打开,这样就方便前端人员独立设计与调试, jsp 就不行了, 不启动服务器 jsp 都没法运行出结果来

集成Thymeleaf

  • pom文件中添加依赖
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  • 在controller控制器中添加一个新的方法

注意这里方法名必须跟路径一样,这个方法返回的 "hello"即templates目录下的hello.html,根据properties配置文件规则去匹配

并且带上数据"name",值为"luckey"

    @RequestMapping("/hello/thymeleaf")
    public String thymeleaf(Model model) {
        model.addAttribute("name", "luckey");
        return "hello";
    }
  • 页面hello.html

hello.html 做了如下事情:
1. 声明当前文件是 thymeleaf, 里面可以用th开头的属性  

<html xmlns:th="http://www.thymeleaf.org">

 

2.把 name 的值显示在当前 p里,用的是th开头的属性: th:text, 而取值用的是 "${name}" 这种写法叫做 ognl,通过这种方式,把服务端的数据显示在当前html里

3.字符串拼写。 两种方式,一种是用加号,一种是在前后放上 ||.

 

<p th:text="'hello:'+${name}" ></p>
<p th:text="|hello: ${name}|" ></p>

 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'hello:'+${name}" >name</p>
</body>
</html>
  • application.properties 中添加配置
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
  • 重启测试 http://localhost:8080/demo/hello/thymeleaf

  

Result结果封装

 一般服务端返回给页面是json格式,常见是封装一个返回实体类,如下结构所示:

{
    "code":0
    "msg":"success"
    "data": object
}

 

 

 

 这种方式并不是很好,具体是每次返回都需要new 一个新的类,且code和msg硬编码,可以考虑把code,msg继续封装成一个对象.

@Setter
@Getter
public class Result<T> {
    private int code;
    private String msg;
    // 成功时返回的data数据
    private T data;

    /**
     *  成功时候的调用
     * */
    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }

    /**
     *  失败时候的调用
     * */
    public static  <T> Result<T> error(CodeMsg codeMsg){
        return new Result<T>(codeMsg);
    }

    private Result(T data) {
        this.data = data;
        this.code = 0;
        this.msg = "success";
    }

    private Result(CodeMsg codeMsg) {
        if(codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

}
@Getter
@Setter
public class CodeMsg {

    public static final CodeMsg SUCCESS = new CodeMsg(0,"success");
    public static final CodeMsg SERVER_ERROR = new CodeMsg(500100,"服务端异常");

    private int code;
    private String msg;

    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

这样我们接口方法返回时可以统一为 Result.success() 或者Result.err().代码更加简洁优雅.

    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello(){
        return Result.success("hello,world.");
    }

    @RequestMapping("/helloError")
    @ResponseBody
    public Result<CodeMsg> helloError(){
        return Result.error(CodeMsg.SERVER_ERROR);
    }

页面返回结果:

后记

标签:code,java,Thymeleaf,CodeMsg,thymeleaf,Result,msg,hello
来源: https://www.cnblogs.com/luckey33/p/16060321.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有