5

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.

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.

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) 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 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;
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
android_eng
  • 1,370
  • 3
  • 17
  • 40

3 Answers3

8

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.

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

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

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

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 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')}">.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
3

You should rather use Struts2 expression syntax likes this:

id="%{'columnId' + count}"
Lukasz Lenart
  • 987
  • 7
  • 14
  • In my code count is a JAVA variable. I am using it as a counter. i have instantiated it as <% int count = 0;%> . I tried with the above piece of code,it's not working – android_eng Feb 01 '13 at 07:53
  • This class="login-textbox " name="chngdColumnValues" id=<%="columnId" + count%> /> works – android_eng Feb 01 '13 at 07:56
  • 1
    Mixing scriplets with JSP tags is a bad practise, you should try to use or or define count as action's property http://struts.apache.org/2.x/docs/iterator.html – Lukasz Lenart Feb 02 '13 at 09:46
0

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

id="%{'columnId'+count}"
arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47
  • In my code count is a JAVA variable. I am using it as a counter. I have instantiated it as <% int count = 0;%> . I tried with the above piece of code,it's not working. This class="login-textbox " name="chngdColumnValues" id=<%="columnId" + count%> /> works – android_eng Feb 01 '13 at 07:56