【问题标题】:How to get a boolean variable DEBUG in a Thymeleaf?如何在 Thymeleaf 中获取布尔变量 DEBUG?
【发布时间】:2020-03-11 22:43:12
【问题描述】:

如何判断项目是否处于调试模式,并在 thymeleaf 模板中获取逻辑类型的变量。

<h1 th:if="${DEBUG}">...</h1>

【问题讨论】:

  • 你能解释一下“在调试模式下”是什么意思吗?

标签: spring-boot thymeleaf


【解决方案1】:

您可以在将Environment bean 注入到您的控制器时实现此目的:

@Controller
@RequestMapping("/public")
public class PublicController {

  private final Environment environment;

  public PublicController(Environment environment) {
    this.environment = environment;
  }

  @GetMapping("/debug")
  public String returnFoo(Model model) {

    String envValue = environment.getProperty("debug");
    boolean isDebugMode = (envValue != null && !envValue.equals("false"));
    model.addAttribute("DEBUG", isDebugMode);

    System.out.println(isDebugMode);

    return "yourView";
  }
}

此实现适用于您可能为应用程序设置debug 标志的所有可能方式 (How can I tell whether my Spring boot application is in debug mode?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2018-08-02
    • 2012-09-27
    • 2010-11-05
    相关资源
    最近更新 更多