struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Struts taglib
Date Thu, 13 Jul 2000 16:59:34 GMT
Hi Pasi ... see embedded comments below.

Pasi Salminen wrote:

> Hi,
>
> We've been investigating struts for a while, and we have couple of
> questions/feature requests to it. I apologize if somebody has already
> asked about these things (it took too long to browse the database to
> search it exhaustively).
>
> The form tag uses name and type attributes to identify the action form
> to work with. However, if I have two different forms in the same page,
> but only one action form instance I seem to have a problem. This could
> be resolved by adding optional actionFormAttribute attribute to form tag
> (which defaults to name if not given).
>

If you have two different forms on the page, aren't you using a different
<struts:form> tag for each of them?  There is no restriction about having
multiple forms on the same page, although none of the examples take
advantage of this.

>
> In addition, use of non-action form beans should be easy as pie. This
> could be achieved by adding beanId (or beanName of whatever) attribute
> to base tag implementation (BaseInputTag?) implementation. The idea is
> shown below.
>
> And the type attribute is something I don't feel very comfortable with;
> it's exposing my implementation class of the action, which should be
> hidden in the action.xml.

This is an issue for <jsp:useBean> also, for the same reason.

In both cases, the type is necessary because the tag supports the option of
creating a new bean instance for you, if there is not one there already.  In
the case of Struts, most of the time the bean will have been there for you
-- but if the user types in the URL of your form page first, without going
through the normal navigation of your application, it won't be there.

One approach would be to make the "type" attribute optional on
<struts:form>.  If the bean is missing and type is not present, all we can
do is throw a JspException, which is not very pretty.


> So, could this be resolved by adding
> ActionForms collection to servlet context and using it to resolve name
> of the implementation class? The (partial) tag implementation could look
> like this:
>
> Object bean = null;
>
> // If action form bean is requested
> if( _beanId == null && _actionFormAttribute != null )
> {
>       bean = session.getAttribute( _actionFormAttribute );
>       // No action form bean bound to session
>       if( bean == null )
>      {
>           ActionForms forms;
>
>           forms  = ( ActionForms ) getServletContext( ).getAttribute(
> Action.FORMS_KEY" );
>           // Create it and put it into session
>           bean = forms.createFormInstanceByName( _actionFormAttribute );
>
>           session.setAttribute( bean );
>      }
> }
> // Was non-action form bean requested?
> else  if( _beanId != null )
> {
>     bean = getPageContext( ).findAttribute( _beanId );
> }
>
> // Do reflection to get the property out of bean
> ...
>
> And then we do like this:
>
> <strutsex:form name="adduserfrm" actionFormAttribute="MainForm"
> action="/add-user.do">
>     <!-- Use normal bean (since beanId is given) -->
>     <strutsex:text property="userName" beanId="app.user"/>
> </strutsex:form>
>
> <strutsex:form name="adduserfrm" actionFormAttribute="MainForm"
> action="/add-user.do">
>     <!-- Use action form (no beanId given) -->
>     <strutsex:text property="user"/>
> </strutsex:form>
>
> Does this make any sense?
>

In recent nightly builds of Struts, the action mappings collection (and the
global action forwards collection) actually are being published as servlet
context attributes.  It would therefore be possible to look up the mapping
we are working with based on the "action" attribute given to the form tag.
I will look into this.

Hmmm, this might make it easier to specify things like required field
validations in the "action.xml" file, and have them generated automatically
as appropriate, rather than making you specify them in the field tags
themselves .....

The idea of letting you reference more than one bean in the field tags has
been requested before.  For consistency with attribute names elsewhere, it
might make more sense to have an optional "name" attribute on the text tag,
for example, instead of beanId.  The default would be the bean identified by
the "name" attribute of the "form" element.  Does that sound reasonable?

So, as a summary example, you would end up with:

    <struts:form name="adduserfrm" action="/add-user.do">
        <!-- Form bean class looked up from the mapping if needed -->

        <!-- The "username" property is in the "adduserfrm" bean -->
        <struts:text property="username"/>

        <!-- The "password" property is in some other bean -->
        <struts:text name="otherBean" property="password"/>

    </struts:form>

>
> Greetings,
>

> Paci

Craig



Mime
View raw message