2016-11-21 2 views
0

J'ai un bean géré, qui est ViewScoped. Dans ce bean j'ai besoin d'envoyer un formulaire (en utilisant h: commandButton). Cela fonctionne très bien, sauf lorsque je change mon entrée dans le menu déroulant (qui déclenche un événement et met à jour la page). Après avoir modifié la valeur du menu déroulant, envoyer le formulaire recréer le bean (et ignorer l'action associée à h: commandButton).Bean ViewScoped recréé au lieu d'appeler l'action du formulaire

Voici mon XML:

<rich:panel styleClass="panel_grid_center fifty_percent" 
     header="Bid matrix"> 

     <!-- display in case the user is not an admin --> 
     <h:panelGroup rendered="#{not loginBean.isAdmin}"> 

      <h:outputText 
       value="You do not have sufficient permission to view this page." /> 
      <br /> 
      <h:form> 
       <h:commandLink action="index.xhtml" 
        value="Click here to go back to login page/search page." /> 
      </h:form> 
     </h:panelGroup> 

     <!-- display if the user is an admin --> 
     <h:panelGroup rendered="#{loginBean.isAdmin}" id="bid_matrices_panel"> 
       <h:panelGrid columns="2"> 

        <!-- customer group panel --> 
        <rich:panel styleClass="contained_width fifty_percent" 
         header="Customer group"> 
         <h:form> 
          <h:selectOneMenu 
           valueChangeListener="#{adminBean.onCustomerGroupChangeListener}" 
           value="#{adminBean.customerGroupService.displayCustomerGroup.spendMinimum}"> 
           <f:selectItems 
            value="#{adminBean.customerGroupService.customerGroups}" 
            var="group" itemLabel="#{group.customerGroupLabel}" 
            itemValue="#{group.spendMinimum}" /> 
           <a4j:ajax event="valueChange" execute="@this" 
            render="bid_matrices_panel" /> 
          </h:selectOneMenu> 
         </h:form> 
        </rich:panel> 

        <!-- repeatables --> 
        <rich:panel styleClass="contained_width fifty_percent" 
         header="Repeatables"> 
        </rich:panel> 
       </h:panelGrid> 

       <h:form> 
        <!-- we loop on each different commoditization (or however that's spelled) --> 
        <a4j:repeat var="bidmatrix_by_commoditization" 
         value="#{adminBean.bidMatrices}"> 
         <rich:dataTable styleClass="contained_width" 
          value="#{bidmatrix_by_commoditization.bidMatricesByCoreStatus}" 
          var="matrix_by_core_status"> 


          <!-- Display core status --> 
          <rich:column> 
           <f:facet name="header"> 
            <h:outputText 
             value="#{bidmatrix_by_commoditization.commoditization}" /> 
           </f:facet> 
           <h:outputText value="#{matrix_by_core_status.coreStatus}" /> 
          </rich:column> 

          <!-- the percentages --> 
          <c:forEach var="index" begin="0" 
           end="#{adminBean.columnsNumber - 1}"> 
           <rich:column> 
            <f:facet name="header"> 
             <h:outputText value="#{adminBean.columnsHeaders[index]}" /> 
            </f:facet> 
            <h:inputText 
             value="#{matrix_by_core_status.bidMatrices[index].percentage}"> 
             <f:convertNumber type="percent" /> 
            </h:inputText> 
           </rich:column> 
          </c:forEach> 
         </rich:dataTable> 
        </a4j:repeat> 

        <br /> 

        <!-- update matrix button --> 
        <h:commandButton value="Update" action="#{adminBean.update}" /> 
       </h:form> 
     </h:panelGroup> 
    </rich:panel> 

Mon haricot:

@ManagedBean(name = "adminBean") 
@ViewScoped 
public class AdminBean implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 5917562235108703019L; 

    private CustomerGroupService customerGroupService; 
    private BidMatrixDao bidMatrixDao; 
    private List<BidMatricesByCommoditization> bidMatrices; 

    @PostConstruct 
    public void init() { 
     customerGroupService = new CustomerGroupService(); 
     bidMatrixDao = new BidMatrixDaoImpl(); 
     bidMatrices = bidMatrixDao.getBidMatricesByCustomerGroup(customerGroupService.getDisplayCustomerGroup()); 
    } 

    public void onCustomerGroupChangeListener(ValueChangeEvent v) { 
     customerGroupService.setDisplayCustomerGroup((BigDecimal) v.getNewValue()); 
     bidMatrices = bidMatrixDao.getBidMatricesByCustomerGroup(customerGroupService.getDisplayCustomerGroup()); 
    } 

    public CustomerGroupService getCustomerGroupService() { 
     return customerGroupService; 
    } 

    /** 
    * @param customerGroupService 
    *   the customerGroupService to set 
    */ 
    public void setCustomerGroupService(CustomerGroupService customerGroupService) { 
     this.customerGroupService = customerGroupService; 
    } 

    /** 
    * @return the bidMatrices 
    */ 
    public List<BidMatricesByCommoditization> getBidMatrices() { 
     return bidMatrices; 
    } 

    /** 
    * @param bidMatrices 
    *   the bidMatrices to set 
    */ 
    public void setBidMatrices(List<BidMatricesByCommoditization> bidMatrices) { 
     this.bidMatrices = bidMatrices; 
    } 

    public int getColumnsNumber() { 
     return bidMatrices.get(0).getColumns(); 
    } 

    public List<String> getColumnsHeaders() { 
     return bidMatrixDao.getAlignments(); 
    } 

    public void update() { 
     bidMatrixDao.updateBidMatrices(bidMatrices); 
    } 
} 

Notez que j'importer correctement ViewScoped de javax.faces.bean.ViewScoped; J'ai aussi enlevé les getters/setters de mon haricot, mais ils sont là. Comme je l'ai dit, le formulaire fonctionne correctement lorsqu'il est envoyé sans modifier la valeur de h: selectOneMenu.

Merci!

Edit: J'utilise JSF 2.2 (mojarra), richfaces 4.1, avec wildfly 10,1

+0

impl JSF et la version? Version Richfaces? – Kukeltje

+0

JSF 2.2 est une spécification d'API, pas une implémentation et une version JSF. Wildfly 10.1 utilise? – Kukeltje

Répondre

0

Je fixe en déplaçant l'identifiant utilisé dans ma directive ajax:

<a4j:ajax event="valueChange" execute="@this" render="bid_matrices_panel" /> 

Au départ, je mis la id dans la panelGroup mère, comme ceci:

<h:panelGroup rendered="#{loginBean.isAdmin}" id="bid_matrices_panel"> 

en créant un riche: panneau autour de la partie que je veux vraiment rerender, mon problème est résolu:

<h:form> 
    <rich:panel id="bid_matrices_panel"> 
     <! -- my data to render --> 

    </rich:panel> 

    <h:commandButton value="Update" action="#{adminBean.update}" /> 
</h:form> 

Je suppose que le rendu du formulaire était la source du problème.

assez sûr que j'essayé avant, mais à ce moment-là, je l'avais aussi oublié de implements Serializable sur mon AdminBean ...