在struts2中我有一些s:选择列表字段
<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=???"
首先,在服务器上调用 actionClass.getOneList() 来填充列表,第二个列表是硬编码的。
我真正想要的是第三个为页面的JSP生成列表 IN 。该列表将包含200个字符串,可以使用Java或javascript在 for 循环中轻松构建为数组。我不想用Java生成列表,并且有一个属性,getter,setter使我的动作类混乱,因为列表不是动态的,也不是由服务器上的数据定义的(如 一个 )并且我不希望使用{}获取200个项目的静态列表(例如 两个 )。< / p>
有没有办法做到这一点?可以将javascript变量用作此列表吗?可以在JSP中使用java代码来创建此列表吗?
另外,我真正可以使用的是 list 的完整描述/语法。官方struts2文档说
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.
没有显示 list 的任何语法。是否有其他文档描述了struts标记/属性,并使用 &#34;,$,#,{ 来定义此列表?
答案 0 :(得分:1)
有很多方法可以做到这一点:
由于列表是静态的,并且它永远不会更改,因此最好的选择是生成一次,然后将STATICALLY包含在代码中。好吧,迭代数字达到200并不是什么大问题,我们谈论的是纳秒,但如果你有数百万用户,那么它可能就不那么重要了。每次都这样做是非常愚蠢的,以防止在Java文件中有一个大的静态声明...把它放在一个独立的文件中,并静态使用它以避免混乱行为(这不是一个很好的理由混乱你的JSP,顺便说一下。
注意:您也可以使用带有min-max和s:set的迭代器生成此List,但是您遇到与上面相同的问题,对每个用户来说都是无用且(可能)成本高昂的操作。 / p>
修改强>:
我猜你的意思是静态&#39;就像在Java&#39;静态&#39;变量。是的我同意一个不变的列表比静态更好,但我不想在我的服务器代码中使用这个列表。在我提到的另一个半相关问题中(可能是由于结构不佳 - 我继承了这个应用程序)这个列表在一个JSP中使用,但是3个动作类/对象需要它。因此,如果列表是Java,我需要在所有这些类中使用变量/ setter / getter。这就是为什么我想在JSP中使用javascript或Java以某种方式完成它。而且我也不愿意为此定义一个超类。 - user3708842 12小时前
什么是动态&#39;方法?如何在加载时使用javascript来更改列表?那会是读取struts生成的HTML并修改它吗?我可能不会使用它,但是怎么做到的?最后将它放在一个独立的文件中,并静态使用它以避免混乱的操作&#39; - 如何避免动作类中的属性/ setter / getter? - user3708842 12小时前
我猜你的整个事情过于复杂......只需使用静态列表,故事结束。你不需要吸气剂,制定者或其他任何东西。你生成一次,并永远使用它。 0 cpu-cost。在类和JSP中都有。免费:)
您甚至不需要编写一次,使用a code like this生成它:
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);
最后为前十个X和Y数字添加前导零。
复制输出,并将这些静态内容放在某个扩展(或不扩展)的类中,在实现(或不实现)的接口中,在枚举或静态嵌套类中,无论在何处。例如。在一个界面(不应该像那样使用,但谁在乎):
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");
}
然后在Java中使用它:
public class Foo extends ActionSupport implements iMyStaticStuff{
public String execute(){
System.out.println(myStaticList);
return SUCCESS;
}
}
和JSP
<s:select list="%{@foo.bar.package.iMyStaticStuff@myStaticList}" ... />