Struts 2 s:选择标签动态ID [英] Struts 2 s:select tag dynamic id
问题描述
我在JSP页面中有多个不同类型的字段和一个按钮。
这些字段是根据从我创建的元数据表中获取的信息生成的。
I have multiple fields of various types in a JSP page and one button. These fields are generated based on the info got from a metadata table that I have created.
因为我不知道有多少和哪种类型的字段存在,我给动态 id
给他们。我在JSP中使用Struts 2标签。
Since I don't know how many and what type of fields are present, I am giving dynamic id
's to them. I am using Struts 2 tags in my JSP.
问题在于< s:select>
标签:当我在 id
属性中给出scriplet时,它会显示以下错误:
The issue is with the <s:select>
tag: when I give scriplet within the id
attribute, it displays the following error :
org.apache.jasper.JasperException:/success.jsp(83,12)引号符号预期
org.apache.jasper.JasperException: /success.jsp(83,12) quote symbol expected
<s:if test="%{#masterColDO.controlType=='dropdown'}">
<s:select styleClass="login-textbox"
style="width:130px"
list="#masterColDO.validation"
name="chngdColumnValues"
id=<%="columnId" + count%> />
</s:if>
<s:else>
<input type=<s:property value="#masterColDO.controlType" />
class="login-textbox "
name="chngdColumnValues"
id=<%="columnId" + count%> />
</s:else>
Javascript如下:
Javascript is as follows:
var addUpdateBtnId = document.getElementById('addUpdateBtnId');
addUpdateBtnId.value='Update';
addUpdateBtnId.onclick = function() {
onClickUpdateBtn(rowIndex);
};
var selectedUpdateRow = xmlhttp.responseText.split(",");
for(var i = 0; i < selectedUpdateRow.length; i++){
var columnElementId = "columnId"+i;
document.getElementById(columnElementId).value = selectedUpdateRow[i];
}
document.getElementById("columnId"+(primaryKeyPos-1)).readOnly = true;
推荐答案
Scriptlet
s是旧的做事方式,你应该避免写 Java
JSP
中的代码;
Struts2仅使用其标签和 OGNL
帮助您实现相同的目标。
Scriptlet
s are the old way of doing things, you should avoid writing Java
code in JSP
's at all;
Struts2 helps you achieving the same goals using its tags and OGNL
only.
< input />
部分正在运行,因为您正在注入 scriptlet
在HTML标记内,这是允许的。
The <input />
part is working because you are injecting a scriptlet
inside an HTML tag, that is allowed.
< s:select />
部分无效,因为您正在注入<$ c $在struts2标记内的c> scriptlet ,这是不允许的。
The <s:select />
part is not working because you are injecting a scriptlet
inside an Struts2 tag, that is not allowed.
为了使其正常工作,你应该使用#
语法,用于访问 OGNL
中的attr 中声明的
并在 Java
变量Scriptlets 页面上下文
中按推送,就像这样(完全未经测试):
To make it work, you should use #attr
syntax in OGNL
to access the Java
variables declared in Scriptlets
and pushed by you in the Page Context
, like this (completely untested):
<%
for (int counter=0;counter<myList.size();counter++) {
// pushing it into the pageContext
pageContext.setAttribute("counter",counter);
%>
<s:select cssClass="login-textbox"
cssStyle="width:130px"
list="#masterColDO.validation"
name="chngdColumnValues"
id="%{'columnId' + #attr['counter']}" />
<%
}
%>
然而,即使技术上可行,也不鼓励。您应该使用纯Struts2方式,如下所示:
However, even if it's technically possible, it is discouraged. You should use the pure Struts2 way for that, that would be the following:
<s:iterator value="myList" status="ctr">
<s:select cssClass="login-textbox"
cssStyle="width:130px"
list="#masterColDO.validation"
name="chngdColumnValues"
id="%{'columnId' + #ctr.index}" />
</s:iterator>
PS:Struts标签没有任何 styleClass
属性;你可以使用 cssClass
和/或 cssStyle
;
如果 controlType
是一个字符串,你应该使用 .equals
而不是 ==
:< s:if test =%{#masterColDO.controlType.equals('dropdown')}>
。
P.S: Struts tags doesn't have any styleClass
attribute; you can use cssClass
and/or cssStyle
;
And, if controlType
is a String, you should use .equals
instead of ==
: <s:if test="%{#masterColDO.controlType.equals('dropdown')}">
.
这篇关于Struts 2 s:选择标签动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!