Struts 2 s:select tag 动态id

Struts 2 s:select tag dynamic id(Struts 2 s:select tag 动态id)

本文介绍了Struts 2 s:select tag 动态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;

推荐答案

Scriptlets 是旧的做事方式,你应该避免在 JSP 中编写 Java 代码;
Struts2 仅使用它的标签和 OGNL 帮助您实现相同的目标.

Scriptlets 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/> 部分正在工作,因为您在 HTML 标记内注入了 scriptlet,这是允许的.

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

<s:select/> 部分不起作用,因为您在 Struts2 标记内注入了 scriptlet,这是不允许的.

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

为了让它工作,你应该使用 OGNL 中的 #attr 语法来访问 ScriptletsJava 变量code> 并在 Page Context 中推送由你,像这样(完全未经测试):

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>

<小时>

P.S: 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:select tag 动态id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Struts 2 s:select tag 动态id

基础教程推荐