Reputation: 563
In struts2 I have some s:select list fields
<s:select name="oneObj" id="ddlOne" list="oneList"
<s:select name="twoObj" id="ddlTwo" list="{'0A','0B','1C','1Z'}"
<s:select name="thrObj" id="ddlThr" list=???"
So for the first, actionClass.getOneList() is called on the server to populate the list, and for the second the list is hardcoded.
What I really want is for the third to generate the list IN the JSP for the page. The list will have 200 strings that can easily be built as an array in a for loop using Java or javascript. I don't want to generate the list in Java and have an attribute, getter, setter cluttering up my action class, since the list is NOT dynamic and isn't defined by data on the server (like one) and I don't want a static list using {} for 200 items (like two).
Is there any way to do this? Can a javascript variable be used as this list? Can java code be used in the JSP to create this list?
Also, what I could really use is a full description/syntax of what list can be. The official struts2 documentation says
list : `Iterable` source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.
which doesn't show any syntax of what list can be. Is there some other doc that describes struts tags/attributes and using ", $, #, { to define this list?
Upvotes: 2
Views: 3796
Reputation: 50203
There is a very high number of ways to do this:
Since the list is static, and it never changes, the best option is to generate it once, then include it STATICALLY in your code. Ok, iterating numbers up to 200 is not a big deal, we're talking about nanoseconds, but if you have million of users, it may be more than relevant. It's just stupid to do it each time to prevent having a big static declaration in a Java file... put it in a stand alone file, and use it statically to avoid cluttering the action (that is not a good reason to clutter your JSP, btw).
Note: you can probably also generate this List by using an iterator with min-max and s:set, but you've the same problems as above, an useless and (potentially) costly operation for each user.
EDIT:
I'm guessing you mean 'static' as in a Java 'static' variable. Yes I agree that an unchanging list is better as static, but I do not want this list in my server code. In another semi-related question I mentioned that (probably due to poor structure - I inherited this application) this list is used in one JSP, but 3 action classes/objects need it. So if the list is in Java, I need variables/setter/getters in all of those classes. This is why I want to do it within the JSP, using javascript or Java somehow. And I'd rather not define a super class for this either. – user3708842 12 hours ago
What are 'dynamic' methods? And how can I use javascript to alter the list when loaded? Would that be reading the struts generated HTML and modifying it? I probably won't use it, but how is that done? Lastly 'put it in a stand alone file, and use it statically to avoid cluttering the action' - how can I avoid the attribute/setter/getter in the action classes? – user3708842 12 hours ago
I guess you are overcomplicating the whole thing... just use a static List, end of story. You don't need getters, setters or anything else. You generate it once, and use it forever. At 0 cpu-cost. Both in Classes and JSPs. For free :)
You don't even need to write it once, use a code like this to generate it:
StringBuffer sb = new StringBuffer();
sb.append("public final static List<String> myStaticList = java.util.Arrays.asList(");
for (int x=0;x<100;x++){
sb.append("\""+x+"X\",");
}
for (int x=0;x<100;x++){
sb.append("\""+x+"Y\"");
sb.append(x<99 ? "," : ");");
}
System.out.println(sb);
Eventually add the leading zeros for the first ten X and Y numbers.
Copy the output, and put this static stuff somewhere, in a class you extend (or not), in an interface you implement (or not), in an enum or in a static nested class, no matter where. Eg. in an interface (that should not be used like that, but who cares):
package foo.bar.package;
interface iMyStaticStuff {
public final static List<String> myStaticList = Arrays.asList("0X","1X","2X","3X","4X","5X","6X","7X","8X","9X","10X","11X","12X","13X","14X","15X","16X","17X","18X","19X","20X","21X","22X","23X","24X","25X","26X","27X","28X","29X","30X","31X","32X","33X","34X","35X","36X","37X","38X","39X","40X","41X","42X","43X","44X","45X","46X","47X","48X","49X","50X","51X","52X","53X","54X","55X","56X","57X","58X","59X","60X","61X","62X","63X","64X","65X","66X","67X","68X","69X","70X","71X","72X","73X","74X","75X","76X","77X","78X","79X","80X","81X","82X","83X","84X","85X","86X","87X","88X","89X","90X","91X","92X","93X","94X","95X","96X","97X","98X","99X","0Y","1Y","2Y","3Y","4Y","5Y","6Y","7Y","8Y","9Y","10Y","11Y","12Y","13Y","14Y","15Y","16Y","17Y","18Y","19Y","20Y","21Y","22Y","23Y","24Y","25Y","26Y","27Y","28Y","29Y","30Y","31Y","32Y","33Y","34Y","35Y","36Y","37Y","38Y","39Y","40Y","41Y","42Y","43Y","44Y","45Y","46Y","47Y","48Y","49Y","50Y","51Y","52Y","53Y","54Y","55Y","56Y","57Y","58Y","59Y","60Y","61Y","62Y","63Y","64Y","65Y","66Y","67Y","68Y","69Y","70Y","71Y","72Y","73Y","74Y","75Y","76Y","77Y","78Y","79Y","80Y","81Y","82Y","83Y","84Y","85Y","86Y","87Y","88Y","89Y","90Y","91Y","92Y","93Y","94Y","95Y","96Y","97Y","98Y","99Y");
}
Then use it in Java:
public class Foo extends ActionSupport implements iMyStaticStuff{
public String execute(){
System.out.println(myStaticList);
return SUCCESS;
}
}
and in JSP
<s:select list="%{@foo.bar.package.iMyStaticStuff@myStaticList}" ... />
Upvotes: 1