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.

-------------Problems Reply------------

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;

// initialize your menu list
List yourMenu = new ArrayList();

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.

Category:javascript Views:0 Time:2010-07-31

Related post

  • How to default-initialize local variables of built-in types in C++? 2010-04-06

    How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef: typedef unsigned char boolean;//that's Microsoft RPC runtime typedef I'd like to change the following line: boolean variable = 0; //initialize to

  • Auto generate constructor with default initialization of variables 2011-11-19

    This might be a super-lame lazy question. Is it possible to generate constructor in Eclipse like class A{ int a; String b; float c; /* Is it possible to auto-generate this*/ public A() { a = 0; b = ""; c = 0f; } } I am aware that default initializati

  • How to prevent default initialization of a const variable with a class type 2011-11-18

    I have a custom class that I want to behave like a built-in type. However I have noticed that you can initialise a const variable of that class without providing an initial value. My class currently has an empty default constructor. Here is a compari

  • Struct with boolean field default initialization? 2009-01-05

    I have the following use case, a struct with some boolean and int variables struct a { int field1; bool field2; bool field3; }; I am refactoring this code, and writing a constructor for the struct , the problem is the default initialization of fields

  • C: Initialize module variable 2009-11-25

    I got two modules (compile units), both using a module variable with the same name: FileA.c and FileB.c both contain: #includes int m_Test; // Functions That's no problem, both variables are independent, as expected - but as soon as I assign values t

  • default initialization in C++ 2010-02-08

    I have a question about the default initialization in C++. I was told the non-POD object will be initialized automatically. But I am confused by the code below. Why when I use a pointer, the variable i is initialized to 0, however, when I declare a l

  • - Default value of variables at the time of declaration - 2010-04-14

    I was wondering what was the default values of variables before I intialize them .. For example, if I do : //myClass.h BOOL myBOOL; // default value ? NSArray *myArray; // default value ? NSUInteger myInteger; // default value ? Some more examples he

  • Is it bad practice to initialize a variable to a dummy value? 2010-05-05

    This question is a result of the answers to this question that I just asked. It was claimed that this code is "ugly" because it initializes a variable to a value that will never be read: String tempName = null; try{ tempName = buildFileName(); } catc

  • What is the default initialization of an array in Java? 2010-08-06

    So I'm declaring and initializing an int array: static final int UN = 0; int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = UN; } Say I do this instead... int[] arr = new int[5]; System.out.println(arr[0]); ... 0 will print to st

  • Why can't I initialize readonly variables in a initializer? 2010-12-16

    Why can't I initialize readonly variables in a initializer? The following doesn't work as it should: class Foo { public readonly int bar; } new Foo { bar=0; }; // does not work Is this due to some technical limits of the CLR? EDIT I know that new Foo

  • What is the better approach to initialize class variables? 2011-03-16

    Here are two way to initialize class variables. 1st Method class Test { private $var1; private $var2; public function Test($var1,$var1) { $this->var1 = $var1; $this->var2 = $var2; } } $objTest = new Test("value1","value2"); 2nd Method class Tes

  • Difference between initializer and default initializer list in c++ 2011-04-28

    This question already has an answer here: Initializing fields in constructor - initializer list vs constructor body [duplicate] 6 answers HI, I have a question but cannot find the answer for a long time, that is, what's difference between the followi

  • Default initialization of POD vs. non-POD class types 2011-11-24

    The C++ standard says (8.5/5): To default-initialize an object of type T means: If T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor). If T i

  • Different ways to initialize a variable 2011-12-18

    As far as I've seen there are two ways to initialize a variable with the output of a process. Is there any difference between these two? ex1=`echo 'hello world'` ex2=$(echo 'hello world') --------------Solutions------------- You get same effect. The

  • Default initializer of C and C++ languages of boolean types on GCC compiler is 64 instead of 0. It's compiler bug? 2012-04-25

    I'm studying D language and simultaneously doing comparison to C and C++ languages.It works fine both dmd and gdc compilers,but when I tested on gcc compiler,I found a thing that looks like bug of GCC compiler,the default initializer of boolean type

  • How to pass variable from action to form 2013-06-24

    I am sure I am going about this the wrong way, but I need to unset an array key from one of my choices in a sfWidgetFormChoice. The only way to get that variable to the Form is from the action. Here's what I have: Action: $id = $request->getParame

  • Java code conventions: Using 'default' as a variable name 2010-06-23

    I would like to use 'default' as a variable name. Is there a code convention (like class -> clazz) that suggests how I should name the variable? --------------Solutions------------- I usually add a term that indicates for what it is the default. S

  • How to initialize several variables in a for (;;) loop in C? 2010-07-27

    I thought one could initialize several variables in a for loop: for (int i = 0, char* ptr = bam; i < 10; i++) { ... } But I just found out that this is not possible, gcc gives the following error: error: expected unqualified-id before 'char' Is it

  • Why should I use constructor if I can initialize instance variable another way 2010-10-13

    Why should I use constructor to initialize instance variables while it is possible at the point of their declaration? class Frog { public int x = 4; Frog() { // Why should I use you } } --------------Solutions------------- You should use me because I

Copyright (C), All Rights Reserved.

processed in 0.116 (s). 11 q(s)