On Thu, April 6, 2006 3:18 am, Madhav Bhargava said:
> I do not want to put any Html generating logic inside my action class. Is
> there any other elegant way in which i can generate the HTML for the drop
> down.
>
> I can very well write different methods to generate the HTML code and then
> call these methods from within my action class. But somehow this does not
> look very elegant to me.

There sure is: you can let a JSP do it!

Remember that a JSP can actually render anything you want.  It doesn't
have to be HTML.  But, in your case, it probably would be.  However, it
still doesn't have to be a full HTML document, it can be just a snippet of
markup.

For instance, let's say you have an Action that populates a List, where
each item in the list is a Map with two elements, optionValue and
optionText.  Create one Map for each <option> in your select.  Then, stick
that list in request as an attribute under the name "myOptions".  Then
forward to a JSP.

The JSP, in simplest terms, can just be this:

<select name="mySelect">
<%
  List options = (List)request.getAttribute("myOptions");
  for (Iterator it = options.iterator(); it.hasNext();) {
    Map m = (Map)it.next();
%>
    <option
value="<%=m.get("optionValue")%>"><%=m.get("optionText")%></option>
<%
  }
%>
</select>

Of course, you would probably want to use JSTL or Struts tags to do it,
but you get the idea.

Then, since you made this request via AJAX, you will have some callback
function on the client.  This callback function simply takes the response
and inserts it into a <div> that was surrounding the <select> that you
want to update.  That's the basic theory.

You can of course do DOM manipulation instead of just updating a <div> via
innerHTML, and you can of course find a library that takes care of the
details for you (AjaxTags in Java Web Parts for instance handler
specifically for populating <select>s, and it accepts XML, which you can
generate in the JSP just as easily... other libraries have similar
functionality), but the basics are pretty much these.

As another poster pointed out, there is indeed an example of doing this in
the JWP cookbook, and most other libraries will probably have a similar
example, since this is such a common thing to do.

> Any help will be appreciated.
>
> --Madhav

Frank

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to