渲染表h:dataTable与f:ajax当select h:selectOneMenu混淆f:validateLength

时间:2015-05-20 22:12:07

标签: ajax jsf selectonemenu

尝试解决此问题。

如果单击“添加主题”按钮,则会在“名称”字段中显示所需的消息。但是,如果我选择该部门并再次点击添加主题,则所需的消息不会显示!

如果我删除了ajax调用:

f:ajax event="valueChange" render="topics"

有效!但是,当我选择一个元素时,我不会更新我的表格。 有没有办法在没有ajax调用的情况下更新表?或者我做错了什么?

此外,如果调用ajax,TopicBean中的showMessageInDialog将不再起作用......

topics.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!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://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="./css/pure-release-0.6.0/pure-min.css"/>
        <h:outputStylesheet name="./css/pure-release-0.6.0/grids-responsive-min.css"/>
        <title>Company</title>
    </h:head>

    <h:body>
        <h:form id="divisions_list" class="pure-form pure-form-stacked" enctype="multipart/form-data" style="margin: 15px">
            <div>
                <img src="images/logo.png" alt="Company Logo"/>
                <br/><br/>
            </div>
            <fieldset>
                <div class="pure-g">
                    <legend style="margin-top: 15px">Filters</legend>

                   <div class="pure-u-1 pure-u-md-1-3">
                        <label for="divisions">Division Name:</label>
                        <h:selectOneMenu id="divisions" value="#{TopicsBean.topic.division.id}">
                            <f:selectItem itemLabel="Select..." itemValue="-1" />
                            <f:selectItems value="#{TopicsBean.divisions}" var="division" itemValue="#{division.id}" itemLabel="#{division.name}" />
                            <f:ajax event="valueChange" render="topics" />
                        </h:selectOneMenu>
                    </div>

                    <legend style="margin-top: 15px">Topic List</legend>

                    <div class="pure-table">
                        <h:dataTable id="topics" value="#{TopicsBean.topics}" var="topics_list">
                            <h:column >
                                <f:facet name="header">#</f:facet>
                                <h:outputText value="#{topics_list.id}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">Name</f:facet>
                                <h:outputText value="#{topics_list.name}"/>
                            </h:column>
                        </h:dataTable>
                    </div>

                    <legend style="margin-top: 15px">Add Topic</legend>

                    <div class="pure-u-1 pure-u-md-1-3">
                        <label for="topic_name">Name:</label>
                        <h:inputText id="topic_name" size="30" maxlength="50" value="#{TopicsBean.topic.name}" validatorMessage="required" immediate="true">
                            <f:validateLength minimum="3" />
                        </h:inputText>
                        <h:message for="topic_name" style="color:red" showSummary="false"/>
                    </div>

                    <legend style="margin-top: 15px"></legend>

                    <br/>
                    <h:commandButton id="addTopic" value="Add Topic" action="#{TopicsBean.addTopic}" class="pure-button pure-button-primary"/>
                </div>
            </fieldset>
        </h:form>

        <p:dialog id="errorID" widgetVar="errorVar">
            <h:form id="errorForm">
                <p:commandButton value="OK" type="button" onclick="errorID.hide()" />
            </h:form><br/>                    
        </p:dialog>
    </h:body>

</html>

TopicsBean.java

import buildgc.jdbc.DBConnection;
import buildgc.model.Company;
import buildgc.model.Division;
import buildgc.model.Topic;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;

@ManagedBean(name="TopicsBean")
@SessionScoped
public class TopicsBean implements Serializable {
    private Division division;
    private Topic topic;
    private List<Division> divisions = new ArrayList<Division>();
    private List<Topic> topics = new ArrayList<Topic>();

    public TopicsBean(){
        defaultValues();
    }

    public void defaultValues(){
        this.division = new Division();
        this.topic = new Topic();                
    }

    public void addTopic(){
        if(this.topic.getDivision().getId() == -1){
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Missing data", "Please select a Division.");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
            return;
        }

        if(DBConnection.getInstance().saveTopic(this.topic)){
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Topic successfully added.");
            RequestContext.getCurrentInstance().showMessageInDialog(message);

            this.topic = new Topic(); 
       }else{
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Error", "Please contact the administrator.");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }

    public Division getDivision() {
        return division;
    }

    public void setDivision(Division division) {
        this.division = division;
    }

    public List<Company> getCompanies() {
        return DBConnection.getInstance().getCompanies();
    }

    public List<Division> getDivisions(){
        return DBConnection.getInstance().getDivisions();
    }

    public List<Topic> getTopics(){
        return DBConnection.getInstance().getTopics(this.topic.getDivision());
    }

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }
}

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题,我找到了一个简单的解决方案...... 而不是使用:

<f:ajax event="valueChange" render="topics" />

使用:

<p:ajax event="valueChange" update="topics" />

这解决了我2个问题:

  1. f:validateLength正常工作@ topics.xhtml
  2. FacesMessage(showMessageInDialog)正常工作@ TopicsBean.java