javascript Struts 2 s:select 标签动态id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14639898/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:11:49  来源:igfitidea点击:

Struts 2 s:select tag dynamic id

javajavascriptjspstruts2ognl

提问by Rohit Ramkumar

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.

我在一个 JSP 页面和一个按钮中有多个不同类型的字段。这些字段是根据从我创建的元数据表中获取的信息生成的。

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.

因为我不知道有多少和什么类型的字段存在,我给id他们动态的。我在我的 JSP 中使用 Struts 2 标签。

The issue is with the <s:select>tag: when I give scriplet within the idattribute, it displays the following error :

问题出在<s:select>标签上:当我在id属性中提供 scriplet 时,它显示以下错误:

org.apache.jasper.JasperException: /success.jsp(83,12) quote symbol expected

org.apache.jasper.JasperException: /success.jsp(83,12) 需要引号

<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 is as follows:

Javascript如下:

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;

回答by Andrea Ligios

Scriptlets are the old way of doing things, you should avoid writing Javacode in JSP's at all;
Struts2 helps you achieving the same goals using its tags and OGNLonly.

Scriptlets 是旧的做事方式,您应该完全避免JavaJSPs 中编写代码
Struts2OGNL仅使用其标签帮助您实现相同的目标。

The <input />part is working because you are injecting a scriptletinside an HTML tag, that is allowed.

<input />部分正在工作,因为您scriptlet在 HTML 标记内部注入了,这是允许的。

The <s:select />part is not working because you are injecting a scriptletinside an Struts2 tag, that is not allowed.

<s:select />部分无法正常工作,因为您scriptlet在 Struts2 标签内注入了,这是不允许的。

To make it work, you should use #attrsyntax in OGNLto access the Javavariables declared in Scriptletsand pushed by youin the Page Context, like this (completely untested):

为了使它的工作,你应该使用#attr语法OGNL访问的Java变量声明Scriptlets,并推动通过你Page Context,像这样(没有经过充分测试):

<%
    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']}" />
<%    
    }
%>

However, even if it's technically possible, it is discouraged. You should use the pure Struts2 way for that, that would be the following:

但是,即使技术上可行,也不鼓励这样做。您应该使用纯 Struts2 方式,如下所示:

<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>


P.S: Struts tags doesn't have any styleClassattribute; you can use cssClassand/or cssStyle;
And, if controlTypeis a String, you should use .equalsinstead of ==: <s:if test="%{#masterColDO.controlType.equals('dropdown')}">.

PS:Struts标签没有任何styleClass属性;您可以使用cssClass和/或cssStyle
而且,如果controlType是一个字符串,你应该使用.equals,而不是==<s:if test="%{#masterColDO.controlType.equals('dropdown')}">

回答by Lukasz Lenart

You should rather use Struts2 expression syntax likes this:

你应该使用像这样的 Struts2 表达式语法:

id="%{'columnId' + count}"

回答by arvin_codeHunk

Struts2 usage valuestacks, So this count should be fetched in ognl manner, try something like this:

Struts2 使用 valuestacks,所以这个计数应该以 ognl 方式获取,尝试这样的事情:

id="%{'columnId'+count}"