You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by un...@thorstenschaefer.de on 2007/01/02 21:36:50 UTC

How to reduce redundant form code in JSPs?

Hi,

My application consists of several forms, all following a common style. Each
form is rendered using a table. This results in the following code for each
field:

<tr>
 <td> <bean:message key="field.xxx"> </td>
 <td>
  <html:text property="xxx" readonly="${readonly}" /><br>
  <html:errors property="xxx" /><br>
 </td>
</tr>

The only variations are the name of the field ("xxx" in the snippet) as well as
the field type (in most cases html:text, sometimes html:checkbox). Is there a
way to reduce this redundant code?

Thanks for your help,

Thorsten


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


Re: How to reduce redundant form code in JSPs?

Posted by Martin Gainty <mg...@hotmail.com>.
gets the job done..
Alternatives?

M-
--------------------------------------------------------------------------- 
This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
--------------------------------------------------------------------------- 
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: "Leon Rosenberg" <ro...@googlemail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, January 02, 2007 6:15 PM
Subject: Re: How to reduce redundant form code in JSPs?


iiiiiiiiiiii scriptlets....
aren't we considering scriptlets evil? :-)

Leon

On 1/2/07, Martin Gainty <mg...@hotmail.com> wrote:
> conceptually you could pull bean:message key and input type when previously populating Bean properties using the example of LabelValueBean
>
> <%@ page import="java.util.*"%>
> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
> <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
> <%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form" %>
> <%@ taglib uri="/WEB-INF/jstl-core.tld" prefix="c" %>
> <%@ taglib uri="/WEB-INF/ragingtorrent.tld" prefix="ap" %>
> <%
> LinkedList hs=new LinkedList();
>
> for (int i=0;i<10;i++)
> {
>    if(i%2==0) hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"text");
>    else            hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"checkbox");
> }
>    pageContext.setAttribute("hs", hs, PageContext.PAGE_SCOPE);
> %>
>
> <%
>     for(int i=0;i<10;i++)
>     {
>  %>
>     <tr>
>     <td>
>       <bean:message key="field."+"prop{<%= ((LabelValueBean) hs.get(i)).label %>"
>     </td>
>     <td>
>      <input type="prop{<%= ((LabelValueBean) hs.get(i)).value %>"
>     </td>
>     </tr>
> <%
>   }
> %>
>
> Viel Gluck,
> Martin --
> ---------------------------------------------------------------------------
> This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
> ---------------------------------------------------------------------------
> Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
> ----- Original Message -----
> From: <un...@thorstenschaefer.de>
> To: <us...@struts.apache.org>
> Sent: Tuesday, January 02, 2007 3:36 PM
> Subject: How to reduce redundant form code in JSPs?
>
>
> > Hi,
> >
> > My application consists of several forms, all following a common style. Each
> > form is rendered using a table. This results in the following code for each
> > field:
> >
> > <tr>
> > <td> <bean:message key="field.xxx"> </td>
> > <td>
> >  <html:text property="xxx" readonly="${readonly}" /><br>
> >  <html:errors property="xxx" /><br>
> > </td>
> > </tr>
> >
> > The only variations are the name of the field ("xxx" in the snippet) as well as
> > the field type (in most cases html:text, sometimes html:checkbox). Is there a
> > way to reduce this redundant code?
> >
> > Thanks for your help,
> >
> > Thorsten
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >

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


Re: How to reduce redundant form code in JSPs?

Posted by Leon Rosenberg <ro...@googlemail.com>.
iiiiiiiiiiii scriptlets....
aren't we considering scriptlets evil? :-)

Leon

On 1/2/07, Martin Gainty <mg...@hotmail.com> wrote:
> conceptually you could pull bean:message key and input type when previously populating Bean properties using the example of LabelValueBean
>
> <%@ page import="java.util.*"%>
> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
> <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
> <%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form" %>
> <%@ taglib uri="/WEB-INF/jstl-core.tld" prefix="c" %>
> <%@ taglib uri="/WEB-INF/ragingtorrent.tld" prefix="ap" %>
> <%
> LinkedList hs=new LinkedList();
>
> for (int i=0;i<10;i++)
> {
>    if(i%2==0) hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"text");
>    else            hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"checkbox");
> }
>    pageContext.setAttribute("hs", hs, PageContext.PAGE_SCOPE);
> %>
>
> <%
>     for(int i=0;i<10;i++)
>     {
>  %>
>     <tr>
>     <td>
>       <bean:message key="field."+"prop{<%= ((LabelValueBean) hs.get(i)).label %>"
>     </td>
>     <td>
>      <input type="prop{<%= ((LabelValueBean) hs.get(i)).value %>"
>     </td>
>     </tr>
> <%
>   }
> %>
>
> Viel Gluck,
> Martin --
> ---------------------------------------------------------------------------
> This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
> ---------------------------------------------------------------------------
> Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
> ----- Original Message -----
> From: <un...@thorstenschaefer.de>
> To: <us...@struts.apache.org>
> Sent: Tuesday, January 02, 2007 3:36 PM
> Subject: How to reduce redundant form code in JSPs?
>
>
> > Hi,
> >
> > My application consists of several forms, all following a common style. Each
> > form is rendered using a table. This results in the following code for each
> > field:
> >
> > <tr>
> > <td> <bean:message key="field.xxx"> </td>
> > <td>
> >  <html:text property="xxx" readonly="${readonly}" /><br>
> >  <html:errors property="xxx" /><br>
> > </td>
> > </tr>
> >
> > The only variations are the name of the field ("xxx" in the snippet) as well as
> > the field type (in most cases html:text, sometimes html:checkbox). Is there a
> > way to reduce this redundant code?
> >
> > Thanks for your help,
> >
> > Thorsten
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >

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


