views:

33

answers:

1

Hi Guys, i have made a from like :

MyForm extend ActionForm{

  list<Menu> MenuList=null;

  MyForm(){

    super();

    setMenuList(); //initialize menu list

  }

}

Menu object has string desciption and boolean variable for selected or not.

In web page, i am iterating the list to display all the menus and checkbox over those boolean variables. But the list is set to default every time i want to access the selected items. Is that because of constructor, i have defined ????? Please help me Guys??

Is there any other way to initialize the variables, i had also tried to initialize the list on its getter function, but its giving me a null pointer exception. I was not even able to understand why is this so.

+1  A: 

Check out the Prepare interceptor. It allows you to automatically invoke a prepare method when you call an action, before any other Action logic occurs:

public class MyForm extend ActionForm implement Preparable {

  list<Menu> MenuList=null;

  prepareView(){
      // initialize your menu list
      List yourMenu = new ArrayList();
      yourMenu.add("foo");
      yourMenu.add("bar");
      setMenuList(yourMenu);
  }

  view(){
      return INPUT;
  }
}

In the above example, when you call MyForm's view() method, prepareView() will be invoked first, setting up your menu and ready for you to use on your INPUT.

You might also want to consider adding yourMenu to the session for the duration of your Action so that it's still available to you if you encounter validation errors.

Pat
Thanks Pat, its really a very useful information.
rahul jain