【问题标题】:Why can I not get the submitted value from component binding?为什么我无法从组件绑定中获取提交的值?
【发布时间】:2012-01-12 02:28:42
【问题描述】:

在 register.xhtml 页面中,我有 2 个inputText 密码组件并确认密码如下:

<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>

这是我的 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);
        }
    }

}

在另一个页面中,我也有一个类似的 bean,它具有类似的电子邮件和确认功能验证功能,它工作得很好。但是,我不知道为什么它不能在这里工作。 passwordStr 始终是 null,即使我已经输入了密码。

如果有人能告诉我我在这里做错了什么,我将不胜感激。

最好的问候, 詹姆斯·特兰

【问题讨论】:

  • 也许验证器方法应该是validateConfirmPassword 而不是validateConfirmEmail。您发布了正确的代码吗?
  • @VineetReynolds:感谢您纠正我:P。我打错了代码。我已经用正确的代码更新了我的问题。

标签: java jsf-2 primefaces validation


【解决方案1】:

JSF 组件按照它们在组件树中出现的顺序进行处理。在验证阶段,对于每个组件,提交的值将由getSubmittedValue() 检索、转换和验证。如果在转换和验证过程中没有发生异常,则提交的值将设置为null,转换和验证的值将由setValue()设置为本地值。

您正在尝试引用此时已处理的组件的提交值。只有当该值的转换/验证失败时,提交的值才会是非null。您需要改为引用其本地值。

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

另见:

【讨论】:

  • 非常感谢!你的答案一如既往的完美=)。
猜你喜欢
  • 1970-01-01
  • 2020-04-16
  • 2011-10-02
  • 1970-01-01
  • 2013-06-18
  • 2012-06-18
  • 2018-10-02
  • 1970-01-01
  • 2020-08-13
相关资源
最近更新 更多