2012-12-10 110 views
0

我有简单的JSF页面和简单的支持bean。我在页面上有p:commandButton和p:datatable。用commandButton我需要用ajax更新dataTable,但它不会发生。可能是什么原因?PrimeFaces commandButton ajax不起作用

这里是页:

<ui:define name="content"> 
     <h2>#{menu['management.roles.perms']}</h2> 

     <h:form id="form"> 
      <p:fieldset style="border: 0px" toggleable="false" collapsed="false"> 
       <p:tabView> 
        <p:tab title="#{messages['roles']}"> 
         <p:dataTable id="dataTable" value="#{rolesBean.roles}" var="role" 
             editable="true" rows="10" rowKey="#{role.id}"> 

          <p:ajax event="rowEdit" listener="#{rolesBean.onEdit}"/> 
          <f:facet name="footer"> 
           <p:commandButton value="#{misc['add.role']}" 
                actionListener="#{rolesBean.addRole}" update="dataTable"/> 
          </f:facet> 
          <p:column headerText="#{misc['name.database']}"> 
           <p:cellEditor> 
            <f:facet name="output"> 
             <h:outputText value="#{role.name}"/> 
            </f:facet> 
            <f:facet name="input"> 
             <h:inputText value="#{role.name}"/> 
            </f:facet> 
           </p:cellEditor> 
          </p:column> 

          <p:column headerText="#{misc['date']}"> 
           <f:facet name="output"> 
            <h:outputText value="#{role.creationDate}"> 
             <f:convertDateTime pattern="dd.MM.yyyy" /> 
            </h:outputText> 
           </f:facet> 
          </p:column> 

          <p:column headerText="#{misc['description']}"> 
           <p:cellEditor> 
            <f:facet name="output"> 
             <h:outputText value="#{role.description}"/> 
            </f:facet> 
            <f:facet name="input"> 
             <h:inputText value="#{role.description}"/> 
            </f:facet> 
           </p:cellEditor> 
          </p:column> 

          <p:column headerText="#{messages['edit']}"> 
           <p:rowEditor /> 
          </p:column> 
         </p:dataTable> 
        </p:tab> 

        <p:tab title="#{messages['perms']}"> 

        </p:tab> 

        <p:tab title="#{clients['history']}"> 
        </p:tab> 
       </p:tabView> 
      </p:fieldset> 
     </h:form> 
    </ui:define> 

这里是backingBean的代码:

@ViewScoped 
@ManagedBean(name = "rolesBean") 
public class RolesViewBean extends BasicViewBean<Role> { 

    private static final long serialVersionUID = 27377603187254821L; 
    @Inject 
    RoleDao dao; 
    private List<Role> roles; 

    @Override 
    protected BasicDao<Role> getDAO() { 
     return dao; 
    } 

    public void onEdit(RowEditEvent event){ 
     this.entity = (Role) event.getObject(); 
     dao.update(this.entity); 
    } 

    public void addRole(){ 
     roles.add(new Role()); 
    } 

    public List<Role> getRoles() { 
     roles = dao.getRoleList(); 
     return roles; 
    } 

    public void setRoles(List<Role> roles) { 
     this.roles = roles; 
    } 
} 

回答

0

乍看之下,问题是,你的addRole方法modifes的 “角色” 列表中,而getRoles(它被dataTable调用)将其重置为数据库中保存的任何内容。 因此,您的数据库得到更新,它只是用相同的值更新。

想想那个;)

+0

你是对的! tnx求助,但现在还有另一个问题。按下按钮时,addRole方法不执行。它可能是什么? –