You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Van Wart <sc...@indosoft.ca> on 2006/06/09 01:59:47 UTC

Variable 'input' attribute in struts-config.xml

I have two different pages that call the same action.  I'm using 
validate="true" in the action mapping.  Can I specify the 'input' 
attribute dynamically (or set it somewhere while the action is being 
called, before validation?).

Thanks,
  Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Scott Van Wart <sc...@indosoft.ca>.
Niall Pemberton wrote:
> 1) Do the "out of the box" falvours cater for your needs? There are 2
> - using the form name as the validation key, or using the action
> mapping.
Using the action mapping _might_ work, ... right now I'm doing this:

        <action path="/entity"
            type="mypkg.EntityAction"
            name="entityForm"
            scope="request"
            validate="false"
            parameter="action">
            <forward name="create" path="page.admin.createEntity" />
            <forward name="edit" path="page.admin.editEntity" />
        </action>

But then I'd still need to call the validate() myself, and I'm not sure 
how to specify mappings in the validations.xml file (if that's what you 
were getting at).

> 2) If the 2 standard flavours don't suit your needs, then you could
> create your own flavour by having a custom validator form
> implementation and overriding the getValidationKey() method:
>
>  public FooValidatorForm extends ValidatorForm {
>    public String getValidationKey(ActionMapping mapping,
>        HttpServletRequest request) {
>        return ... // your own key
>    }
>  }
This might work... I'd just need to be careful not to use 
static/instance variables.

- Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Niall Pemberton <ni...@gmail.com>.
On 6/10/06, Scott Van Wart <sc...@indosoft.ca> wrote:
> Dave Newton wrote:
> > So I still keep all my simple, non-business-logic validations in the
> > validation config file.  Just call validate on the form: if it's a ValidatorForm or
> > ValidatorActionForm or subclass (or Dynas) it just works.
> >
> OK, I've no problem caling validate() from the DispatchAction, but the
> validation rules are different depending on which action gets called.
> Is there a way to split up the validation rules for a form in the
> validation.xml, and pass a parameter to determine which one to invoke
> when I call validate()?

This is very easy, depending on what you want:

1) Do the "out of the box" falvours cater for your needs? There are 2
- using the form name as the validation key, or using the action
mapping.

2) If the 2 standard flavours don't suit your needs, then you could
create your own flavour by having a custom validator form
implementation and overriding the getValidationKey() method:

  public FooValidatorForm extends ValidatorForm {
    public String getValidationKey(ActionMapping mapping,
        HttpServletRequest request) {
        return ... // your own key
    }
  }

3) If you can't determine the key to use from the form/request/mapping
- then what Struts does to invoke the Validator framework is not very
complex - a few lines in your Action (create an ActionMessages object,
initialize a Validator and call the Validator.validate() method) - see
the ValidatorForm's validate() method to see how to do this. This way
you can pass it whatever key to use for the validation rules (i.e Form
in your validation.xml)

Niall

> - Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Scott Van Wart <sc...@indosoft.ca>.
Dave Newton wrote:
> So I still keep all my simple, non-business-logic validations in the
> validation config file.  Just call validate on the form: if it's a ValidatorForm or
> ValidatorActionForm or subclass (or Dynas) it just works.
>   
OK, I've no problem caling validate() from the DispatchAction, but the 
validation rules are different depending on which action gets called.  
Is there a way to split up the validation rules for a form in the 
validation.xml, and pass a parameter to determine which one to invoke 
when I call validate()?

- Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Dave Newton <ne...@pingsite.com>.
Scott Van Wart wrote:
> Right, but the difficulty here is that the validation then has to be
> done in the action, and I can't rely on the 'validate="true"' to use
> ValidatorForm's validate() method and dump my simple,
> non-database-reliant validations into validations.xml.

Many of us _never_ use the automagic validation.

