8 Replies Latest reply on Feb 1, 2008 7:27 AM by rhancke

    DropDownComboBox using aj4:support

    rhancke

      Hi,

      Are available for tests:
      - War project: http://www.fileupyours.com/files/158115/DropDownComboBox/richfaces.war
      - Source code: http://www.fileupyours.com/files/158115/DropDownComboBox/richfaces-src.zip

      I've created a form with two combos, where comboA loads items of comboB, using a4j:support, when an onchange event occurs.

      But, after select one option on each combo, and submit the form, a business validation error is throwed, and the values of options selected before are empty.

      If I use a4j:keepAlive, these values are kept.

      My question is: Is there another way using a4j:support to keep values of combo selected options after submit, without use a4j:keepAlive ?


        • 1. Re: DropDownComboBox using aj4:support
          ilya_shaikovsky

          if you need to avoid processing of the whole form while you send request from comboA - you should wrap it to a4j:region.

          Then it will load the values from comboB without validating anything except itself.

          • 2. Re: DropDownComboBox using aj4:support
            rhancke

            ilya_shaikovsky,

            I tried to use a4j:region following your instructions, but, without success.
            Can you give me an example with a4j:region using the source below?

            <html xmlns="http://www.w3.org/1999/xhtml"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:a4j="http://richfaces.org/a4j"
             xmlns:rich="http://richfaces.org/rich">
            
             <form jsfc="h:form">
             <table>
             <tr>
             <td>Errors: <h:messages /></td>
             </tr>
             <tr>
             <td>ComboBox A: #{dropDownComboBoxBean.comboBoxA}</td>
             </tr>
             <tr>
             <td>ComboBox B: #{dropDownComboBoxBean.comboBoxB}</td>
             </tr>
             <tr>
             <td>
             <select jsfc="h:selectOneMenu" id="comboBoxA" value="#{dropDownComboBoxBean.comboBoxA}" label="ComboBox A">
             <f:selectItems value="#{dropDownComboBoxBean.comboBoxAItems}" />
             <a4j:support event="onchange" reRender="comboBoxB"/>
             </select>
             </td>
             </tr>
             <tr>
             <td>
             <select jsfc="h:selectOneMenu" id="comboBoxB" value="#{dropDownComboBoxBean.comboBoxB}" label="ComboBox B">
             <f:selectItems value="#{dropDownComboBoxBean.comboBoxBItems}" />
             </select>
             </td>
             </tr>
             <tr>
             <td>
             <input type="submit" jsfc="h:commandButton" id="button" value="Submit" action="#{dropDownComboBoxBean.save}" />
             </td>
             </tr>
             </table>
             </form>
            </html>


            "ilya_shaikovsky" wrote:
            if you need to avoid processing of the whole form while you send request from comboA - you should wrap it to a4j:region.

            Then it will load the values from comboB without validating anything except itself.


            • 3. Re: DropDownComboBox using aj4:support
              ilya_shaikovsky

               

               <a4j:region>
               <select jsfc="h:selectOneMenu" id="comboBoxA" value="#{dropDownComboBoxBean.comboBoxA}" label="ComboBox A">
               <f:selectItems value="#{dropDownComboBoxBean.comboBoxAItems}" />
               <a4j:support event="onchange" reRender="comboBoxB" />
               </select>
               </a4j:region>
              
              
              So I see that every time list updated and select reRendered.. Am I wrong? Then describe please your steps and what you should see after each one.
              


              • 4. Re: DropDownComboBox using aj4:support
                rhancke

                ilya_shaikovsky,

                Follow all steps:

                1. Select an option on ComboBox A (ok)
                1.1 ComboBox B options are loaded

                2. Select an option on ComboBox B (ok)

                3. Click on Submit button (ok)
                3.1 Save method is called, simulating a business error.
                3.2 There is an area on form where this error message is displayed

                4. Options values of combos A and B selected before, are empty (problem)
                4.1 There is an area on form that shows selected options values (in this case, they are empty, instead of show selected values before submit).


                "ilya_shaikovsky" wrote:
                 <a4j:region>
                 <select jsfc="h:selectOneMenu" id="comboBoxA" value="#{dropDownComboBoxBean.comboBoxA}" label="ComboBox A">
                 <f:selectItems value="#{dropDownComboBoxBean.comboBoxAItems}" />
                 <a4j:support event="onchange" reRender="comboBoxB" />
                 </select>
                 </a4j:region>
                
                
                So I see that every time list updated and select reRendered.. Am I wrong? Then describe please your steps and what you should see after each one.
                


                • 5. Re: DropDownComboBox using aj4:support
                  ilya_shaikovsky

                  show me your java code please.. seems like it's wrong...

                  • 6. Re: DropDownComboBox using aj4:support
                    rhancke

                    ilya_shaikovsky,

                    If you prefer, you can download war and src projects:
                    http://www.fileupyours.com/files/158115/DropDownComboBox/richfaces.war
                    http://www.fileupyours.com/files/158115/DropDownComboBox/richfaces-src.zip

                    Follow the source-code of Managed Bean:

                    public class DropDownComboBoxBean
                    {
                     String comboBoxA, comboBoxB;
                    
                     List<SelectItem> comboBoxAItems, comboBoxBItems;
                    
                     public DropDownComboBoxBean()
                     {
                     setComboBoxAItems(loadComboBoxAItems());
                     setComboBoxBItems(new ArrayList<SelectItem>());
                     }
                    
                     private List<SelectItem> loadComboBoxAItems()
                     {
                     List<SelectItem> list = new ArrayList<SelectItem>();
                    
                     list.add(new SelectItem("0", "select ..."));
                     list.add(new SelectItem("1", "Item A1"));
                     list.add(new SelectItem("2", "Item A2"));
                     list.add(new SelectItem("3", "Item A3"));
                     list.add(new SelectItem("4", "Item A4"));
                     list.add(new SelectItem("5", "Item A5"));
                    
                     return list;
                     }
                    
                     private List<SelectItem> loadComboBoxBItems()
                     {
                     List<SelectItem> list = new ArrayList<SelectItem>();
                    
                     list.add(new SelectItem("0", "select ..."));
                     list.add(new SelectItem("1", "Item B1"));
                     list.add(new SelectItem("2", "Item B2"));
                     list.add(new SelectItem("3", "Item B3"));
                     list.add(new SelectItem("4", "Item B4"));
                     list.add(new SelectItem("5", "Item B5"));
                    
                     return list;
                     }
                    
                     public String save()
                     {
                     String message = "Business Error";
                    
                     FacesContext facesContext = FacesContext.getCurrentInstance();
                     facesContext.addMessage(null, new FacesMessage(
                     FacesMessage.SEVERITY_ERROR, message, null));
                    
                     return "failure";
                     }
                    
                     /**
                     * @return the comboBoxA
                     */
                     public String getComboBoxA()
                     {
                     return comboBoxA;
                     }
                    
                     /**
                     * @param comboBoxA the comboBoxA to set
                     */
                     public void setComboBoxA(String comboBoxA)
                     {
                     this.comboBoxA = comboBoxA;
                     }
                    
                     /**
                     * @return the comboBoxB
                     */
                     public String getComboBoxB()
                     {
                     return comboBoxB;
                     }
                    
                     /**
                     * @param comboBoxB the comboBoxB to set
                     */
                     public void setComboBoxB(String comboBoxB)
                     {
                     this.comboBoxB = comboBoxB;
                     }
                    
                     /**
                     * @param comboBoxAItems the comboBoxAItems to set
                     */
                     public void setComboBoxAItems(List<SelectItem> comboBoxAItems)
                     {
                     this.comboBoxAItems = comboBoxAItems;
                     }
                    
                     /**
                     * @param comboBoxBItems the comboBoxBItems to set
                     */
                     public void setComboBoxBItems(List<SelectItem> comboBoxBItems)
                     {
                     this.comboBoxBItems = comboBoxBItems;
                     }
                    
                     public List<SelectItem> getComboBoxAItems()
                     {
                     return this.comboBoxAItems;
                     }
                    
                     public List<SelectItem> getComboBoxBItems()
                     {
                     if (getComboBoxA() != null && getComboBoxA() != "0")
                     setComboBoxBItems(loadComboBoxBItems());
                    
                     return this.comboBoxBItems;
                     }
                    }


                    "ilya_shaikovsky" wrote:
                    show me your java code please.. seems like it's wrong...


                    • 7. Re: DropDownComboBox using aj4:support
                      ilya_shaikovsky

                      So.. All seems works just as is

                      Your buisness exception even not thrown. The problem is:

                      After you select item in fitst select - the list populated.
                      On second request (from button) - bean created again and the list is empty in created bean.

                      So not your buisness exception but standard JSF validation error is thrown, because you try to apply value that isn't from list for second box (as list is empty at this time)

                      And after the page rendered getter for empty second list called.

                      So seems the only correct way is use keepAlive (for ajax requests only if you want to live it request scoped for standard requests).

                      • 8. Re: DropDownComboBox using aj4:support
                        rhancke

                        ilya_shaikovsky,

                        First of all, thanks for your replies.

                        I'll use a4j:keepAlive to solve this case.


                        "ilya_shaikovsky" wrote:
                        So.. All seems works just as is

                        Your buisness exception even not thrown. The problem is:

                        After you select item in fitst select - the list populated.
                        On second request (from button) - bean created again and the list is empty in created bean.

                        So not your buisness exception but standard JSF validation error is thrown, because you try to apply value that isn't from list for second box (as list is empty at this time)

                        And after the page rendered getter for empty second list called.

                        So seems the only correct way is use keepAlive (for ajax requests only if you want to live it request scoped for standard requests).