【发布时间】:2025-12-09 03:15:01
【问题描述】:
我有一个问题:
场景: 我有一个 JSF-2,Spring(Beans 布线)应用程序。我编写了一个自定义验证器,我想执行它。
@FacesValidator("com.test.vali")
@Named("com.test.vali")
public class TestValidator implements Validator {
@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException {
System.out.println("dhkdfkbhdfbkdfksdfdfk");
}
}
我尝试使用以下方式注入验证器:
方式#1:
<h:inputText value="#{helloWorld.name}">
<f:validator binding="#{com.test.vali}" />
</h:inputText>
输出
当试图渲染页面时,它抛出了一个异常。
javax.servlet.ServletException: /testRichFaces.xhtml @17,48 <f:validator> A validator id was not specified. Typically the validator id is set in the constructor ValidateHandler(ValidatorConfig)
对此进行了很多搜索,并验证了以下几种方法:
- Java 文件位于包中。
方式#2
<f:validator validatorId="com.test.vali" />
输出
javax.servlet.ServletException: Expression Error: Named Object: com.test.vali not found.
因此,从方式#1 和方式#2 来看,我可以解释没有任何注释对我有用。
然后,我尝试转向最后一种方法:
方式#3:在 faces-config.xml 中添加验证器,只是为了表明我使用的是 2.0 合规性:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
验证器配置为:
<validator>
<validator-id>com.test.vali</validator-id>
<validator-class>teet.TestValidator</validator-class>
</validator>
输出
作品
现在问题来了,即使使用 JSF-2.0,我也不得不求助于 faces-config.xml。
我做错了什么?
如果需要更多配置,请告知。
【问题讨论】:
-
@Named 注释不是必需的。你是如何管理你的 backing bean 的。如果它们使用 Spring 注释进行注释,那么您必须在 faces-config.xml 中配置 org.springframework.web.jsf.el.SpringBeanFacesELResolver。