【问题标题】:Jsf 2, Displaying validation messages inside dataTableJsf 2,在dataTable中显示验证信息
【发布时间】:2012-03-16 05:00:50
【问题描述】:

我在显示由 ui 组件触发的验证错误时遇到问题,这些组件是 嵌套在数据表中。

这是xhtml页面,其中包含一个带有静态上部的表单,可以在其中输入地址。

在它下方显示订购商品,用户可以在其中输入他们想要订购的商品数量。 这些项目是从数据库表中检索出来的,并显示在 dataTable 中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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:f="http://java.sun.com/jsf/core">

<ui:composition template="../templates/_standard.xhtml">    

    <ui:define name="pageHeadline">
            #{msg['supplies.module_headline']}
    </ui:define>

    <ui:define name="pageContent">

        <h:form id="supplies" styleClass="editForm" rendered="#{!suppliesHandler.sent}">
            <h:panelGrid
                    columns="2"
                    columnClasses="tdLabel,tdValue"
                    rowClasses="row"
                    styleClass="leftPane"
                    > 

                    <!-- row 1 -->
                    #{msg['supplies.account']}:
                    <h:panelGroup>
                        <h:inputText id="account" value="#{supply.contact.account}" tabindex="1" styleClass="text"/>
                        <h:message for="account" styleClass="error"/>
                    </h:panelGroup>

                    <!-- row 2 -->
                    #{msg['supplies.company']}:
                    <h:panelGroup>
                        <h:inputText id="company" value="#{supply.contact.company}" tabindex="2" styleClass="text"/>
                        <h:message for="company" styleClass="error"/>
                    </h:panelGroup>             

                    <!-- row 3 -->
                    #{msg['supplies.street']}:
                    <h:panelGroup>
                        <h:inputText id="street" value="#{supply.contact.street}" tabindex="3" styleClass="text"/>
                        <h:message for="street" styleClass="error"/>
                    </h:panelGroup>             

                    <!-- row 4 -->
                    #{msg['supplies.postcode']}:
                    <h:panelGroup>
                        <h:inputText id="postcode" value="#{supply.contact.postcode}" tabindex="4" styleClass="text"/>
                        <h:message for="postcode" styleClass="error"/>
                    </h:panelGroup>

                    <!-- row 5 -->
                    #{msg['supplies.city']}:
                    <h:panelGroup>
                        <h:inputText id="city" value="#{supply.contact.city}" tabindex="5" styleClass="text"/>
                        <h:message for="city" styleClass="error"/>
                    </h:panelGroup>

                </h:panelGrid>

                <h:panelGrid
                    columns="2"
                    columnClasses="tdLabel,tdValue"
                    rowClasses="row"
                    styleClass="rightPane"
                    > 

                    <!-- row 2 -->
                    #{msg['supplies.contact']}:
                    <h:panelGroup>
                        <h:inputText id="contact" value="#{supply.contact.contact}" tabindex="6" styleClass="text"/>
                        <h:message for="contact" styleClass="error"/>
                    </h:panelGroup>

                    <!-- row 3 -->                      
                    #{msg['supplies.phone']}:
                    <h:panelGroup>
                        <h:inputText id="phone" value="#{supply.contact.phone}" tabindex="7" styleClass="text"/>
                        <h:message for="phone" styleClass="error"/>
                    </h:panelGroup>

                    <!-- row 4 -->  
                    #{msg['supplies.email']}:
                    <h:panelGroup>
                        <h:inputText id="email" value="#{supply.contact.email}" tabindex="8" styleClass="text">
                            <f:validator validatorId="com.abc.myproduct.be.ui.validator" />
                        </h:inputText>
                        <h:message for="email" styleClass="error"/>
                    </h:panelGroup> 

                    <!-- row 5 -->
                    #{msg['supplies.fax']}:
                    <h:panelGroup>
                        <h:inputText id="fax" value="#{supply.contact.fax}" tabindex="9" styleClass="text"/>
                        <h:message for="fax" styleClass="error"/>
                    </h:panelGroup>

                </h:panelGrid>

                <div class="spacer"></div>

                <h:dataTable id="items" 
                    styleClass="listing_large" 
                    value="#{supply.supplyItems}" 
                    headerClass="heading" 
                    var="item">
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.id']}
                        </f:facet>
                        <h:outputText value="#{item.supply_id}" />
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.amount']}
                        </f:facet>
                        <h:inputText value="#{item.amount}" id="amount" styleClass="text" size="3" maxlength="3" style="width:50px"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.description']}
                        </f:facet>
                        <h:outputText value="#{item.description}" />
                    </h:column>
                </h:dataTable>

                <div><br/>
                    <h:messages globalOnly="true" layout="table"  styleClass="error"/>
                </div>

                <h:panelGrid
                    columns="1"
                    columnClasses="tdLabel,tdValue"
                    rowClasses="row">   

                    <!-- row 2 -->
                    <h:panelGroup>
                        <h:commandButton value="#{msg['general.submit']}" action="#{suppliesHandler.submitMessage}" styleClass="button"/>
                    </h:panelGroup>

                </h:panelGrid>          

            </h:form>
            <h:messages globalOnly="true" layout="table" rendered="#{suppliesHandler.sent}"/>

    </ui:define>

