java struts 1.0 逻辑标签选择 - if/else 逻辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15092236/
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
struts 1.0 logic tag choice - if/else logic
提问by bouncingHippo
What I have is a dropdown list. When the user selects a certain option, where each option represents a specific String on the Java server side.
我有一个下拉列表。当用户选择某个选项时,其中每个选项代表 Java 服务器端的一个特定 String。
Right now, the Java server side able to check which option is selected, and the number to correspond. At the moment, I am able to output the value in Java backend, not on the JSP page.
现在,Java 服务器端能够检查选择了哪个选项,以及对应的编号。目前,我能够在 Java 后端输出值,而不是在 JSP 页面上。
Is there an if/else tag for Struts 1.0?
Struts 1.0 是否有 if/else 标签?
I am not sure which logic tag is the best to pass a Java value for frontend processing:
我不确定哪个逻辑标记最适合为前端处理传递 Java 值:
JSP page
JSP页面
if(value = 666)
this textbox is readonly
else
this textbox row is active
My research so far:
到目前为止我的研究:
Looking at logic:equal
, it seems to pass a value on the JSP page using taglibs like below. This doesn't work for me, because I want to pass the value FROMa Java class on the server side.
看着logic:equal
,它似乎使用如下标签库在 JSP 页面上传递一个值。这对我不起作用,因为我想从服务器端的 Java 类传递值。
<logic:equal name="name" property="name" value="<%= theNumber %>" >
回答by PSR
<c:choose>
<c:when test="${the number}">
Both are equal.
</c:when>
<c:otherwise>
Both are not equal.
</c:otherwise>
</c:choose>
this is jstl tag
这是jstl标签
you need to use
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
回答by James Drinkard
The JSTL answer is the best one, however, in my case it was an old Java legacy application without JSTL and I couldn't introduce it.
JSTL 答案是最好的答案,但是,就我而言,它是一个没有 JSTL 的旧 Java 遗留应用程序,我无法介绍它。
This was the requirement I had with some legacy Struts 1.3 code. Set a checkbox to match the value of a form bean's value from a database call, as in "Y", "N", "", or null.
这是我对一些遗留 Struts 1.3 代码的要求。设置复选框以匹配来自数据库调用的表单 bean 的值,如“Y”、“N”、“”或 null。
I also had to keep in within struts logic tags and not use JSTL, which was my original preference. I know that the struts docssaid the checkbox should equate to a boolean variable in the actionForm, but I used a string value, which worked.
我还必须保持在 struts 逻辑标签内,而不是使用 JSTL,这是我最初的偏好。我知道 struts文档说复选框应该等同于 actionForm 中的布尔变量,但我使用了一个字符串值,它起作用了。
From the docs link above:
从上面的文档链接:
NOTE: The underlying property value associated with this field should be of type boolean, and any value you specify should correspond to one of the Strings that indicate a true value ("true", "yes", or "on"). If you wish to utilize a set of related String values, consider using the multibox tag.I had thought about converting my string values to booleans in the form, but it took the strings this way.
注意:与此字段关联的基础属性值应为布尔类型,您指定的任何值都应对应于指示真值的字符串之一(“true”、“yes”或“on”)。如果您希望使用一组相关的字符串值,请考虑使用 multibox 标签。我曾想过将我的字符串值转换为表单中的布尔值,但它采用了这种方式。
Last of all, I used some javascript to set the checked status. All of this is verbose and not the best solution, but faced with the requirement, it does work.
最后,我使用了一些 javascript 来设置选中状态。所有这些都是冗长的,并不是最好的解决方案,但面对需求,它确实有效。
Here is the code:
这是代码:
<logic:equal name="<%=formName%>" property="formInd" value="Y">
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = true;
</script>
</logic:equal>
<logic:equal name="<%=formName%>" property="formInd" value="N">
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = false;
</script>
</logic:equal>
<logic:empty name="<%=formName%>" property="formInd" >
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = false;
</script>
</logic:empty>