For example, I used to split all my Actions into GET and POST handlers
(I almost always submit forms with POST). So my base Action's execute
method basically checks the request type. If it's a GET, just go to the
input. If it's a POST, call the form's validate method. If errors, go to
input, else process the post.

So I still keep all my simple, non-business-logic validations in the
validation config file.

> Anyways, what I may do is some kind of hybrid form/validation where
> the action validates the form and determines the appropriate forward
> on failure.  My only issue is the one mentioned above, where I'm not
> sure how to hook this into the validator mechanism.

Just call validate on the form: if it's a ValidatorForm or
ValidatorActionForm or subclass (or Dynas) it just works.

Dave



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


Re: Variable 'input' attribute in struts-config.xml

Posted by Scott Van Wart <sc...@indosoft.ca>.
Michael Jouravlev wrote:
> After you filled in the form, you call EntityAction with "store"
> command. It validates the input and then tries to update/store data.
> If input is not valid, the action checks the state field and forward
> to a JSP that corresponds to that state. The page will be displayed
> along with the errors. If data is saved successfully, you forward to
> your success page, which is probably the item list. Same with
> deletion, after "delete" event is served, you forward to success page.

Right, but the difficulty here is that the validation then has to be 
done in the action, and I can't rely on the 'validate="true"' to use 
ValidatorForm's validate() method and dump my simple, 
non-database-reliant validations into validations.xml.

Here's what I'm doing for all my entities (things are bound to get more 
complex, as for now I'm just doing an "admin" module, which is pretty 
isolated against everything else, but I'll cross those bridges once I 
get to them):

I have a class called EntityAction (extends DispatchAction).  It has the 
following actions:
    - newEntity: Clears the request form and forwards to create_entity.jsp
    - createEntity: Creates the entity in the database and forwards to 
edit_entity.jsp
    - updateEntity: Updates the entity in the database and forwards to 
edit_entity.jsp
    - retrieveEntity: Gets the entity out of the database, populates the 
form and forwards to edit_entity.jsp
    - deleteEntity: Deletes the entity from the database, and forwards 
to create_entity.jsp

This is far, far cleaner than the approach I was using before I read 
these e-mails :).  Additionally, I use exactly one form for this process.

A little trickery starts to appear when I have a child entity.  Now I 
have to make the following modifications:

    - newChild, createChild: Needs to redirect to parents.jsp if no 
parent key is supplied
    - updateChild, retrieveChild, deleteChild: Needs to redirect to 
parents.jsp if child key not supplied or found

I also need to accommodate for a valid child key being specified without 
a parent key, as I can correct this by populating the parent key from 
the database.  Fun stuff :).

Anyways, what I may do is some kind of hybrid form/validation where the 
action validates the form and determines the appropriate forward on 
failure.  My only issue is the one mentioned above, where I'm not sure 
how to hook this into the validator mechanism.

