Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error using html:options tag

 
sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

i am using following code in my jsp page
-----------------------

<html:select styleClass="formtext-1" property="painScale">
<% String totalPainScale []={"-1","1","2","3","4","5","6","7","8","9","10"}; %>
<html ptions collection="<%=totalPainScale%>"/>
</html:select>
------------------
i am getting error

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 276 in the jsp file: /pd/PatientComplaint.jsp
Generated servlet error:
The method setCollection(String) in the type OptionsTag is not applicable for the arguments (String[])
--------------------------

even i want the difference between html ption tag and html ptionsCollection tag

I have checked the difference in the followingdocs link.

I am cleared with html ptions tag but I not clear with html ptionsCollection tag.

clear my doubt with an example.

Thanks
[ March 28, 2007: Message edited by: sreenivas jeenor ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few problems with your code.

First is the fact that any object used in a struts tag must be in some scope (either page, request, session, or application). It's not enough just to declare a scriptlet variable. So, your scriptlet should look like this:

This code puts the array in page scope, so now it's available to a Struts tag.

The second problem is that you're using the wrong attribute of the htmlptions tag. If you've got a collection of Strings and you want each String to be both the label and the value of the option, you simply specify the name of the collection or array as the name attribute. So, your htmlptions tag should look like this:


Regarding the difference between htmlptions and htmlptionsCollection, here is the rule I generally use:

If you've got an array or collection of strings that represent both the label and the value of the option, use htmlptions just like I showed you in the example above.

If you've got a collection of JavaBeans for which one bean property represents the label and another represents the value, use htmlptionsCollection. Example:

In the above example, the LabelValueBean has a label and a value property.
[ March 29, 2007: Message edited by: Merrill Higginson ]
 
sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Merrill,

now i got the difference between the two.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic