Spring Thymeleaf - 用于显示HTML项目的调用服务方法布尔值

问题描述 投票:1回答:2

在我的Header HTML中,我显示了一个UL / LI菜单,其中一个LI项的可见性取决于Service方法调用。

我试过这个:

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]
spring thymeleaf
2个回答
1
投票

您正在引用Thymeleaf中的实例方法。这有两个选择:

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

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

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

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

为了避免使用NPE,您也可以在#bools.isTrue(isChangeUserAllowed)实用程序中使用bools或相应的方法。

这是Thymeleaf文档所采用的首选方式和路径。一个明显的好处是前端现在不依赖于服务。

2)静态引用它(不推荐):

Error trying call method from view Thymeleaf Spring


旁白:推荐的方法是使用构造函数注入而不是自动装配。


1
投票

除了bphilipnyc的答案(将直接值设置到模型中),

model.addAttribute("isChangeUserAllowed", etdService.isChangeUserAllowed());

如果你需要全局化常见的模型属性而不是每次都重新添加,那么解决方案就是带有@ControllerAdvice@ModelAttribute类,例如:

/**
 * This class is used to globalize common Model Attributes without re-adding every time
 * The approach is to mark it as @ControllerAdvice to make it apply to every Controller method, 
 * and implement a @ModelAttribute Model-Adder to append to the model on every Controller method.
 */

// Makes the methods of this class apply to all Controller Request methods
@ControllerAdvice
public class GlobalController {

    @Autowired
    MyService myService;

    @ModelAttribute   // A Model Attribute Adder method
    public void setGlobalModelAttributes(HttpServletRequest request, Model model) {

        model.addAttribute("isChangeUserAllowed", myService.isChangeUserAllowed());
        model.addAttribute("currentUserFullName", myService.getCurrentUserFullName());

    }       
}

更多的例子

https://stackoverflow.com/a/33879102/1005607

https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation

© www.soinside.com 2019 - 2024. All rights reserved.