I love this stuff (especially the whole "find the best way to do 
something you can already technically do," I just wish I knew more about 
it...

Thanks!

- Scott


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


Re: Variable 'input' attribute in struts-config.xml

Posted by Michael Jouravlev <jm...@gmail.com>.
Hi Scott,

this is how I would implemented it. What we have is a standard CRUD
stuff for Entity object. There are two ways to navigate to Entity.
Either you simply navigate to it and it renders itself according to
its state, or you navigate to it with a certain command, like "view"
or "update" or "delete" or "create". The latter way is simpler and
occurs more often and is easiter to understand and implement, so let's
discuss that one.

Say, you have a table with a rows, each row represents an Entity. You
may click the row to pull Entity out. Say, each row has links like
"view", "update" or "delete". There is also one link or button called
"create". To view, update or delete an entity, you call EntityAction
with a corresponding command along with entity ID. To create an entity
you call EntityAction without ID. See, EntityAction is a perfect
candidate to be implemented with dispatch action, I suggest
EventDispatchAction.

If you call EntityAction with "view" command, the action loads
existing entity and forwards to "view_entity.jsp". I prefer to use
session-scoped formbeans so I set Entity state in the form to "view".
If you prefer to use request-scoped beans, you can save Entity state
in the hidden field in "view_entity.jsp".

Same thing with "edit" command, the action loads existing entity, sets
state to "edit" and forwards to "edit_entity.jsp". To create Entity,
the action initializes formbean, sets state to "create" and forwards
to "create_entity.jsp".

In fact, all these JSP pages may be actually one page, you just need
to update the title and maybe to show/hide ID field.

After you filled in the form, you call EntityAction with "store"
command. It validates the input and then tries to update/store data.
If input is not valid, the action checks the state field and forward
to a JSP that corresponds to that state. The page will be displayed
along with the errors. If data is saved successfully, you forward to
your success page, which is probably the item list. Same with
deletion, after "delete" event is served, you forward to success page.

So, the only problem here is what happens if a user navigates to
EntityAction without command parameter? This is up to you. If you use
dispatch action, you would correlate commands with action methods. If
no command is passed to the action, the execute() will be called
instead of a specific method handler. Because there is no active
Entity, you can either show "No entity" message or forward to some
error page.

If you prefer to use redirection instead of forwarding, then you would
have to either save state on the server (this is what I do), or to
pass all state info in redirected request. A combination of these
approaches is to pass Entity ID and maybe mode ID (like "updating" or
"creating") in redirected request, and to store everything else in the
session.

As you may see, one dispatch action, one form bean and maybe even one
JSP page is enough to serve all your commands.

Michael.

On 6/8/06, Scott Van Wart <sc...@indosoft.ca> wrote:
> Michael Jouravlev wrote:
> > I suggest to look at your issue from another perspective. Do you
> > really need the functionality you are asking for? First, a small
> > clarification. There is no input page for an action, "input" attribute
> > is poorly named, it should be called "error" or "errorTarget", because
> > Struts forwards to that location if input data does not validate. Your
> > request is always sent to action/actionform.
> That might be it then... it might just be a matter of having to separate
> things out a little more.  So let's say I had a page that edits an
> entity.  I might call this edit_entity.jsp, with an EntityForm, and a
> SaveEntityAction.  I found things got a little cumbersome in the .jsp,
> so I separated it out into a create_entity.jsp and edit_entity.jsp.
> These two forms submit to the same action.  Obviously, they have a lot
> of code in common, but I thought of factoring that out into tags.
> Perhaps I should do the same thing to the action class
> (CreateEntityAction and UpdateEntityAction rather than all-encompassing
> SaveEntityAction; any code duplication can go into base classes or
> utility classes).
>
> The other side of this is that I also have a ShowEntityForm and
> ShowEntityAction.  The action is mapped to /showEntity, and there are
> three forward mappings from there.  Two are "success"-like forwards
> called "create" and "edit", which forward to create_entity.jsp and
> edit_entity.jsp.  The 3rd one, the "failure" forward's destination would
> depend on who called /showEntity in the first place.  The ShowEntityForm
> has validation, to ensure the given entityId is a number, and the
> "failure" forward gets hit when the entityId can't be found in the
> database table.  While I can break the "failure" out into multiple
> forwards, I can't do the same with input.
>
> So the question is, then, do I break up my ShowEntityXxxxx classes into
> things like NewEntityAction->create_entity.jsp->CreateEntityAction and
> EditEntityAction->edit_entity.jsp->UpdateEntityAction?  It's starting to
> sound like a lot more classes, but if I'm on the right track, and if
> that's how Struts is designed, then perhaps that's the point.
>
> Or I could be completely off my rocker.
>
> :)
>
> - Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Michael Jouravlev <jm...@gmail.com>.
What I would do is having an AvailableItems web resource with two
states: "no items" (default) and "items found". For "no items" it
would render a search page, for "items found" it would render items
list.

Michael.

