Why can I not get the submitted value from component binding?

邮差的信 提交于 2019-12-13 16:40:11

问题


In register.xhtml page, I have 2 inputText components for password and confirm password as following:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">

   <h:form>

      <h:outputText style="font-weight: bold" value="Password: " />
      <p:password feedback="true" minLength="9" 
                  binding="#{mrBean.passwordComponent}"
                  id="password" value="#{mrBean.password}"/>
      <p:message for="password" id="passwordMsg" />

      <h:outputText style="font-weight: bold" value="Confirm password: " />
      <p:password feedback="false" minLength="9" 
                  id="confirmPassword" value="#{mrBean.confirmPassword}"
                  validator="#{mrBean.validateConfirmPassword}>
         <f:attribute name="oriPassword" value="#{mrBean.passwordComponent.submittedValue}"/>
         <p:ajax process="password confirmPassword" update="confirmPasswordMsg" /> 
      </p:password>
      <p:message for="confirmPassword" id="confirmPasswordMsg" />

   </h:form>

</html>

And this is my mrBean:

@ManagedBean
@RequestScoped
public class MrBean {

    private String  password;
    private String  confirmPassword;
    private UIInput passwordComponent;

    public void validateConfirmPassword(FacesContext context, UIComponent toValidate,
            Object value) throws ValidatorException {

        String passwordStr        = (String) toValidate.getAttributes().get("oriPassword");
        String confirmPasswordStr = (String) value;

        if (!confirmPasswordStr.equals(passwordStr)) {
            FacesMessage message = new FacesMessage("The 2 passwords do not match.");
            throw new ValidatorException(message);
        }
    }

}

In another page, I also have a similar bean with similar validate function for email & confirmEmail and it works perfectly. However, I have no idea why it couldn't work here. The passwordStr is always null even though I have already entered the password.

I'd be very grateful if someone could show me what I have done wrong here.

Best regards, James Tran


回答1:


JSF components are processed in the order they appear in the component tree. During validations phase, for each component the submitted value will be retrieved by getSubmittedValue(), converted and validated. If no exceptions occurred during conversion and validation, then the submitted value will be set to null and the converted and validated value will be set as local value by setValue().

You're trying to reference the submitted value of the component which has already been processed at that point. The submitted value will only be non-null when conversion/validation failed for that value. You need to reference its local value instead.

<f:attribute name="oriPassword" value="#{mrBean.passwordComponent.value}"/>

See also:

  • How validate two password fields by ajax? (this example does the other way round, so that you don't need an additional property for confirm password)


来源:https://stackoverflow.com/questions/8366846/why-can-i-not-get-the-submitted-value-from-component-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!