You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rick Reumann <ma...@reumann.net> on 2002/12/10 18:25:34 UTC

Using the "key" of a HashMap in an html:option tag ?

I have a case where I'm getting returned a HashMap that has an "id" as
the key and an EmployeeBean as the value. For now I'm trying to get a
select box with the options created from this map and the key is the
value. The only part I'm having trouble with is using the "key" as the
value of the option tag:

<html:select styleClass="field" property="assignedTo">
    <logic:iterate id="element" name="employees">
        <html:option value="<%= **** key value here **** ??? %>">
            <bean:write name="element" property="value.firstName"/>
            <bean:write name="element" property="value.lastName"/>
        </html:option>
    </logic:iterate> 
</html:select>

I can't use the simple method  of

<html:options collection="employees" property="key" labelProperty="value"/>

Since the value in this case is a bean and I need to make the value
from two of it's properties.

I've tried all different ways to get a handle to the key but am having
no luck the way I set it up. What am I doing wrong?

Thanks,

-- 

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Using the "key" of a HashMap in an html:option tag ?

Posted by Kris Schneider <kr...@dotech.com>.
The problem is that the type of the property that the tag handler exposes as an
attribute has to be assignable from the type of the expression. Taking the
current example:

<html:select styleClass="field" property="assignedTo">
  <logic:iterate id="element" name="employees">
    <html:option value="<%= ((java.util.Map.Entry)element).getKey() %>">
      <bean:write name="element" property="value.firstName"/>
      <bean:write name="element" property="value.lastName"/>
    </html:option>
  </logic:iterate>
</html:select>

The type of the expression is Object and the type of the tag handler's
(OptionTag) value property is String. So you'll end up with a compilation error
like:

setValue(java.lang.String) in org.apache.struts.taglib.html.OptionTag cannot be
applied to (java.lang.Object)

In which case, either of these should work:

<%= ((java.util.Map.Entry)element).getKey().toString() %>
<%= String.valueOf(((java.util.Map.Entry)element).getKey()) %>

If you know that the key really is a String, you could also cast:

<%= (String)((java.util.Map.Entry)element).getKey() %>

Quoting Rick Reumann <ma...@reumann.net>:

> On Tuesday, December 10, 2002, 5:06:26 PM, Kris wrote:
> 
> KS> Glad it's working, but I don't understand why you'd have a problem with
> the JSP
> KS> expression. IIRC, it's equivalent to the following code getting generated
> within
> KS> the page's _jspService method:
> 
>     Trust me I don't know either. I'm using Tomcat 4.0.6 if that
>     matters. It only causes me this problem when inside of a tag
>     trying to use <%= . I don't get it. Has anyone else ever ran into
>     this situation?
> 
>     
> -- 
> 
> Rick
> mailto:maillist@reumann.net
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[4]: Using the "key" of a HashMap in an html:option tag ?

Posted by Rick Reumann <ma...@reumann.net>.
On Tuesday, December 10, 2002, 5:06:26 PM, Kris wrote:

KS> Glad it's working, but I don't understand why you'd have a problem with the JSP
KS> expression. IIRC, it's equivalent to the following code getting generated within
KS> the page's _jspService method:

    Trust me I don't know either. I'm using Tomcat 4.0.6 if that
    matters. It only causes me this problem when inside of a tag
    trying to use <%= . I don't get it. Has anyone else ever ran into
    this situation?

    
-- 

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Re[2]: Using the "key" of a HashMap in an html:option tag ?

Posted by Kris Schneider <kr...@dotech.com>.
Rick,

Glad it's working, but I don't understand why you'd have a problem with the JSP
expression. IIRC, it's equivalent to the following code getting generated within
the page's _jspService method:

// out is a javax.servlet.jsp.JspWriter instance
out.print( ((java.util.Map.Entry)element).getKey() );

Which, for an Obect argument to the print method, JspWriter treats as:

String s = String.valueOf( ((java.util.Map.Entry)element).getKey() );
byte[] buffer = s.getBytes();
for (int i = 0, n = buffer.length; i < n; i++) {
  out.write(buffer[i]);
}

Quoting Rick Reumann <ma...@reumann.net>:

> On Tuesday, December 10, 2002, 2:21:36 PM, Kris wrote:
> 
> KS> You may have to cast "element":
> 
> KS> <%= ((java.util.Map.Entry)element).getKey() %>
> 
>     Thanks Kris! Yes that's exactly what I had to do (mostly). I say
>     mostly, because so many times I've been bitten in the butt by this
>     and I'm not sure why it works this way but...
>     for some reason I often can't just use the scriplet: <%=
>     to force a String conversion, when inside of a tag. I forget
>     I have to do:
>     <% String.valueOf( ) %>.
>     So in the above I think I tried something
>     like you mentioned but then realized it only works for me when I
>     do:
>     value="<% String.valueOf( ((java.util.Map.Entry)element).getKey() ) %>"
> 
>     Appreciate you coming up with the solution. I was pulling my hair
>     out over this.
> 
> -- 
> 
> Rick
> mailto:maillist@reumann.net
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[2]: Using the "key" of a HashMap in an html:option tag ?

Posted by Rick Reumann <ma...@reumann.net>.
On Tuesday, December 10, 2002, 2:21:36 PM, Kris wrote:

KS> You may have to cast "element":

KS> <%= ((java.util.Map.Entry)element).getKey() %>

    Thanks Kris! Yes that's exactly what I had to do (mostly). I say
    mostly, because so many times I've been bitten in the butt by this
    and I'm not sure why it works this way but...
    for some reason I often can't just use the scriplet: <%=
    to force a String conversion, when inside of a tag. I forget
    I have to do:
    <% String.valueOf( ) %>.
    So in the above I think I tried something
    like you mentioned but then realized it only works for me when I
    do:
    value="<% String.valueOf( ((java.util.Map.Entry)element).getKey() ) %>"

    Appreciate you coming up with the solution. I was pulling my hair
    out over this.

-- 

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Using the "key" of a HashMap in an html:option tag ?

Posted by Kris Schneider <kr...@dotech.com>.
Maybe something like this:

<html:select styleClass="field" property="assignedTo">
  <logic:iterate id="element" name="employees">
    <html:option value="<%= element.getKey() %>">
      <bean:write name="element" property="value.firstName"/>
      <bean:write name="element" property="value.lastName"/>
    </html:option>
  </logic:iterate>
</html:select>

You may have to cast "element":

<%= ((java.util.Map.Entry)element).getKey() %>

Can't recall if you can directly access "element" in a JSP expression, so if
not, try:

<%= ((java.util.Map.Entry)pageContext.getAttribute('element')).getKey() %>

Quoting Rick Reumann <ma...@reumann.net>:

> I have a case where I'm getting returned a HashMap that has an "id" as
> the key and an EmployeeBean as the value. For now I'm trying to get a
> select box with the options created from this map and the key is the
> value. The only part I'm having trouble with is using the "key" as the
> value of the option tag:
> 
> <html:select styleClass="field" property="assignedTo">
>     <logic:iterate id="element" name="employees">
>         <html:option value="<%= **** key value here **** ??? %>">
>             <bean:write name="element" property="value.firstName"/>
>             <bean:write name="element" property="value.lastName"/>
>         </html:option>
>     </logic:iterate> 
> </html:select>
> 
> I can't use the simple method  of
> 
> <html:options collection="employees" property="key" labelProperty="value"/>
> 
> Since the value in this case is a bean and I need to make the value
> from two of it's properties.
> 
> I've tried all different ways to get a handle to the key but am having
> no luck the way I set it up. What am I doing wrong?
> 
> Thanks,
> 
> -- 
> 
> Rick
> mailto:maillist@reumann.net
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[2]: Using the "key" of a HashMap in an html:option tag ?

Posted by Rick Reumann <ma...@reumann.net>.

On Tuesday, December 10, 2002, 12:39:26 PM, Vinh wrote:

VT> have you tried this?

VT> <html:option name="element" value="key">

    Tried that, but name is an invalid attribute of <html:option>.

    I must be missing something simple here. There has to be a way to
    get the value of the key set as the option value property.

    Thanks,


-- 

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Using the "key" of a HashMap in an html:option tag ?

Posted by Vinh Tran <vi...@processintelligence.com>.
have you tried this?

<html:option name="element" value="key">

-----Original Message-----
From: Rick Reumann [mailto:maillist@reumann.net]
Sent: Tuesday, December 10, 2002 12:26 PM
To: Struts List
Subject: Using the "key" of a HashMap in an html:option tag ?


I have a case where I'm getting returned a HashMap that has an "id" as
the key and an EmployeeBean as the value. For now I'm trying to get a
select box with the options created from this map and the key is the
value. The only part I'm having trouble with is using the "key" as the
value of the option tag:

<html:select styleClass="field" property="assignedTo">
    <logic:iterate id="element" name="employees">
        <html:option value="<%= **** key value here **** ??? %>">
            <bean:write name="element" property="value.firstName"/>
            <bean:write name="element" property="value.lastName"/>
        </html:option>
    </logic:iterate>
</html:select>

I can't use the simple method  of

<html:options collection="employees" property="key" labelProperty="value"/>

Since the value in this case is a bean and I need to make the value
from two of it's properties.

I've tried all different ways to get a handle to the key but am having
no luck the way I set it up. What am I doing wrong?

Thanks,

--

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>