當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
012_SpringBoot视图层技术thymeleaf-条件判断
生活随笔
收集整理的這篇文章主要介紹了
012_SpringBoot视图层技术thymeleaf-条件判断
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 使用maven構(gòu)建SpringBoot的名叫spring-boot-view-thymeleaf-if-switch項(xiàng)目
2. 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.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-if-switch</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標(biāo)簽不規(guī)范報(bào)錯(cuò) --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動(dòng)器 --><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>3. 在src/main/resources/templates下新建condition.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf條件判斷</title></head><body><h3>Thymeleaf if條件判斷</h3><span th:if="${sex} == '男'">性別: 男</span><span th:if="${sex} == '女'">性別: 女</span><hr/><h3>Thymeleaf switch條件判斷</h3><div th:switch="${id}"><span th:case="1">ID為1</span><span th:case="2">ID為2</span><span th:case="3">ID為3</span></div></body> </html>4. 新建UserController.java
package com.bjbs.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class UserController {@RequestMapping("/condition")public String condition(Model model) {model.addAttribute("sex", "女");model.addAttribute("id", "1");return "condition";} }5. 新建App.java
package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動(dòng)類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }6. 啟動(dòng)項(xiàng)目, 并使用瀏覽器訪問
總結(jié)
以上是生活随笔為你收集整理的012_SpringBoot视图层技术thymeleaf-条件判断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 011_SpringBoot视图层技术t
- 下一篇: 013_SpringBoot视图层技术t