On 6/9/06, Albert L. Sapp <as...@uiuc.edu> wrote:
> Scott,
>
> I think your approach, if I understand it right, should be fine.  In our
> application, we start with a query page that allows entry of data for a
> query or has a link to create a new object.  If they enter query
> information we go to a query action which generates a results jsp or
> returns them to the query jsp if no matchs are found.  On the results
> page on each line displaying a item, we have 3 links that either show
> them the details for the item, allow them to update the item or delete
> the item.  Links are displayed based on the role that the user might have.

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


Re: Variable 'input' attribute in struts-config.xml

Posted by "Albert L. Sapp" <as...@uiuc.edu>.
Scott Van Wart wrote:

> Michael Jouravlev wrote:
>
>> I suggest to look at your issue from another perspective. Do you
>> really need the functionality you are asking for? First, a small
>> clarification. There is no input page for an action, "input" attribute
>> is poorly named, it should be called "error" or "errorTarget", because
>> Struts forwards to that location if input data does not validate. Your
>> request is always sent to action/actionform.
>
> That might be it then... it might just be a matter of having to 
> separate things out a little more.  So let's say I had a page that 
> edits an entity.  I might call this edit_entity.jsp, with an 
> EntityForm, and a SaveEntityAction.  I found things got a little 
> cumbersome in the .jsp, so I separated it out into a create_entity.jsp 
> and edit_entity.jsp.  These two forms submit to the same action.  
> Obviously, they have a lot of code in common, but I thought of 
> factoring that out into tags.  Perhaps I should do the same thing to 
> the action class (CreateEntityAction and UpdateEntityAction rather 
> than all-encompassing SaveEntityAction; any code duplication can go 
> into base classes or utility classes).
>
> The other side of this is that I also have a ShowEntityForm and 
> ShowEntityAction.  The action is mapped to /showEntity, and there are 
> three forward mappings from there.  Two are "success"-like forwards 
> called "create" and "edit", which forward to create_entity.jsp and 
> edit_entity.jsp.  The 3rd one, the "failure" forward's destination 
> would depend on who called /showEntity in the first place.  The 
> ShowEntityForm has validation, to ensure the given entityId is a 
> number, and the "failure" forward gets hit when the entityId can't be 
> found in the database table.  While I can break the "failure" out into 
> multiple forwards, I can't do the same with input.
>
> So the question is, then, do I break up my ShowEntityXxxxx classes 
> into things like 
> NewEntityAction->create_entity.jsp->CreateEntityAction and 
> EditEntityAction->edit_entity.jsp->UpdateEntityAction?  It's starting 
> to sound like a lot more classes, but if I'm on the right track, and 
> if that's how Struts is designed, then perhaps that's the point.
>
> Or I could be completely off my rocker.
>
> :)
>
> - Scott
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Scott,

I think your approach, if I understand it right, should be fine.  In our 
application, we start with a query page that allows entry of data for a 
query or has a link to create a new object.  If they enter query 
information we go to a query action which generates a results jsp or 
returns them to the query jsp if no matchs are found.  On the results 
page on each line displaying a item, we have 3 links that either show 
them the details for the item, allow them to update the item or delete 
the item.  Links are displayed based on the role that the user might have.

HTH,

Al


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


Re: Variable 'input' attribute in struts-config.xml

Posted by Scott Van Wart <sc...@indosoft.ca>.
Michael Jouravlev wrote:
> I suggest to look at your issue from another perspective. Do you
> really need the functionality you are asking for? First, a small
> clarification. There is no input page for an action, "input" attribute
> is poorly named, it should be called "error" or "errorTarget", because
> Struts forwards to that location if input data does not validate. Your
> request is always sent to action/actionform.
That might be it then... it might just be a matter of having to separate 
things out a little more.  So let's say I had a page that edits an 
entity.  I might call this edit_entity.jsp, with an EntityForm, and a 
SaveEntityAction.  I found things got a little cumbersome in the .jsp, 
so I separated it out into a create_entity.jsp and edit_entity.jsp.  
These two forms submit to the same action.  Obviously, they have a lot 
of code in common, but I thought of factoring that out into tags.  
Perhaps I should do the same thing to the action class 
(CreateEntityAction and UpdateEntityAction rather than all-encompassing 
SaveEntityAction; any code duplication can go into base classes or 
utility classes).

