1. 程式人生 > >Spring Boot起步以及整合themaleaf

Spring Boot起步以及整合themaleaf

1、建立專案

2、選擇Maven專案

Maven配置檔案

開啟專案

檢視專案結構

新增依賴pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <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>

  <!-- 1、spring-boot-starter-parent依賴 -->
  <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>
    <!-- 2、新增Spring web依賴 -->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 3、新增thymeleaf依賴 -->
    <dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

  </dependencies>
</project>

錯誤通用編碼


public class CodeMsg {
	
	private int code;
	private String msg;
	
	//通用的錯誤碼
	public static CodeMsg SUCCESS = new CodeMsg(0, "success");
	public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服務端異常");
	public static CodeMsg BIND_ERROR = new CodeMsg(500101, "引數校驗異常:%s");
	//登入模組 5002XX
	public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已經失效");
	public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登入密碼不能為空");
	public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手機號不能為空");
	public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手機號格式錯誤");
	public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手機號不存在");
	public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密碼錯誤");
	
	//商品模組 5003XX
	
	//訂單模組 5004XX
	
	//秒殺模組 5005XX
	
	private CodeMsg( ) {
	}
			
	private CodeMsg( int code,String msg ) {
		this.code = code;
		this.msg = msg;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public CodeMsg fillArgs(Object... args) {
		int code = this.code;
		String message = String.format(this.msg, args);
		return new CodeMsg(code, message);
	}

	@Override
	public String toString() {
		return "CodeMsg [code=" + code + ", msg=" + msg + "]";
	}
	
	
}

泛型

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);
	}
	
	/**
	 *  失敗時候的呼叫
	 * */
	public static  <T> Result<T> error(CodeMsg codeMsg){
		return new Result<T>(codeMsg);
	}
	
	private Result(T data) {
		this.data = data;
	}
	
	private Result(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	private Result(CodeMsg codeMsg) {
		if(codeMsg != null) {
			this.code = codeMsg.getCode();
			this.msg = codeMsg.getMsg();
		}
	}
	
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
}

編寫啟動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainApplication.class, args);
    }
}
SampleController
@Controller
@RequestMapping("/demo")
public class SampleController {

    /**
     * 使用Model往模板裡面傳遞引數
     * @param model
     * @return
     */
    @RequestMapping("/hello/themaleaf")
    public String themaleaf(Model model) {
        model.addAttribute("name", "Joshua");
        return "hello";
    }

}

編寫themaleaf配置

application.properties

#thymeleaf
# 字首
spring.thymeleaf.prefix=classpath:/templates/
# 字尾
spring.thymeleaf.suffix=.html

建立themaleaf模板

hello.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>

將resources新增到原始碼目錄

滑鼠右擊resources->Mark Directory as ->Sources Root

右擊MainApplication ->Run 'MainApplication'

訪問:localhost:8080/demo/thymeleaf

備註:IDEA匯入專案

選中專案