1

Please Help me here to solve below problem.

Code:

<%for(int ind=0;ind<15;ind++){%>

 <tr>
   <struts-el:select name="OpEnh01pagSincomModelMaintanenceFormBean" property="mdlCode" styleId="mdlDrpDown_+'<%=ind %>'" onchange="modelCodeChanged(this.id)">
     <struts-el:options collection="listmodelCodes" property="modelCode" labelProperty="modelCodeDesc" />
   </struts-el:select> 
 </tr>

<%}%>

1) I want 15 dropdowns in 15 rows. I want dropDown's index to identify which dropDown is modified in JavaScript.

2) I want to get a unique id so that I can access it using dom in JavaScript called by onChangeEvent.

I have a form bean Name properties mdlDrpDown1, mdlDrpDown2 and so on..

I tried this format: styleId="mdlDrpDown_+'<%=ind %>'" but couldn't get it as styleId, property attribute are not runTimeExpr.

Vishnu300
  • 126
  • 2
  • 11
  • onchange passes `this.id` but there is no id attribute set. – coding_idiot Aug 27 '15 at 04:27
  • Actually in generated html. "styleId" attribute is converted to "id" attribute. Hence I used this.id as parameter to JS. But my main problem is i cant get the loopIndex in the property or StyleID attribute. – Vishnu300 Aug 27 '15 at 04:44

2 Answers2

1

You are using dynamic property names mdlCode<%=ind %> for select, if getter and setter are not found in the formbean then No getter method Exception will be raised, to avoid this we can index based properties (which can holds multiple values based on index, index value will order of the elements appear in the page) instead of single properties (which can holds only one value).

Try the below code:

FormBean:

//create getter and setter for `mdlCode` using String[], so it can hold dynamic values.

String[] mdlCode;

public String[] getMdlCode() {
   return mdlCode;
}
public void setMdlCode(String[] mdlCode) {
   this.mdlCode=mdlCode;
}

JSP:

<%for(int ind=0;ind<15;ind++) {
    String id = "mdlDrpDown_"+ind;
%>

<tr>
    <!-- change property from `property="mdlCode<%=ind %>"` to property='mdlCode' -->
    <struts-el:select name="OpEnh01pagSincomModelMaintanenceFormBean" property="mdlCode" styleId="<%=id %>" onchange="modelCodeChanged(this.id)">
        <struts-el:options collection="listmodelCodes" property="modelCode" labelProperty="modelCodeDesc" />
    </struts-el:select> 
</tr>

<%}%>
Srinu Chinka
  • 1,471
  • 13
  • 19
  • I tried and got error: JSPG0124E: Custom tag attribute styleId cannot be runtime expression. value: "[%=id %]" – Vishnu300 Aug 27 '15 at 08:43
  • Now i changed the styleId="const" a **constant** >> Page loaded and Now the generated html for all select elements has : **name="mdlCode" id="const" ** 1)the modification of property attribute is not helpful. 2)styleID **cannot have any rutime expressions in it**. so i cant get scriptlet <%=ind %> evaluated here. – Vishnu300 Aug 27 '15 at 08:47
  • @SalmanParacha: Can you check this once. this question is similar to your earlier post: [link](http://stackoverflow.com/questions/5970295/how-to-append-loop-index-of-cforeach-tag-to-struts-html-tag-attributes) – Vishnu300 Aug 27 '15 at 08:53
  • @Vishnu300 i tired the same code in struts-1.3 and tomcat its working for me. which version of strtuts you are using and server? – Srinu Chinka Aug 27 '15 at 09:00
  • What is the generated html for select tag ( id and name attributes) in your case ? Struts 1.1 and IBM WAS 6.1 are the versions used. – Vishnu300 Aug 27 '15 at 09:15
  • when directly used `<%=ind %>` it is not evaluated, when created separate variable (`id`) the output came as expected, i.e. `mdlDrpDown_1` ... . – Srinu Chinka Aug 27 '15 at 09:30
  • Is it version specific ? Cant get in my configuration.. @Srinu: Any Idea ? – Vishnu300 Aug 27 '15 at 09:49
  • @Vishnu300 I checked it with `struts 1.1` also, its working same as `struts1.3`. May be the problem is with your server. once check with that. – Srinu Chinka Aug 27 '15 at 10:18
  • @Vishnu300 Which `taglib` you are using? I'm using `<%@ taglib prefix="struts-el" uri="http://jakarta.apache.org/struts/tags-html" %>` for `select` – Srinu Chinka Aug 27 '15 at 10:28
  • @Vishnu300 once check with `tld file` for ` styleId false true ` under `select` in this `` must be `true`, they only run time values can be evaluated. – Srinu Chinka Aug 27 '15 at 10:36
  • indexed ID is coming now.my **taglib uri is different** earlier whose styleID attribute has **rtexprvalue-false**. Now i modified and its working. **Thanks alot @Srinu for all the help.** – Vishnu300 Aug 27 '15 at 11:27
  • generated html is: ** – Vishnu300 Aug 27 '15 at 11:38
  • yes, you can get values by calling `getMdlCode()`, it'll return `String[]` and order of values stored in the array is the order of the `select` appears in the output. – Srinu Chinka Aug 27 '15 at 11:46
  • Thanks alot for the answer. – Vishnu300 Aug 27 '15 at 11:51
-1

The format of your styleId will produce ids like:

id="mdlDrpDown_+'1'" 
id="mdlDrpDown_+'2'"

You should write styleId like this:

styleId="mdlDrpDown_<%=ind %>"

then ids will be like:

id="mdlDrpDown_1" 
id="mdlDrpDown_2"
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
  • @ Brijesh: I got error when tried: styleId="mdlDrpDown_<%=ind %>" javax.servlet.jsp.JspException: No getter method available for property mdlDrpDown_%=ind % for bean. <%=ind> is not evaluated. – Vishnu300 Aug 27 '15 at 06:11
  • Then I tried: styleId="<%=ind %>". Error is: JSPG0124E: Custom tag attribute styleId cannot be runtime expression. value: "[%=india %]" it is same in the tld file also. – Vishnu300 Aug 27 '15 at 06:14
  • @Vishnu300 Not aware about this Exception. But I think scriptlet cannot help you here. – Brijesh Bhatt Aug 27 '15 at 06:27
  • Yes..Can we keep scriptlet variable in pageScope and then use EL to append index dynamically here ? I know very less of EL and JSTL so please help in this context.. – Vishnu300 Aug 27 '15 at 06:42
  • @Vishnu300 <%=ind> is evaluating, you are getting exception because of dynamic property name, you are using `struts select` tag which means you must have getter and setter methods in `formbean` class. – Srinu Chinka Aug 27 '15 at 06:43
  • @ Srinu: I used the name property="mdlCode" which has setter&getter in formBean. I have corrected it in question now.. The <%=ind> is not evaluated hence got the error: JSPG0124E: Custom tag attribute styleId cannot be runtime expression. value: "[%=india %]" – Vishnu300 Aug 27 '15 at 07:00
  • @Vishnu300 check my answer – Srinu Chinka Aug 27 '15 at 07:11