开发者

Default initialization of variables in action forms

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