It is a quite common that you come across a screen whose common requirement is to layout a part or whole of the screen dynamically based on the runtime decisions made by the user. A typical example is of screen used for submission of report process. Screen for such a functionality is made of two blocks, in the first block user inputs report process name and presses a “Search” button on the basis of report name specified a list of arguments appears in the lower block, against which the user is supposed to provide values.
In general against every field in JSP,
bean has to have a pair of accessor and mutator methods (getter and setter methods). In this scenario since the number of fields varies there could be a problem if some fields does not have a corresponding accessor and mutator method. Best practices There are three possible solutions to this problem
- Name all the fields in specific pattern like field1, field2, field3 etc and provide their accessor and mutator methods in the FormBean. In this approach there is a maximum limit on number of fields that can appear on screen.
- The purpose of is to reduce the overhead of doing getParameter () in the Servlet. We always have the option of not using a and doing the getParameter in Action class.
- Third option is to utilize the setProperty ()/getProperty () methods provided by Struts especially for this type of problem.
In case we have a small and fixed number of fields on screen, it is not a bad idea to go for the first approach described above, but in general it has been found that the last approach is the best one.
To use this, define two methods in your
bean class. These methods may in turn be written to store the values into a
. Populate all the required fields in the form Action Class or any Helper class.
Then use these field and respective values in your JSP using the following tag.
There's a fourth, and, IMHO, more elegant option available based on the JavaBeans standard: Use indexed properties.
In your HTML, you can use:
If you use dyna forms, you specify the type as an implementation class of a List (e.g. java.util.ArrayList).
If you are using hand coded forms, just use accessor and mutator methods like:
One Gotcha: These are naive implementations that don't check for index out of bounds; production code must be more robust.
Delve a bit more into the API and you'll find that Struts builds on this concept to allow you to use mapped properties as well.
– Java Architect
Also see: StrutsCatalogInstrumentableForms – Michael McGrady