- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 struts2 中我有一些 s:select 列表字段
<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() 来填充列表,而对于第二个,列表是硬编码的。
我真正想要的是第三个生成列表 IN 页面的 JSP。该列表将包含 200 个字符串,可以使用 Java 或 javascript 在 for 循环中轻松构建为数组。我不想用 Java 生成列表并且有一个属性、getter、setter 弄乱了我的操作类,因为列表不是动态的并且不是由服务器上的数据定义的(比如 one ),我不希望使用 {} 的静态列表包含 200 个项目(例如两个)。
有什么办法吗?可以将 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 标签/属性并使用 ", $, #, { 来定义此列表?
最佳答案
有很多方法可以做到这一点:
由于列表是静态的,它永远不会改变,最好的选择是生成一次,然后将其静态地包含在您的代码中。好吧,将数字迭代到 200 没什么大不了的,我们谈论的是纳秒级,但如果你有数百万用户,它可能就更重要了。每次都这样做以防止在 Java 文件中有一个大的静态声明是愚蠢的......把它放在一个独立的文件中,并静态地使用它以避免使操作困惑(这不是使你的 JSP 困惑的好理由,顺便说一句)。
注意:您可能也可以通过使用带有 min-max 和 s:set 的迭代器来生成此列表,但是您会遇到与上述相同的问题,即对每个用户来说都是无用且(可能)代价高昂的操作。
编辑:
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
我猜你把整个事情搞得太复杂了……只使用静态列表,故事结束。您不需要 getter、setter 或其他任何东西。您生成它一次,并永远使用它。 CPU 成本为 0。在类和 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 数字添加前导零。
复制输出,并将这些静态内容放在某个地方,在您扩展(或不扩展)的类中,在您实现(或不实现)的接口(interface)中,在枚举中或静态嵌套类中,无论在哪里。例如。在界面中(不应该那样使用,但谁在乎):
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}" ... />
关于java - 我怎样才能建立struts2 s :select list in JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24557138/
是否可以让标准 Java EE servlet 容器将文件解释并呈现为 JSP,即使该文件没有 .jsp 扩展名? 假设我的 WAR 根目录中有一个名为 foo.xyz 的文件。该文件包含一些 jST
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我有几个通用元素(组件),它们会生成一些 html。似乎我的选择是创建一个 taglib,或者只是将该逻辑放入一个 jsp 页面并包含 jsp。 有什么不同?积极与消极? 最佳答案 Taglibs 允
我是 Ejb-JSP 的新手,对它知之甚少。我已经创建了一个 JSP 页面,它调用 Controller Servlet,而 servlet 调用 EJB。结构就像 JSP -> Servlet ->
我想编写一个自定义 JSP 标签,其输出包括其他 JSP 标签,这些标签本身也应该被动态评估。但显然我的一切TagSupport子类写入 pageContext.getOut()无需任何进一步评估,直
我有一个包含页面顶部内容的 JSP,我们称它为 header.jsp。出于性能原因,我想呈现此 JSP 并将其刷新给用户,然后再构建页面的其余部分。 (有关性能优势的解释,请参阅 here。) 我能想
我发现自己在处理一些旧的 JSP,想做一些简单的事情,比如以 dd/mm/yyyy 格式显示今天的日期 好像没那么简单, 到目前为止,我已经导入了 java.util.* 我试过很多东西 String
关于 JSP 标签字符集的简单问题。 mytag很简单。 tag文件位于 WEB-INF/tags .这个文件在 Eclipse 中的字符集是 UTF-8。出于某种原因,UTF-8 符号无法
这让我很吃惊!我的 JSP 中有以下代码。 在我重构 SlideShow 类并公开所有属性并删除 getter/setter 之前,这段代码一直有效。所以在我看来,EL 只适用于 getter 而不
现有的一组 JSP,用英文字段标签、javascript 错误/信息消息、图像“alt”消息等硬编码,需要多语言化。 将这些 JSP 转换为使用标准 Java 多语言功能(资源包、语言环境等)的最佳方
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题
我已经在 Tomcat 6.0 下部署了我们的 War 到 Linux 服务器。在那个 Linux 机器上,我们没有打开浏览器的权限。 是否可以从命令行执行 JSP? 最佳答案 您可以使用其中 wge
有没有人建议为 JSP 设置最佳缓冲区大小?您可以使用以下页面指令在 JSP 中设置缓冲区大小 我的问题如下 我的理解是,您使用的缓冲区大小越小,客户端浏览器的性能就越好。我的假设正确吗?如
我们正在使用 JBoss 7.1.3.Final 和 Java 6。我想将 UTF-8 页面编码应用于我们网站上提供的所有 JSP 页面,因此我将其添加到我们的 web.xml 文件中
学过jsp native,想包含动态文件。我想使用 include 调用动态页面 这段代码 如果我输入 dashboard.jsp?p=about 页面打开“pages/a
在我的 JSP 页面中,我希望链接转发到另一个 JSP 页面。当用户在 home.jsp 上时,我希望他们转到 login.jsp 等。我遇到的问题是无法找到 JSP,除非我将页面放在项目文件夹中(在
我正在尝试在新的grails应用程序中使用index.jsp切换index.gsp。我将默认的index.gsp重命名为not_index.gsp,并添加了index.jsp。现在,我收到以下错误。
是否可以从服务器端 jsf 代码将资源打开到新的浏览器选项卡(如命令按钮的 target="_newtab")? 以下代码在同一选项卡中打开资源: FacesContext.getCurrentIns
我想问一个关于 .jsp 的问题。使用 jsp 语法(例如 )和 XML 语法(例如 ... )有什么不同。使用其中一种语法是否有维护或某种 advs?谢谢。 最佳答案 原始的 语法更加紧凑,但如
JSP 文件是否有行业标准命名约定? 我遇到过来自不同来源的三种约定: 全部小写 (thisismyfile.jsp) 首字母小写的驼峰式大小写 (thisIsMyFile.jsp) 首字母大写的驼峰
我是一名优秀的程序员,十分优秀!