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

时间:2019-04-21 14:56:15

标签: spring thymeleaf

在页眉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]

2 个答案:

答案 0 :(得分: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 :(得分: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

相关问题