主なことは、SpringBootプロジェクトフレームワーク(IDEAバージョン)を構築することです。
ここでは、タイムリーフテンプレートの導入とResult pomファイルのカプセル化
(thymeleaf依存関係の導入)を示します。
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>miaosha_1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>miaosha_1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
application.propertiesのコードは
thymeleafを構成します
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
アクセス
するWebページのテンプレートのHTMLコード
<!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}" ></p>
</body>
</html>
結果の結果
をカプセル化します出力jsonデータが次のようになることを期待し
ます。デフォルトのコード= 0が成功します。
{"Code":
"msg":
"data":jsonオブジェクトまたは配列は正常です
}
結果のCodeMsg.javaコード
は正しいですコードとメッセージのカプセル化、エラー発生時に簡単に呼び出すことができます
public class CodeMsg {
private int code;
private String msg;
//通用异常
//默认code=0是成功的
public static CodeMsg SUCCESS = new CodeMsg(0, "success");
public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
//登录模块 5002XX
//商品模块 5003XX
//订单模块 5004XX
//秒杀模块 5005XX
get和set方法
}
結果のResult.javaコード
public class Result<T> {
private int code;
private String msg;
private T data;
/**
* 成功时候的调用,只需要数据
* */
public static <T> Result<T> success(T data){
return new Result<T>(data);
}
/**
* 失败时候的调用,需要知道code和msg
* */
public static <T> Result<T> error(CodeMsg cm){
return new Result<T>(cm);
}
//success时的构造函数,定义成私有的都应该懂吧
private Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
//fail时的构造函数
private Result(CodeMsg cm) {
if(cm == null) {
return;
}
this.code = cm.getCode();
this.msg = cm.getMsg();
}
get方法
}
コントローラのコード
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
//1.rest api json输出 2.页面
@RequestMapping("/hello")
@ResponseBody
public Result<String> hello() {
return Result.success("hello,imooc");//成功的时候这样写,所以在 result中定义了对应的方法
// return new Result(0, "success", "hello,imooc");这样写是硬编码
}
@RequestMapping("/helloError")
@ResponseBody
public Result<String> helloError() {
return Result.error(CodeMsg.SERVER_ERROR);//异常的时候这样写
//return new Result(500102, "XXX");这样写是硬编码
}
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model) {
model.addAttribute("name", "Joshua");
return "hello";
}
}