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;
Scriptlets are the old way of doing things, you should avoid writingJavacode inJSP‘s at all;Struts2 helps you achieving the same goals using its tags and
OGNLonly.The
<input />part is working because you are injecting ascriptletinside an HTML tag, that is allowed.The
<s:select />part is not working because you are injecting ascriptletinside an Struts2 tag, that is not allowed.To make it work, you should use
#attrsyntax inOGNLto access theJavavariables declared inScriptletsand pushed by you in thePage Context, like this (completely untested):However, even if it’s technically possible, it is discouraged. You should use the pure Struts2 way for that, that would be the following:
P.S: Struts tags doesn’t have any
styleClassattribute; you can usecssClassand/orcssStyle;And, if
controlTypeis a String, you should use.equalsinstead of==:<s:if test="%{#masterColDO.controlType.equals('dropdown')}">.