Java jsp 设置变量变量名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24081989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsp set variable variable name
提问by TheOneWhoPrograms
I have been looking how to do this properly but cannot find a definitive guide on how to do with.
我一直在寻找如何正确执行此操作,但找不到有关如何处理的明确指南。
I know that you cannot use an expression within the tag, but I am unsure how else I am meant to approach it.
我知道您不能在标签中使用表达式,但我不确定我打算如何处理它。
I've seen a multitude of answers for this without much explanation or help.
我已经看到了很多答案,但没有太多解释或帮助。
Essentially I want something like the following to work, but obviously doesn't.
基本上我想要像下面这样的东西,但显然没有。
<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
<c:set var="name_${status.count}" value="${lineposition.value}" scope="session"/>
</c:forEach>
The exact error message is
确切的错误消息是
"According to TLD or attribute durective in tag file, attribute var does not accept any expressions"
“根据标记文件中的 TLD 或属性 durective,属性 var 不接受任何表达式”
What is the proper way to accomplish this?
完成此操作的正确方法是什么?
(I changed the variable names from my actual code, but hopefully you still get the idea)
(我从我的实际代码中更改了变量名称,但希望您仍然明白这一点)
If I need to create java objects or something I am fine with that, but I would need to know how to include them in my project and how to use them within the code. Something like a list sounds about right.
如果我需要创建 java 对象或其他东西,我可以接受,但我需要知道如何将它们包含在我的项目中以及如何在代码中使用它们。列表之类的东西听起来是对的。
I have created an object to hold my values for me.
我创建了一个对象来为我保存我的价值观。
<jsp:useBean id="myid" class="myclass" scope="session"/>
and I want to use it, but am unsure how:
我想使用它,但不确定如何使用:
<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
<%
myid.add_position(lineposition.var1, lineposition.var2, lineposition.var3, lineposition.var4, lineposition.var5);
%>
</c:forEach>
采纳答案by Erik Gillespie
Using an MVC framework to separate the code to setup your data from how you want to present it would be the ideal way to go. If you have a controller of any kind that executes on the server before your JSP is rendered then I would recommend putting your logic there. Besides the controller being the more appropriate place to prepare this sort of data, the syntax will almost definitely look more clean than anything you could put on the JSP to make this work.
使用 MVC 框架将用于设置数据的代码与您希望如何呈现数据分开将是理想的方式。如果您有任何类型的控制器在呈现 JSP 之前在服务器上执行,那么我建议将您的逻辑放在那里。除了控制器是准备此类数据的更合适的位置之外,语法几乎肯定看起来比您可以放在 JSP 上以使其工作的任何东西都更干净。
If you don't have a controller and you're only using JSPs then I guess I would recommend writing a tag to replace <c:set>. This is not the cleanest approach but if JSPs are what you're stuck with then it seems like a decent compromise to me (I think it's better than scriptlets and hacking the JSTL core TLD to allow expressions in the "var" attribute anyway). It would do all of the same things as <c:set> except the TLD could be written to allow expressions in the "var" attribute.
如果您没有控制器并且您只使用 JSP,那么我想我会建议您编写一个标签来替换 <c:set>。这不是最干净的方法,但如果 JSP 是你所坚持的,那么对我来说这似乎是一个不错的妥协(我认为它比 scriptlet 更好,并破解 JSTL 核心 TLD 以允许“var”属性中的表达式无论如何)。除了可以编写 TLD 以允许“var”属性中的表达式之外,它会执行与 <c:set> 相同的所有操作。
MyTag.java
我的标签
package example;
import org.apache.taglibs.standard.tag.rt.core.SetTag;
public class MySetTag extends SetTag { }
WEB-INF/my.tld
WEB-INF/my.tld
<taglib>
<tag>
<name>set</name>
<tag-class>example.MySetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
...
</tag>
</taglib>
rownamer.jsp
行名.jsp
<%@ taglib prefix="my" uri="WEB-INF/my.tld"%>
<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
<my:set var="name_${status.count}" value="${lineposition.value}" scope="session"/>
</c:forEach>