Spring Boot Thymeleaf条件判断

      Thymeleaf中使用th:if和th:unless属性进行条件判断,标签只有在th:if中的条件成立时才显示,th:unless与th:if恰好相反,只有条件不成立时,才会显示其内容。

测试Thymeleaf条件判断

程序清单:/springboot2/src/main/java/com/dwx/hello/ThymeleafController.java

package com.dwx.hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
@Controller
public class ThymeleafController {
	@RequestMapping("/ifTest")
	public String ifTest(WebRequest request) {
		request.setAttribute("userName", "jack", WebRequest.SCOPE_REQUEST);
		request.setAttribute("sex", "0", WebRequest.SCOPE_REQUEST);
		request.setAttribute("age", 22, WebRequest.SCOPE_REQUEST);
		return "success";
	}
}

程序清单:/springboot2/src/main/resources/templates/success.html





thymeleaf条件判断






	

thymeleaf条件判断

条件成立时显示

用户名:

条件不成立时显示

地址为空!

Spring Boot项目启动后,访问以下地址:http://localhost:8080/ifTest

Spring Boot Thymeleaf条件判断_第1张图片

你可能感兴趣的:(spring,boot)