2015-09-05 49 views
0

在经历了一段绝望的尝试和错误之后,我来找你寻求帮助,因为现在我甚至不确定自己是否做得不对。在ui中没有触发JSF 2.2自定义验证器:重复

我的代码如下所示,显示只读输入文本框中用户配置文件的一些字段。数据来自一个bean,作为“字段”对象的ArrayList,其属性在ui:param指令中描述。 用户可以点击框旁边的解锁/锁定按钮开始/结束编辑她的数据。 当用户在编辑后按下Enter或点击锁定按钮时,我想触发输入验证。

但无论我尝试过,编辑并按下输入或单击按钮后,更改的值不会被写回,并且我的自定义验证程序不会被触发。不知何故,似乎单击按钮后,呈现的输入框不会绑定到该字段?然而,验证工作(和所有属性获得通过它)如果是始终呈现一个输入文本使用的 - 我不明白:)

请看看它。如果我没有足够好地解释我的意图,或者如果您需要更多代码,请告诉我。

编辑:支持bean的注释:

@ManagedBean(name = "beanProfile") 
@RequestScoped 

了JSF代码:

<ui:composition> 
    <ui:param name="i18n" value="#{field.i18n}" /> 
    <ui:param name="val" value="#{field.value}" /> 
    <ui:param name="req" value="#{field.required}" /> 
    <ui:param name="min" value="#{field.min}" /> 
    <ui:param name="max" value="#{field.max}" /> 
    <ui:param name="validationType" value="#{field.validationType}" /> 
    <ui:param name="useHidden" value="#{field.hidden}" /> 
    <ui:param name="locked" value="#{field.locked}" /> 
    <ui:param name="fullName" value="#{beanProfile.name}" /> 

    <h:form id="profileFrm"> 
     <h:outputText value="#{msg.role_profile}&nbsp;" styleClass="headingOutputText" /> 
     <h:outputText value="#{fullName}" styleClass="headerTitle" /> 
     <p />   
     <h:panelGroup> 
      <ui:repeat var="field" value="#{beanProfile.userFields}"> 
       <h:panelGrid columns="2"> 
        <h:outputText value="#{msg[i18n]}" styleClass="headerTitle" /> 
        <h:message showDetail="true" for="visInput" styleClass="warn" 
         rendered="#{!useHidden}" /> 
        <h:message showDetail="true" for="invisInput" styleClass="warn" 
         rendered="#{useHidden}" /> 
       </h:panelGrid> 
       <h:panelGrid columns="2" columnClasses="nameColumn"> 
        <h:inputText id="visInput" required="true" value="#{val}" 
         rendered="#{!useHidden}" readonly="#{locked}" > 
         <f:validator validatorId="customValidator" /> 
         <f:attribute name="requiredField" value="#{req}" /> 
         <f:attribute name="minLen" value="#{min}" /> 
         <f:attribute name="maxLen" value="#{max}" /> 
         <f:attribute name="type" value="#{validationType}" /> 
        </h:inputText> 
        <h:inputSecret id="invisInput" required="true" value="#{val}" 
         rendered="#{useHidden}" readonly="#{locked}"> 
         <f:validator validatorId="customValidator" /> 
         <f:attribute name="requiredField" value="#{req}" /> 
         <f:attribute name="minLen" value="#{min}" /> 
         <f:attribute name="maxLen" value="#{max}" /> 
         <f:attribute name="type" value="#{validationType}" /> 
        </h:inputSecret> 
        <h:commandLink id="lock" action="#{beanProfile.lock(field)}" 
         rendered="#{!locked}" title="#{msg.profile_lock}"> 
         <h:graphicImage height="16" width="16" 
          name="images/padlock_unlocked.svg" alt="#{msg.profile_lock}" /> 
        </h:commandLink> 
        <h:commandLink id="unlock" action="#{beanProfile.unlock(field)}" 
         rendered="#{locked}" title="#{msg.profile_unlock}"> 
         <h:graphicImage height="16" width="16" 
          name="images/padlock_locked.svg" alt="#{msg.profile_unlock}" /> 
        </h:commandLink> 
       </h:panelGrid> 
      </ui:repeat> 
     </h:panelGroup> 
     <p /> 
     <h:commandButton id="submitProfileBtn" value="#{msg.cmd_submit}" 
      styleClass="button" includeViewParams="true" 
      action="#{beanProfile.submitProfile()}" /> 
    </h:form> 
</ui:composition> 
+0

Colud你显示应用于beanProfile' backing bean的注释吗? – lametaweb

+0

更改为ViewScoped范围,包javax.faces.view – lametaweb

+0

我没有和得到这个错误:CDI @ViewScoped管理器不可 – Alfred

回答

1

问题的根源在于您使用的RequestScoped范围用于包含模型的支持bean,因此,当您想要执行回发请求以执行backing bean方法时,绑定只会消失(而不是服务器内存)并且JSF生命周期不按预期执行。解决方案是使用更长的作用域作为ViewScoped作用域。理想情况下,您应该使用兼容CDI的viewScoped范围,因此您必须使用JSF 2.2.x版本。

0

谢谢lametaweb,你使我在正确的方向。

暂时无法使用@ViewScoped注释我找到了链接

how-to-install-cdi-in-tomcat

,随后BalusC的方向为在Tomcat中使用CDI。然后我不得不让所有的java类实现Serializable接口。

现在我可以使用@ViewScoped注释并验证器被触发。

+0

您好,我很高兴你解决你的问题。我已经为最初的问题写了一个答案,请接受它。要将答案标记为已接受,请单击答案旁边的复选标记以将其从空心切换为绿色。 – lametaweb