当前位置: 首页 > 知识库问答 >
问题:

Bean验证不适用于mojarra 2.2.4

柴彬
2023-03-14

我正在尝试使用Hibernate Validator 5.0.1和JSF2.2,但自mojarra版本2.2.3以来,它们的集成似乎被破坏了。我创建了一个小应用程序来演示这个问题,并获得异常“javax.servlet.ServletException:表达式错误:命名对象:未找到javax.faces.Bean”在Tomcat 7.0.42上运行时。

还有其他人有这个问题吗?

webapp/页面/索引。xhtml:

  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:body>
    <h:form>
        <h:messages/>
        <h:inputText value="#{theBean.z}">
            <f:validateBean/>
        </h:inputText>
        <h:commandButton value="send z"/>
    </h:form>
  </h:body>
  </html>

豆子。JAVA

package lo.test;

import javax.faces.bean.ManagedBean;
import javax.validation.constraints.Min;

@ManagedBean
public class TheBean {

    @Min(5)
    private Integer z;

    public Integer getZ() {
        return z;
    }

    public void setZ(Integer z) {
        this.z = z;
    }
}

网状物xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>pages/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

依赖关系:

"org.hibernate:hibernate-validator:5.0.1.Final"
"com.sun.faces:jsf-impl:2.2.4"
"com.sun.faces:jsf-api:2.2.4"

异常的全栈跟踪:

javax.faces.FacesException: Expression Error: Named Object: javax.faces.Bean not found.
at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1596)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.createValidator(ValidatorTagHandlerDelegateImpl.java:245)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyAttachedObject(ValidatorTagHandlerDelegateImpl.java:133)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyNested(ValidatorTagHandlerDelegateImpl.java:212)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.apply(ValidatorTagHandlerDelegateImpl.java:88)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:190)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:190)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:190)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:161)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:980)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

共有3个答案

郎曜文
2023-03-14

这确实适用于Mojara 2.2.2,但不适用于较新版本。感谢Michail的提示。这是另一种预配置面孔的方法:

public class FacesContextBootstrap implements ServletContextListener
{
    public static final String BEANS_VALIDATION_AVAILABILITY_CACHE_KEY = "javax.faces.BEANS_VALIDATION_AVAILABLE";

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().remove(BEANS_VALIDATION_AVAILABILITY_CACHE_KEY);
        FacesContext.getCurrentInstance().getExternalContext().getApplicationMap()
                .put(BeanValidator.VALIDATOR_FACTORY_KEY, Validation.buildDefaultValidatorFactory());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
常茂
2023-03-14

Mojarra 2.2.3到2.2.6在Tomcat或Jetty等servlet容器上似乎会出现问题,因为Mojarra试图通过JNDI加载bean验证。

我在JavaLand 2014上与Ed Burns讨论了这个问题,应该使用Mojarra 2.2.7修复这个问题(参见JAVASERVERFACES-3183)。

辛星宇
2023-03-14

我可以用这种方式修复它:

public static final String BEANS_VALIDATION_AVAILABILITY_CACHE_KEY = "javax.faces.BEANS_VALIDATION_AVAILABLE";

@PostConstruct
private void init() {
    FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().remove(BEANS_VALIDATION_AVAILABILITY_CACHE_KEY);
    FacesContext.getCurrentInstance().getExternalContext().getApplicationMap()
            .put(BeanValidator.VALIDATOR_FACTORY_KEY, Validation.buildDefaultValidatorFactory());
}
 类似资料:
  • 我有一个bean验证,根据: 在我的小面我有: 当我有这个时,它可以正常工作,只是将显示所有字段ID的所有消息。我想为每个字段指定,如下所示: 然而,当我添加for=“km”选项时,不会显示任何验证消息。我错过了什么还是一个错误? 顺致敬意,

  • 我正在使用spring(4.2.0.RELEASE)、hibernate validator(5.2.1.Final)和validation api(1.1.0.Final)对后端应用程序进行JSR验证,配置如下:, 但是没有一个JSR303注释在我的应用程序中工作。 注意:在POJO类上添加了JSR303注释,在服务类(使用POJO)上添加了@Validated注释,还尝试在方法级别添加@Val

  • 我正在尝试在我的Jenkins工具中配置LDAP身份验证。我已经在Jenkins上完成了以下LDAP身份验证的设置,但我仍然无法登录。 服务器:ldaps://rootdc1.myweb.com:636 根DN:dc=myweb,dc=com 提前感谢!

  • 我试图通过将请求参数直接绑定到用户实体来简化我的代码,而不是一个字段一个字段地复制值,但是我似乎不能通过这种方式得到验证。 控制器: 验证器: 模型(注意嵌套对象用@Valid标注): 问题是只有密码和重新密码字段被正确验证,用户类中的验证注释被忽略,任何密码匹配的请求都通过,但它应该会出现错误。空密码或不同的密码会按预期出现错误。

  • 我使用Python和selenium(PhantomJS webdriver)来解析网站,但遇到了问题。 我想从这个电台网站获取当前歌曲:http://www.eskago.pl/radio/eska-warszawa. xpath: 该xpath不适用于python selenium 错误: Traceback(最近一次调用):File"parser4.py",第41行,p.loop()File

  • 我正在使用WebSphere Liberty 19.0.0.2和WebProfile-8.0特性,该特性支持JAXRS-2.1和BeanValidation-2.0。为了更好地支持多部分流,我使用Jersey作为JAX-RS实现,而不是Liberty的默认Apache CXF。 有关相关组件版本控制的一些更多上下文信息 Bean验证1.1(JSR 349)、Bean验证2.0(JSR 380) 下