"Burns, Brendan" wrote:
>
> [...] But this brings up a question I have been struggling with:
> What is the better way of generating html, having the bean do it through a
> method or including it in the JSP.  I know that it is the intention of the
> JSP designers to seperate HTML from Java code and vis-versa but for example
> you have a bean which represents multiple choices that a user has made.  do
> you:
>
>         1) have a single method on the bean that returns the
> "<select>...</select>
>
>         or
>
>         2) put in the jsp
>         <select name=<jsp:expr>mySelectBean.getName()</jsp:expr>>
>         <% for(int i=0;i<mySelectBean.getNumberOfItems();i++) { %>
>                 <option><jsp:expr>mySelectBean.getName()</jsp:expr></option>
>         ....
>         </select>
>
>         now 1) seems to obfuscate some code from the HTML designer, but two
> seems pretty much like Java code in the JSP  which is better?  opinions?

This is definitely an area where you will find a lot of different opinions.
My opinion is that in in general it's better to generate HTML with JSP tags
or scriptlets using JavaBean property values, i.e. alternative 2) above.
This makes it possible to reuse a JavaBean that's just a "keeper of data",
and all layout decisions can be made by the person who designs the JSP page.

But in some cases it may make sense to use a Java object to generate complex
HTML. Take for instance a complex set of nested multi-row HTML tables used to
present some data object, with cells linked to other pages based on JavaBean
values etc. Generating this table using scriptlets may be pretty hard for
a non-programmer, so a JavaBean with a getHtml() method may be a better
choice. The HTML generating JavaBean can have a number of other properties
for setting things like background color, font size, etc. so the JSP page
designer still has some control over the look.

With JSP 1.1 custom tags you may be able to get the best of both worlds.
You could use a custom tag that generates an HTML table like this:

  <foo:fooHTMLTable source="someMultiRowBean" >
    <foo:rowPattern>
      <TD ALIGN=RIGHT>$colA$</TD>
      <TD>
        <TABLE>
          <TR>
            <TD>$colB$</TD>
          </TR>
          <TR>
            <TD>$colC$</TD>
          </TR>
        </TABLE>
      </TD>
    </foo:rowPattern>
  </foo:fooHTMLTable>

The custom tag takes care of looping over all rows and replaces the variables
in the pattern ($colA$, $colB$, $colC$) with the values of the corresponding
JavaBean property values for each row. So all the "tricky" code is done with
Java inside the custom tag implementation but the web page designer still has
control over the HTML code.

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to