Spring Thymeleaf - 用于显示 HTML 项目的调用服务方法布尔值 [英] Spring Thymeleaf - Call Service Method Boolean for Display of HTML Item

查看:19
本文介绍了Spring Thymeleaf - 用于显示 HTML 项目的调用服务方法布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的标题 HTML 中,我显示了一个 UL/LI 菜单,其中一个 LI 项的可见性取决于服务方法调用.

In my Header HTML I display a UL/LI Menu where the visiblity of one of the LI items depends on a Service method call.

我试过了:

HomeController

@Controller
public class HomeController {
    private static final Logger log = LogManager.getLogger(HomeController.class);

    @Autowired 
    private EtdService etdService;

    @GetMapping("/home")
    public String home(Model model) throws EtdException {

        model.addAttribute("tierTemplate", etdService.getTierTemplate());  
        // Also tried this explicitly
        model.addAttribute("etdService", etdService);
        return "home";
    }
}

服务接口(EtdService)

public interface EtdService {
  boolean isChangeUserAllowed();
}

服务实现 (EtdServiceImpl)

@Component
public class EtdServiceImpl implements EtdService {

    @Override
    public boolean isChangeUserAllowed() {
        System.out.println("Got here");
        return false;
    }

}

HTML:

<li th:if="${@etdService.isChangeUserAllowed()}" class="nav-item dropdown" id="changeUserPanel" role="presentation">
<!-- ... Definition of this LI -- note can't put a new DIV in a UL list ... -->
</li>

错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'etdService' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]

推荐答案

您正在引用 Thymeleaf 中的实例方法.这里有两个选项:

You are referencing an instance method in Thymeleaf. Here are two options:

1) 通过将布尔值添加到模型中来引用它:

1) Reference it by adding the value of the boolean to the model:

@GetMapping("/home")
public String home(Model model) throws EtdException {

   //...
   model.addAttribute("isChangeUserAllowed", etdService.isChangeUserAllowed());
   return "home";
}

在你的 HTML 中:th:if="${isChangeUserAllowed}"

And in your HTML: th:if="${isChangeUserAllowed}"

为了避免 NPE,您也可以使用 #bools.isTrue(isChangeUserAllowed)bools 实用程序中的适当方法.

To avoid NPEs, you can alternatively use #bools.isTrue(isChangeUserAllowed) or the appropriate method in the bools utility.

这是 Thymeleaf 文档采用的首选方式和路径.一个明显的好处是前端现在与服务无关.

This is the preferred way and the path that the Thymeleaf documentation takes. A clear benefit is that the front-end is now not tied to the service.

2) 改为静态引用(不推荐):

2) Reference it statically instead (not recommended):

尝试从 Thymeleaf Spring 视图调用方法时出错

另外:推荐的方法是使用构造函数注入而不是自动装配.

Aside: the recommended way is to use constructor injection instead of autowiring.

这篇关于Spring Thymeleaf - 用于显示 HTML 项目的调用服务方法布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