Re: How to reduce redundant form code in JSPs?

Posted by Martin Gainty <mg...@hotmail.com>.
conceptually you could pull bean:message key and input type when previously populating Bean properties using the example of LabelValueBean

<%@ page import="java.util.*"%> 
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> 
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> 
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %> 
<%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form" %> 
<%@ taglib uri="/WEB-INF/jstl-core.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/ragingtorrent.tld" prefix="ap" %> 
<% 
LinkedList hs=new LinkedList(); 

for (int i=0;i<10;i++)
{    
   if(i%2==0) hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"text"); 
   else            hs.add(new org.apache.struts.util.LabelValueBean(new Integer(i).toString(),"checkbox"); 
}
   pageContext.setAttribute("hs", hs, PageContext.PAGE_SCOPE); 
%> 

<% 
    for(int i=0;i<10;i++)
    {
 %>
    <tr>
    <td>
      <bean:message key="field."+"prop{<%= ((LabelValueBean) hs.get(i)).label %>"
    </td>
    <td>
     <input type="prop{<%= ((LabelValueBean) hs.get(i)).value %>"
    </td>
    </tr>
<%
  }
%> 

Viel Gluck,
Martin --
--------------------------------------------------------------------------- 
This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
--------------------------------------------------------------------------- 
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: <un...@thorstenschaefer.de>
To: <us...@struts.apache.org>
Sent: Tuesday, January 02, 2007 3:36 PM
Subject: How to reduce redundant form code in JSPs?


> Hi,
> 
> My application consists of several forms, all following a common style. Each
> form is rendered using a table. This results in the following code for each
> field:
> 
> <tr>
> <td> <bean:message key="field.xxx"> </td>
> <td>
>  <html:text property="xxx" readonly="${readonly}" /><br>
>  <html:errors property="xxx" /><br>
> </td>
> </tr>
> 
> The only variations are the name of the field ("xxx" in the snippet) as well as
> the field type (in most cases html:text, sometimes html:checkbox). Is there a
> way to reduce this redundant code?
> 
> Thanks for your help,
> 
> Thorsten
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

Re: How to reduce redundant form code in JSPs?

Posted by Van <va...@gmail.com>.
On 1/2/07, Laurie Harper <la...@holoweb.net> wrote:
> uni@thorstenschaefer.de wrote:
>
> - if you're using Struts 2, you could create a Struts 2 component / tag
> to handle this

Not using S2 yet, but, WW 2.2 that S2 is based on has tag themes for
the form tags. You can write your own theme extensions of base form
tags to follow your convention for form field layout. I'd be surprised
if that capability was lost in the merger. You can look at the xhtml
theme as an example. It might even do something close enough to what
you want already. I seem to recall that it layed out form fields as
table rows with label in one column and field in adjacent column of
same table row.

-Van

-- 
- Mike "Van" Riper
  van.riper@gmail.com

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


Re: How to reduce redundant form code in JSPs?

Posted by un...@thorstenschaefer.de.
Hi,

> There are lots of ways :-) You didn't list what technologies you're
> using, but here are some possibilities:
>
> - if you're using JSP 2.0, you could create a tag file to represent each
> type of form field and encapsulate the common markup there
>
> - you could write a JSP Custom Tag to emit the markup (though I don't
> recommend that, since altering the markup then involves recompiling)
>
> - if you're using Tiles, you could create a tile definition to
> encapsulate the common markup
>
> - if you're using Struts 2, you could create a Struts 2 component / tag
> to handle this
>
> - you could use a plain ol' jsp:include tag or @include directive
Im using Struts 1 without tiles. I took a look at all the options and finally
created a JSP2 tag file - this approach is great because you don't need to
recompile code in case of changes and the designer can change the layout by
only modifying the tag file (basically a JSP fragment), without having to
modify code.

Thanks for your help,

Thorsten


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


Re: How to reduce redundant form code in JSPs?

Posted by Laurie Harper <la...@holoweb.net>.
uni@thorstenschaefer.de wrote:
> Hi,
> 
> My application consists of several forms, all following a common style. Each
> form is rendered using a table. This results in the following code for each
> field:
> 
> <tr>
>  <td> <bean:message key="field.xxx"> </td>
>  <td>
>   <html:text property="xxx" readonly="${readonly}" /><br>
>   <html:errors property="xxx" /><br>
>  </td>
> </tr>
> 
> The only variations are the name of the field ("xxx" in the snippet) as well as
> the field type (in most cases html:text, sometimes html:checkbox). Is there a
> way to reduce this redundant code?

There are lots of ways :-) You didn't list what technologies you're 
using, but here are some possibilities:

- if you're using JSP 2.0, you could create a tag file to represent each 
type of form field and encapsulate the common markup there

- you could write a JSP Custom Tag to emit the markup (though I don't 
recommend that, since altering the markup then involves recompiling)

- if you're using Tiles, you could create a tile definition to 
encapsulate the common markup

- if you're using Struts 2, you could create a Struts 2 component / tag 
to handle this

- you could use a plain ol' jsp:include tag or @include directive

There are probably other options I'm not thinking of, but any of those 
should work fine.

L.


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