Bill O'Keefe wrote:

> I am using a bean to initialize the form fields of a JSP.
> I understand how to use <jsp:getProperty...> to initialize text fields,
> or to just include a string within the page.  However I'm a bit unclear
> on the best way to initialize radio buttons and check boxes.  The only thing
> I've come up with is the following (somewhat ugly) solution:
>
>         <jsp:useBean id="mbean" class="..." scope="session"/>
>         ...
>         <input type="radio" name="choice" value="apparel"
>         <% if (mbean.getChoice() != null &&
> mbean.getChoice().equals("apparel"))
>                 out.println("checked"); %>
>         >Apparel
>
>         <input type="radio" name="choice" value="computers"
>         <% if (mbean.getChoice() != null &&
> mbean.getChoice().equals("computers"))
>                 out.println("checked"); %>
>         >Computers
>         ...
>
> Is there a more elegant way to accomplish this?  Thanks.
>

For radio buttons, I haven't discovered anything that is significantly nicer
than this.  However, an approach I'm taking on SELECT elements might be of
interest.  The basic idea is to add a read-only property to the bean itself
that returns a String containing the HTML to generate the select element.
Inside the bean's "get" method you can hide all the messy logic required to
build this, and then in your JSP page you just say:

    <% out.println(mbean.getMySelectElement() %>

to render the entire thing.  You might be able to apply the same principle by
having a pseudo-property that rendered the HTML for all of your radio buttons
and their options and prompts.

>
>     -- Bill
>

Craig McClanahan

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to