myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gerald Müllan" <bierbra...@gmail.com>
Subject Re: selectOneMenu and selectItem sample
Date Thu, 21 Sep 2006 17:11:29 GMT
Do this:

public class TableForm {

    private List<SelectItem> tables = null;
    private String selectedTable = null;

    public String getSelectedTable() {
        return selectedTable;
    }

    public void setSelectedTable(String selectedTable) {
        this.selectedTable = selectedTable;
    }

    public List<SelectItem> getTables() {
       if(tables == null){
                  tables =  new ArrayList<SelectItem>();
                  tables.add(new SelectItem("1", "Table 1"));
                  tables.add(new SelectItem("2", "Table 2"));
        }
        return tables;
    }

    public String pagetoDisplay() {

        if (selectedTable.equals("1")) {
            System.out.println("Inside 1");
            return "displaytable1";
        }
        else if (selectedTable.equals("2")) {
            return "displaytable2";
        }
        else return null;
    }
}

and the jsp:

<h:selectOneMenu required="true" value=#{tableform.selectedTable}>
         <f:selectItems value="#{tableform.tables}"/>
    </h:selectOneMenu>
     <h:commandButton value="Submit"  action="#{tableform.pagetoDisplay }"/>

This should work now,

cheers,

Gerald

On 9/21/06, Aneesha Govil <popcorn09@gmail.com> wrote:
>
>
> On 9/21/06, Gerald Müllan <bierbrauen@gmail.com> wrote:
> > You don`t need the immediate attribute in this simple usecase.
> Removed that. Changed the JSP selectOneMenu to the following:
> <h:selectOneMenu value="1">
> Also tried with:
> <h:selectOneMenu value=" tableform.selectedTable">
>
> Modified the getSelectedItem to as follows:
>
> public SelectItem getSelectedTable() {
>         selectedTable = new SelectItem("1", "Table 1");
>         return selectedTable;
>     }
>
> > Just
> > declare the value attribute of the selectOneMenu in the bean and give
> > it the type corresponding to the "value"
> > type of the SelectItem. In your case is this a String.
> >
> > private String selectedTable = null;
>
> I have declared this as a SelectItem, any reason why that should not work?
>
> > You have also permuted the "label" and the "value" attribute when
> > creating the SelectItem.
> > The fist argument is the value (hidden on the page, this is the value
> > which is set to the value of the selectOneMenu) and the second one the
> > label (can be seen in the drop down). And in your getPagetoDisplay()
> method, place the condition of which
> > value the selectedTable is.
> >
>
> Yes, I figured that, when one wasn't working I tried the other and forgot to
> change it back. I have changed it back to
> use getValue instead of getLabel.
>
> >
> > Any further help needed?
>
> Still getting the null pointer exception in the getPagetoDisplay method :(
>  Thanks for trying to help.
> Regards,
> Aneesha
>


-- 
http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces

Mime
View raw message