The other side of this is that I also have a ShowEntityForm and 
ShowEntityAction.  The action is mapped to /showEntity, and there are 
three forward mappings from there.  Two are "success"-like forwards 
called "create" and "edit", which forward to create_entity.jsp and 
edit_entity.jsp.  The 3rd one, the "failure" forward's destination would 
depend on who called /showEntity in the first place.  The ShowEntityForm 
has validation, to ensure the given entityId is a number, and the 
"failure" forward gets hit when the entityId can't be found in the 
database table.  While I can break the "failure" out into multiple 
forwards, I can't do the same with input.

So the question is, then, do I break up my ShowEntityXxxxx classes into 
things like NewEntityAction->create_entity.jsp->CreateEntityAction and 
EditEntityAction->edit_entity.jsp->UpdateEntityAction?  It's starting to 
sound like a lot more classes, but if I'm on the right track, and if 
that's how Struts is designed, then perhaps that's the point.

Or I could be completely off my rocker.

:)

- Scott

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


Re: Variable 'input' attribute in struts-config.xml

Posted by Michael Jouravlev <jm...@gmail.com>.
On 6/8/06, Scott Van Wart <sc...@indosoft.ca> wrote:
> I have two different pages that call the same action.  I'm using
> validate="true" in the action mapping.  Can I specify the 'input'
> attribute dynamically (or set it somewhere while the action is being
> called, before validation?).

You cannot change config information that has been defined in
struts-config.xml file. If you try, you will likely get "The
configuration is frozen" exception.

You may want to try using wildcards [1], but it looks that wildcards
are not the answer to your question.

I suggest to look at your issue from another perspective. Do you
really need the functionality you are asking for? First, a small
clarification. There is no input page for an action, "input" attribute
is poorly named, it should be called "error" or "errorTarget", because
Struts forwards to that location if input data does not validate. Your
request is always sent to action/actionform.

It appears to me that you have two different HTML forms that are
submitted to one action. After many years of using Struts this
page-oriented approach seems convoluted and inflexible to me. If these
forms are different views of one web resource, they should be rendered
by one action, and a proper page is chosen depending on resource
state. If these forms are views of different resources, then they
should be served by different action classes belonging to different
web resources, that is, you simply should not submit to one action
class from views that represent different web resources.

So, to display the first form you call FirstAction and tell it to
render itself. It shows the form, you fill it out, submit, in case of
error it redisplays the form. Do display the second form you call
SecondAction and tell it to render itself. It shows the form, you fill
it out, submit, in case of error it redisplays the form. :-) This is
it, all you need is two actions, not two "input" attributes.

If you have time, the DataEntryForm page [2] explains my opinion on that matter.

[1] http://struts.apache.org/struts-action/userGuide/building_controller.html#action_mapping_wildcards
[2] http://wiki.apache.org/struts/DataEntryForm

Michael.

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


RE: Variable 'input' attribute in struts-config.xml

Posted by Ralf Stoltze <ra...@2meter3.de>.
hi.

> I have two different pages that call the same action.  I'm 
> using validate="true" in the action mapping.  Can I specify 
> the 'input' 
> attribute dynamically (or set it somewhere while the action 
> is being called, before validation?).

never tried, but ...

your action's execute method gives you an ActionMapping object which can
call setInput(String input).

you have to set validate="false" in order to give control to the action
before validation starts. you can then manually call
form.validate(mapping, request) (+ save errors into request, + return
inputForward ). 


hth, ralf


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