f:validateRequired对Tomcat不起作用

时间:2012-03-02 01:36:24

标签: java jsf tomcat

我在第一个JSF项目上使用GlassFish(仍在学习JSF),一切都很好。但后来,我转而使用Tomcat(GlassFish导致了一些问题,Tomcat托管更便宜)。现在我遇到了一个我无法解决的问题:使用f验证表单:验证停止工作。

我已经尝试了很多解决方案,最常提到的解决方案是将 validation-api.jar hibernate-validator.jar 添加到libs中 - 这只会导致表单停止工作(见上面的错误)。

现在我真的不知道该怎么做。我使用的是NetBeans 7.1,Tomcat 7.0.22.0。任何帮助表示赞赏。谢谢!

哦,是的......有趣的是,通过正则表达式(电子邮件)进行验证是有效的,但前提是输入不是空的。 (并没有添加libs)

validation-api.jar hibernate-validator.jar 添加到lib并提交表单后出现错误消息:

javax.validation.ValidationException: Unable to find a default provider
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
    at javax.faces.validator.BeanValidator.validate(BeanValidator.java:276)
    at javax.faces.component.UIInput.validateValue(UIInput.java:1149)
    at javax.faces.component.UIInput.validate(UIInput.java:967)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

表格:

<h:form>
    <table>
        <tr>
            <th><label for="registration-email">#{usersTexts.email}:</label></th>
            <td>
                <h:inputText value="#{userRegistry.newUser.email}" id="registration-email" validatorMessage="#{msg.invalidEmail}" maxlength="100">
                    <f:validateRequired />
                    <f:validateRegex pattern="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$" />
                </h:inputText>
            </td>
            <td><p:message for="registration-email" /></td>
            <td rowspan="4">
                <!--p:captcha immediate="true" validatorMessage="#{msg.wrongCaptcha}" requiredMessage="#{formsTexts.captcha} #{msg.isEmpty}" /-->
            </td>
        </tr>
        <tr>
            <th><label for="registration-name">#{usersTexts.name}:</label></th>
            <td>
                <h:inputText value="#{userRegistry.newUser.name}" id="registration-name" validatorMessage="#{formsTexts.field} #{usersTexts.name} #{msg.isEmpty}" maxlength="100">
                    <f:validateRequired />
                </h:inputText>
            </td>
            <td><p:message for="registration-name" /></td>
        </tr>
        <tr>
            <th><label for="registration-password1">#{usersTexts.password}:</label></th>
            <td>
                <h:inputSecret value="#{userRegistry.newUser.password}" binding="#{password}" id="registration-password1" validatorMessage="#{formsTexts.field} #{usersTexts.password} #{msg.isEmpty}">
                    <f:validateRequired />
                </h:inputSecret>
            </td>
            <td><p:message for="registration-password1" /></td>
        </tr>
        <tr>
            <th><label for="registration-password2">#{usersTexts.passwordAgain}:</label></th>
            <td>
                <h:inputSecret id="registration-password2" validatorMessage="#{msg.passwordsNotEqual}">
                    <f:validateLength minimum="#{user.MIN_PASSWORD_LENGTH}" />
                    <f:validator validatorId="equality" />
                    <f:attribute name="matchAgainst" value="#{password}" />
                </h:inputSecret>
            </td>
            <td><p:message for="registration-password2" /></td>
        </tr>
        <tr>
            <td colspan="2" class="submit_row">
                <h:commandButton value="#{usersTexts.register}" action="#{userRegistry.register}" />
            </td>
        </tr>
    </table>
</h:form>

2 个答案:

答案 0 :(得分:0)

好吧,在朋友的帮助下我解决了它...不知道原因,但如果输入为空,则忽略所有f:validate标签 - 包括f:validateRequired,因此根本没有验证

通过删除f:validateRequired代码并使用required="true"属性解决了这个问题。因此,输入值不能为空(required="true"有效),而且所有其他f:验证器都有效。

答案 1 :(得分:0)

这种情况正在发生,因为需要设置以下属性。 默认情况下,javax.faces.VALIDATE_EMPTY_FIELDS设置为false。