Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts html:select error

 
chris hart
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code sniblet below. My issue is this. When I attempt to pass a script variable into a function it is not being dereferenced. What gets passed to the function is <%=index%>, rather then the value of the variable index. Any ideas why? This syntax works with other html tags and I believe it has something to do with the struts html:select tag. Attempting to use double quotes or no quotes results in a complilation error. Thank for any help.


<SCRIPT>
function performActions(id)
{
creditForm.id.value=id;
creditForm.submit();
}
</SCRIPT>
<% String index = "1" ; %>
<html:select property="<%=property%>" onchange="performActions('<%index%')">
 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's performActions('<%=index%>') not performActions('<%index%')>
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and they're usually called "code snippets"
[ May 17, 2005: Message edited by: Junilu Lacar ]
 
chris hart
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the lack of the "=" is a type o. The code does not function properly with the "=" added to <%index%>
In debugging I can see that <%=index%> is what is contained in the var.
 
chris hart
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below are two drop down boxes. One using the struts html taglib. The struts drop down box does not dereference the var when it is passed to the javascript function. The non struts drop down works as expected. Can anyone explain why the struts html:select acts differently and how I can get it to dereference the var
Non struts dropdown:
<select name="<%=property%>" onchange="javascript erformActions('<%=property%>')">
<option value="include">Include</option>
<option value="payoff">Payoff</option>
<option value="payoffWithAssets">Payoff with Assets</option>
<option value="exclude">Exclude</option>
</select>

Struts drop down:
<html:select property="<%=property%>" onchange="javascript erformActions('<%=property%>')">
<html ption value="include">Include</html ption>
<html ption value="payoff">Payoff</html ption>
<html ption value="payoffWithAssets">Payoff with Assets</html ption>
<html ption value="exclude">Exclude</html ption>
</html:select>
 
chris hart
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a solution. The variable would not get derefernced so I used the "this" keyword, now the value of the object gets passed.

<html:select property="<%=property%>" onchange="javascript erformActions(this.name)">
<html ption value="include">Include</html ption>
<html ption value="payoff">Payoff</html ption>
<html ption value="payoffWithAssets">Payoff with Assets</html ption>
<html ption value="exclude">Exclude</html ption>
</html:select>
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason struts didn't decode your <%=index%> scriptlet is that struts has an "all or nothing" policy on scriptlets in attributes. This means that if the entire attribute is a scriptlet, struts will decode it, but if only part is a scriptlet, struts will not decode it.

If you had written

<%="performActions(" + index + ")"%>

It would have worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic