You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by mfirry <mf...@hotmail.com> on 2008/06/17 12:29:51 UTC

populate combo after comparing bean values with enum

hi,
i need to populate my combo with options taken from the FormBean and
compared with the Enum values.

i have it working but i doubt it's the best way to do it (and it mixes too
much struts tags with jstl ones, in my opinion)

anybody can help?


<%@page import="MyPackage.*" //this package contains the class with the enum
used further down %>

<%! String val; %>
<html:form action="/full_profiloGiocoRespAutoesclusione">
 <label>WANT TO DO THIS?</label> 
 <% String d2 = "session"; %>
 <logic:notEmpty name="MyFormBean" property="duration">
  <bean:define id="d1" name="MyFormBean" property="duration"/>
  <% d2 = d1.toString();%>
 </logic:notEmpty>
<fieldset>
 <label for="question">Choose duration</label>
 <html:select property="duration" name="MyFormBean">
 <%
  QuestionValues value;
  String d = "session";
  if (d2 != null) {
    d = d2;
  }
  if ("day".equals(d)) {
    value = QuestionValues.day;
  } 
  else if ("week".equals(d)) {
    value = QuestionValues.week;
  } else {
    value = QuestionValues.session;
  }
  for (QuestionValues v : QuestionValues.values()) {
    if (v.compareTo(value) >= 0) {
      val = (String) (v.getLabel());
  %>
  <html:option value='<%=val%>'><%=val%></html:option>
  <% }
  } 
  %>
  </html:select>
 </fieldset>
</html:form>
-- 
View this message in context: http://www.nabble.com/populate-combo-after-comparing-bean-values-with-enum-tp17882097p17882097.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: populate combo after comparing bean values with enum

Posted by mfirry <mf...@hotmail.com>.
 hi, and thanks for your help

With all those scriptlets, mixing of taglib and RT expression values, 
and the relevant Java code not included, it's a little difficult to see 
exactly what you're attempting.

i'm just trying to get the duration from the FormBean, compare it with the
other possible values from the Enum type and show only certain values as
options of the combo

I'm assuming MyFormBean.duration's type is a custom enumeration with 
with a getLabel() method, and that you want to use the label associated 
with each enum value as both the label and value of the select options? 
but you're using the enum value directly in the html:select?

It looks like it would be a lot cleaner and easier to handle the 
conversion to and from enum values in your action, and let the form bean 
and JSP deal with a simple list of Strings.

how?
-- 
View this message in context: http://www.nabble.com/populate-combo-after-comparing-bean-values-with-enum-tp17882097p17937275.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: populate combo after comparing bean values with enum

Posted by Laurie Harper <la...@holoweb.net>.
mfirry wrote:
> hi,
> i need to populate my combo with options taken from the FormBean and
> compared with the Enum values.
> 
> i have it working but i doubt it's the best way to do it (and it mixes too
> much struts tags with jstl ones, in my opinion)
> 
> anybody can help?
> 
> 
> <%@page import="MyPackage.*" //this package contains the class with the enum
> used further down %>
> 
> <%! String val; %>
> <html:form action="/full_profiloGiocoRespAutoesclusione">
>  <label>WANT TO DO THIS?</label> 
>  <% String d2 = "session"; %>
>  <logic:notEmpty name="MyFormBean" property="duration">
>   <bean:define id="d1" name="MyFormBean" property="duration"/>
>   <% d2 = d1.toString();%>
>  </logic:notEmpty>
> <fieldset>
>  <label for="question">Choose duration</label>
>  <html:select property="duration" name="MyFormBean">
>  <%
>   QuestionValues value;
>   String d = "session";
>   if (d2 != null) {
>     d = d2;
>   }
>   if ("day".equals(d)) {
>     value = QuestionValues.day;
>   } 
>   else if ("week".equals(d)) {
>     value = QuestionValues.week;
>   } else {
>     value = QuestionValues.session;
>   }
>   for (QuestionValues v : QuestionValues.values()) {
>     if (v.compareTo(value) >= 0) {
>       val = (String) (v.getLabel());
>   %>
>   <html:option value='<%=val%>'><%=val%></html:option>
>   <% }
>   } 
>   %>
>   </html:select>
>  </fieldset>
> </html:form>

With all those scriptlets, mixing of taglib and RT expression values, 
and the relevant Java code not included, it's a little difficult to see 
exactly what you're attempting.

I'm assuming MyFormBean.duration's type is a custom enumeration with 
with a getLabel() method, and that you want to use the label associated 
with each enum value as both the label and value of the select options? 
but you're using the enum value directly in the html:select?

It looks like it would be a lot cleaner and easier to handle the 
conversion to and from enum values in your action, and let the form bean 
and JSP deal with a simple list of Strings.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org