</ui:composition>
</html>

表单地址部分的验证非常完美。 只有这部分表单的消息没有显示:

<h:dataTable id="items" 
                    styleClass="listing_large" 
                    value="#{supply.supplyItems}" 
                    headerClass="heading" 
                    var="item">
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.id']}
                        </f:facet>
                        <h:outputText value="#{item.supply_id}" />
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.amount']}
                        </f:facet>
                        <h:inputText value="#{item.amount}" id="amount" styleClass="text" size="3" maxlength="3" style="width:50px"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            #{msg['supplies.description']}
                        </f:facet>
                        <h:outputText value="#{item.description}" />
                    </h:column>
                </h:dataTable>

正在通过 BeanValidation 进行验证:

public class SupplyItem implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private long supply_id;

private String description;
private int orderNo;

@Transient
@Max(value=200)
private int amount;

/*
 * constructor
 */
public SupplyItem() {
    super();
}

public long getSupply_id() {
    return supply_id;
}

public void setSupply_id(long supply_id) {
    this.supply_id = supply_id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getOrderNo() {
    return orderNo;
}

public void setOrderNo(int orderNo) {
    this.orderNo = orderNo;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

}

它实际上正在被验证,但是没有显示消息......

12:29:45,860 信息 [javax.enterprise.resource.webcontainer.jsf.renderkit] (http--127.0.0.1-8080-2) 警告:FacesMessage(s) wurde(n) in die Warteschlange gestellt, aber möglicherweise nicht angezeigt . sourceId=supplies:items:0:amount[severity=(ERROR 2), summary=(允许的最大值为 200), detail=(允许的最大值为 200)] sourceId=supplies:items:1:amount[severity=(ERROR 2), summary=(supplies:items:1:amount: 'a' 必须是一个或多个数字组成的数字。), detail=(supplies:items :1:amount: 'a' 必须是介于 -2147483648 和 2147483647 之间的数字 例如:9346)]

尝试动态设置输入字段的 id 并结合 h:message for="" 不起作用,通过 h:messages globalOnly="true" 也没有显示。

任何帮助将不胜感激。

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    您尚未在数据表中的任何位置为输入字段添加&lt;h:message&gt;。你需要放一个

    <h:message for="amount" />
    

    数据表中您想要显示它们的位置。

    &lt;h:messages globalOnly="true"&gt; 只显示带有 null 客户端 ID 的消息,因此对于带有非 null 客户端 ID 的消息肯定根本不起作用。您需要删除 globalOnly="true" 才能显示其他任何地方都没有显示的消息。

    【讨论】:

    • Foutje..bedankt!那绝对太容易了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2012-09-05
    • 2011-09-08
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多