java 将动态内容传递给 jsp:param "value" 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15064512/
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
Passing dynamic content to jsp:param "value" attribute
提问by GVR
Hie... I was wondering whether it is possible to send dynamic content involving "if" or "switch" functions to jsp:param "value" attribute ... one can send a single value which can be represented in the following manner
嘿...我想知道是否可以将涉及“if”或“switch”函数的动态内容发送到 jsp:param“value”属性...可以发送一个可以用以下方式表示的值
<jsp:param name="blah" value="<%=blah%>"/>
now what i mean to ask is ..
现在我想问的是..
<jsp:param name="blah" value="<%
if(blah == 1)
out.print("The value is 1");
if(blah == 2)
out.print("The value is 2");
%>"/>
is the above method possible.. when i do the same i get an error stating that an " = " sign is expected after the tag in value attribute ..
上述方法是否可行..当我做同样的事情时,我收到一个错误,指出在 value 属性中的标签之后需要一个“=”符号..
采纳答案by Kevin Bowersox
I would recommend determining the value of blah
prior to executing your jsp
. This can be done within a servlet
using straight Java. Once you have determined the value of blah
place it in the request before forwarding to the jsp
.
我建议blah
在执行您的jsp
. 这可以在servlet
直接使用 Java 的情况下完成。一旦确定了blah
将其放入请求中的值,然后再转发到jsp
.
request.setAttribute("blah", "some value");
Then within your .jsp
file you can reference the attribute using jsp expression language
.
然后在您的.jsp
文件中,您可以使用jsp expression language
.
${blah}
Its best to keep as much logic out of your view (jsp) as possible.
最好将尽可能多的逻辑排除在您的视图 (jsp) 之外。
回答by spiritwalker
would you consider to change it to
你会考虑把它改成
<% if(blah == 1){ %>
<jsp:param name="blah" value="The value is 1"/>
<%}else{%>
<jsp:param name="blah" value="The value is 2"/>
<%}%>
else use equivalent JSTL tags like
否则使用等效的 JSTL 标签,如
<c:choose>
<c:when test="${blan eq 1}">
<jsp:param name="blah" value="The value is 1"/>
</c:when>
<c:otherwise>
<jsp:param name="blah" value="The value is 2"/>
</c:otherwise>
</c:choose>
@gabbi,
You do not necessarily have to have two <jsp:forward>
, instead you can declare another variable for holding the value, like below:
@gabbi,您不一定必须有两个<jsp:forward>
,而是可以声明另一个变量来保存值,如下所示:
<%
String blahValue = "";
if(blah == 1){
blahValue = "The value is 1";
}else if(blah==2){
blahValue = "The value is 2";
}else{
blahValue = "the value is invalid"; }
%>
<jsp:forward>
<jsp:param name="blah" value="<%=blahValue%>"/>
</jsp:forward>