Validation jsf一次验证两个字段

Validation jsf一次验证两个字段,validation,jsf,Validation,Jsf,我可以用一个验证器验证中的两个相互依赖的字段吗 <h:form> <h:inputText value="#{logRegBean.person.name}" > <f:validator validatorId="loginCorrectValidator" /> </h:inputText> <h:inputSecret value="#{logRe

我可以用一个验证器验证中的两个相互依赖的字段吗

     <h:form>
            <h:inputText value="#{logRegBean.person.name}" >
                <f:validator validatorId="loginCorrectValidator" />
            </h:inputText>
            <h:inputSecret value="#{logRegBean.person.password}" />
            <h:commandButton action="#{logRegBean.login}" />
        </h:form>


我想在数据库中搜索用户,如果有用户,我将测试密码(在数据库中输入)是否匹配。但是我如何访问一个验证器中的密码字段呢?我试图通过
createValueExpression()
计算另一个字段中的值,但由于我总是得到空字符串,因此在这段时间内我似乎无法访问该值。

最好的方法是通过
validate()
方法获取另一个组件,然后通过(发生在组件树中当前已验证组件之后)或(发生在当前组件之前,因此已验证)

例如

另见:

JSF中的验证机制旨在验证单个组件。
但是,在实践中,在让值传播到模型中之前,通常需要确保相关组件具有合理的值。
例如,要求用户在单个文本字段中输入日期不是一个好主意。
相反,您将使用三个不同的文本字段,分别表示日期、月份和年份

如果用户输入非法日期,如2月30日,则您希望显示验证错误并防止非法数据进入模型

诀窍是将验证器附加到最后一个组件。在调用其验证器时,前面的组件通过了验证并设置了它们的本地值。最后一个组件通过了转换,转换后的值作为验证方法的对象参数传递

当然,您需要访问其他组件。您可以通过使用包含当前表单的所有组件的支持bean轻松实现该访问。只需将验证方法附加到支持bean:

public class BackingBean {

    private int day;
    private int month;
    private int year;

    private UIInput dayInput;
    private UIInput monthInput;
    private UIInput yearInput;

    // PROPERTY: day
    public int getDay() { return day; }
    public void setDay(int newValue) { day = newValue; }

    // PROPERTY: month
    public int getMonth() { return month; }
    public void setMonth(int newValue) { month = newValue; }

    // PROPERTY: year
    public int getYear() { return year; }
    public void setYear(int newValue) { year = newValue; }

    // PROPERTY: dayInput
    public UIInput getDayInput() { return dayInput; }
    public void setDayInput(UIInput newValue) { dayInput = newValue; }

    // PROPERTY: monthInput
    public UIInput getMonthInput() { return monthInput; }
    public void setMonthInput(UIInput newValue) { monthInput = newValue; }

    // PROPERTY: yearInput
    public UIInput getYearInput() { return yearInput; }
    public void setYearInput(UIInput newValue) { yearInput = newValue; }

    public void validateDate(FacesContext context, UIComponent component, Object value) {

       int d = ((Integer) dayInput.getLocalValue()).intValue();
       int m = ((Integer) monthInput.getLocalValue()).intValue();
       int y = ((Integer) value).intValue();

       if (!isValidDate(d, m, y)) {
          throw new ValidatorException(new FacesMessage("Invalid Date"));
       }

    }

    private static boolean isValidDate(int d, int m, int y) {
        //DO YOUR VALIDATION HERE
    }

 }
这是您的JSP

 <html>

   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

    <f:view>

       <head></head>

       <body>

          <h:form>

             <h:panelGrid columns="3">

                <h:inputText value="#{bb.day}"   binding="#{bb.dayInput}" size="2" required="true"/>

                <h:inputText value="#{bb.month}" binding="#{bb.monthInput}" size="2" required="true"/>

                <h:inputText value="#{bb.year}"  binding="#{bb.yearInput}" size="4" required="true" validator="#{bb.validateDate}"/>

                <h:message for="year" styleClass="errorMessage"/>

             </h:panelGrid>

             <h:commandButton value="Submit" action="submit"/>

          </h:form>

       </body>

    </f:view>

 </html>

参考: 核心JavaServer™ 面孔 大卫·盖里,凯伊·霍斯特曼

出版商:艾迪生·韦斯利 发布日期:2004年6月15日
ISBN:0-13-146305-5

我认为SeamFaces'可能正是您所需要的。(这是一个非常有用的库,它为JSF带来了一些基于CDI的漂亮特性。)

我也有同样的问题。我在代码中添加了示例代码。但是我遇到了nullpointer异常。你能告诉我原因吗?@Kayser:通常,当代码试图访问/调用一个实际为
null
的引用时,你会遇到这种情况。另请参见DateIntervalValidator不能为null
public void validate(FacesContext context,UIComponent component,Object value){UIInput fromComponent=(UIInput)context.getViewRoot().findComponent(“from”);Date fromDate=(Date)fromComponent.getSubmittedValue();
@Kayser try findComponent(:from”),其中是表单的实际id(ommit角括号)。不要吃肉。请注意,对于绝对ID,在
UIViewRoot\findComponent()中的ID之前需要一个
。我认为,根据文档,它似乎遵循与XHTML ID搜索相同的规则。在Seam停止使用并且验证转移到beanvalidation.org[]@KarlRichter之后,有人能提供或链接关于
s:validateForm
在哪里可用的信息吗?据我所知,表单验证还没有在Apache DeltaSpike中,Seam的继任者。目前,您仍然可以使用Seam的
s:validateForm
或RichFaces的
rich:graphValidator
。那么,如果输入有效日期,例如2017年2月28日,然后将日期更改为30,会发生什么情况?
 <html>

   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

    <f:view>

       <head></head>

       <body>

          <h:form>

             <h:panelGrid columns="3">

                <h:inputText value="#{bb.day}"   binding="#{bb.dayInput}" size="2" required="true"/>

                <h:inputText value="#{bb.month}" binding="#{bb.monthInput}" size="2" required="true"/>

                <h:inputText value="#{bb.year}"  binding="#{bb.yearInput}" size="4" required="true" validator="#{bb.validateDate}"/>

                <h:message for="year" styleClass="errorMessage"/>

             </h:panelGrid>

             <h:commandButton value="Submit" action="submit"/>

          </h:form>

       </body>

    </f:view>

 </html>