JSF DataTable命令按钮不起作用

时间:2012-09-08 19:50:33

标签: jsf jsf-2 datatable primefaces managed-bean

我使用的是primefaces 3.3,页面上有3个组件:数据表,编辑区,命令按钮。我有一个奇怪的问题:当我打开页面时,数据显示在数据表中,但如果我点击任何一个按钮,调试器会建议甚至不调用相应的按钮代码。但是,如果我点击表格中的任何记录,则会填充编辑区域,如果我点击按钮,他们就会开始工作。 下面是页面代码,我尝试了不同的选项,将所有内容放在1个表单等,没有用。请帮忙

    <ui:define name="content">
        <h:form id="form" prependId="false">
            <p:growl id="growl" showDetail="true"/>  
            <p:dataTable id="table" var="var" 
                         value="#{regions.list}" 
                         selection="#{regions.selected}"  
                         rowKey="#{var.idRegion}"
                         selectionMode="single" 
                         paginator="true" rows="10" 
                         paginatorPosition="bottom"                                  
                         >                                
                <p:ajax event="rowSelect" listener="#{regions.onRowSelect}" update=":frmEdit :form:growl"  />  
                <p:ajax event="rowUnselect" listener="#{regions.onRowUnSelect}"  update=":frmEdit :form:growl"/>  

                <f:facet name="header">  
                    List of Regions  
                </f:facet>  
                <p:column sortBy ="#{var.shortName}">
                    <f:facet name="header">Short Name</f:facet> 
                    <h:outputText value="#{var.shortName}" />
                </p:column>
                <p:column sortBy ="#{var.fullName}">
                    <f:facet name="header">Full Name</f:facet>
                    <h:outputText value="#{var.fullName}" />
                </p:column>
                <p:column sortBy ="#{var.created}">
                    <f:facet name="header">Created On</f:facet>
                    <h:outputText value="#{var.created}" />
                </p:column>
            </p:dataTable>
                            </h:form>
            <p:panel header="Add/Edit Properties">
        <h:form id="frmEdit">
                <h:panelGrid id="display" columns="6" cellpadding="4" >

                    <h:outputText value="Short Name:" />
                    <h:inputText value="#{regions.selected.shortName}" />

                    <h:outputText value="Full Name:" />
                    <h:inputText value="#{regions.selected.fullName}" />

                </h:panelGrid>
        </h:form>
        <h:form id="frmBtn">
                <h:panelGrid id="command" columns="6"  cellpadding="4" >
                    <p:commandButton id="new" value="New" actionListener="#{regions.add()}"  process="@this :frmEdit"  update="result :frmEdit :form" >                                
                    </p:commandButton>
                    <p:commandButton id="save" value="Save" actionListener="#{regions.save()}" process="@this :frmEdit"  update="result :frmEdit :form" >                                
                    </p:commandButton>
                    <p:commandButton id="delete"  value="Delete" actionListener="#{regions.delete()}"  process="@this :frmEdit"  update="result :frmEdit :form" >                                
                    </p:commandButton>
                    <p:commandButton id="refresh"  value="Refresh" actionListener="#{regions.refresh()}"  process="@this :frmEdit"  update="result :frmEdit :form" >                                
                    </p:commandButton>
                </h:panelGrid>

                <p:messages id="result" showDetail="true" autoUpdate="true"/>  
        </h:form>
            </p:panel>
    </ui:define>

下面是JSF MB代码:

@ManagedBean
@ViewScoped
public class Regions implements Serializable {
    private List<Region> list;
    private Region selected;
    private @EJB RegionFacade esvc;  //entity services    
    private FacesContext context;
    private FacesMessage msg;
    private boolean newRec =false;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public Regions() {
//        context = FacesContext.getCurrentInstance();
//        HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
//        Users user = (Users)session.getAttribute("user");
//        if (user != null)
//            init();
//        else {
//            msg = new FacesMessage("Session Expired, Return to Login Page");
//            context.addMessage(null, msg);
//        }
    }

    @PostConstruct
    public void init(){        
        try{
            list = esvc.findAll();
        }catch (Exception ex){
            System.out.println(ex);
        }
    }

    public String add(){
        selected = new Region();          
        newRec = true;
        return "region";
    }

    public String save(){
        int result;
        if (newRec){
            result=esvc.insert(selected);
            newRec=false;
        }
        else{       
            result=esvc.update(selected);
        }
        refresh();
        context = FacesContext.getCurrentInstance();
        if (result>0){
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Success", "Record Saved");
            refresh();
        }else{
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Failure", "Record not Saved");            
        }
        context.addMessage(null, msg);
        return "region";
    }

    public String delete(){
        int result;
        result= esvc.delete(selected);
        context = FacesContext.getCurrentInstance();
        refresh();
        if (result>0){
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Success", "Record Deleted");            
            refresh();
        }else {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Failure", "Record not Deleted");            
        }
        context.addMessage(null, msg);
        return "region";
    }

    public void refresh(){
        selected=null;
        list=getList();         
    }

    public List<Region> getList() {
        return list;
    }

    public Region getSelected() {
        return selected;
    }

    public void setSelected(Region selected) {
        this.selected = selected;
    }

    public void onRowSelect(SelectEvent event) {
        context = FacesContext.getCurrentInstance();
        msg = new FacesMessage("Selected", ((Region) event.getObject()).getFullName());
        context.addMessage(null, msg);
    }


    public void onRowUnSelect(UnselectEvent event) {
        context = FacesContext.getCurrentInstance();
        selected=null;
    }    

}

1 个答案:

答案 0 :(得分:0)

ActionListener方法(要在actionListener属性上使用)必须采用ActionEvent参数和void返回类型。如果要保留方法签名,请删除命令按钮上的actionListener属性并使用action属性。但是,如果您需要立即处理事件,则会在按钮上触发事件,您必须更改方法签名以实现ActionListener interface