You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by vo...@magiccode.eu on 2008/04/23 17:18:05 UTC

Validation on methods

Hello y'all,

i have quite an interesting problem again.
In my struts.xml i have defined a method to call for my action
(method="save")

Every time my action class is called, the method validate() is executed.
The same action class used with struts.xml-parameter method="save" is also executed as expected, but validate is never called.
I thought, i could exclude methods from validation, but how do i include one ?

this
<interceptor-ref name="validation">
  <param name="includeMethods">save</param>
</interceptor-ref>			

does not work.

thanks in advance

  Volker

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


Re: once again: validate() not called

Posted by Laurie Harper <la...@holoweb.net>.
Volker Karlmeier wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hello struts freaks,
> 
> i have quite an interesting problem again. In my struts.xml i have 
> defined a method to call for my action (method="save")
> 
> Every time execute() is called in my action class, the method validate() 
> is executed before.
> The same action class used with struts.xml-parameter method="save" is 
> also executed as expected,
> but validate is never called before the method save.
> 
> I think the intention of the interfaces Validatable/ValidationAware is 
> that validate() should be called before the
> action is executed. This should work using execute() as well as any 
> other method configured in struts.xml with the
> parameter method="<method to call>".

Perhaps you didn't see my previous reply requesting more information:

http://www.nabble.com/Re%3A-Validation-on-methods-p16886534.html

> <interceptor-ref name="validation">
> ~  <param name="includeMethods">save</param>
> </interceptor-ref>           
> does not work.

That's because it's the workflow interceptor, not the validation 
interceptor, which calls validate().

L.


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


once again: validate() not called

Posted by Volker Karlmeier <vo...@magiccode.eu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello struts freaks,

i have quite an interesting problem again. In my struts.xml i have 
defined a method to call for my action (method="save")

Every time execute() is called in my action class, the method validate() 
is executed before.
The same action class used with struts.xml-parameter method="save" is 
also executed as expected,
but validate is never called before the method save.

I think the intention of the interfaces Validatable/ValidationAware is 
that validate() should be called before the
action is executed. This should work using execute() as well as any 
other method configured in struts.xml with the
parameter method="<method to call>".


<interceptor-ref name="validation">
~  <param name="includeMethods">save</param>
</interceptor-ref>            

does not work.

Any suggestions ? Thanks in advance

~  Volker
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFIF5lYaEMQXBIqssERAvT9AJ4iSJRWu8MLlRt7V0+AApriy7K9OQCgj2js
0Zmn6+7SzBDSZ2nzB/s5gww=
=GPw3
-----END PGP SIGNATURE-----


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


Re: Validation on methods

Posted by Laurie Harper <la...@holoweb.net>.
The validate() method should be called for any request on that action 
unless (a) the request is serviced by an excluded method (where the set 
of excluded methods depends on the interceptor stack you are using 
and/or an explicit configuration you've specified) or (b) you have 
switched the 'allways execute validate()' behaviour off.

Please post all the relevant parts of your struts.xml and other 
configuration, as well as the precise URL you are requesting that 
results in validate() not being called.

L.

Volker Karlmeier wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>  
> Hello Márcio*,
> 
> *thanks for your quick answer. Yes, that will work, you're right.
> Unfortunately is is not exactly what i would want. You are doing your
> validation in the method that is called.
> I would prefer the framework to call *validate()* before (!) my method
> (in that case "save" ) is called.
> This should happen when I subclass from ActionSupport and implement
> validate(), but it only seems to work
> when the method execute() is called.
> 
> I could call validate() from the method save in it's beginning (that's
> in fact what I do now), but that not the intention
> of the interfaces Validatable/ValidationAware. So I hope, anyone can
> give me a hint as I like to write clean, readable
> code and do not want to create "workarounds" all the time ;)
> 
> Anyway, thanks for the effort.
> 
>   Volker
> 
> *
> *Márcio Gurgel schrieb:
>> Hi Volker,
>>
>> You can do it with struts xml validator:
>>
>> Include a xml into your action package like this:
>>
>> ActionName - alias from action -  validation.xml
>> Example:
>> ActionName-save-validation.xml
>>
>> * Note that "save" is the name of action in your struts.xml
>>
>> Nothing better than a real example:
>>
>> struts.xml -------------------------------------------------------
>> <action name="salvar" method="salvar"
>>             class="br.com.sgvdba.actions.usu.UsuAction">
>>             <result name="success" type="redirectAction">
>>                 <param name="idUsu">${currentUsu.cd}</param>
>>                 <param name="actionName">abrirDados</param>
>>                 <param name="namespace">/usuario</param>
>>             </result>
>>             <result type="tiles" name="input">default.usu</result>
>>         </action>
>>
>> UsuAction (Action class) -----------------------------------------
>> /**
>>      * Persiste determinado usuário no banco de dados
>>      * *     Para inserir um usuário deve ser confirmada a não-existência
>>      *         de outro registro com o mesmo nome de usuário.
>>      * @return
>>      */
>>     public String salvar(){
>>         //Verifica se a tela possui erros
>>         if (hasFieldErrors() || hasErrors()){
>>             return Action.INPUT;
>>         }
>>
>>         try {
>>             if (!getUsuFacade().isSenhaValid(currentUsu.getSenha(),
>> senhaConf)){
>>                 addActionError("As senhas informadas devem ser iguais");
>>                 return Action.INPUT;
>>             }
>>             else
>>                 if (!getUsuFacade().isChvValid(currentUsu.getChv())){
>>                     addActionError("Já existe um usuário com este nome,
>> favor informar um nome diferente");
>>                     return Action.INPUT;
>>             }
>>             else {
>>                 NvlAcso nvlAcso =
>> getNvlAcsoFacade().recuperaPorId(idNvlAcso);
>>                 getUsuFacade().incluir(currentUsu, nvlAcso);
>>                 setIdUsu(currentUsu.getCd());
>>             }
>>
>>         } catch (Exception e) {
>>             addActionError(SgvdbaConstants.ERRO_DEFAULT + e.getMessage());
>>             return Action.INPUT;
>>         }
>>
>>         return Action.SUCCESS;
>>     }
>>
>> UsuAction-salvar-validation.xml (VALIDATION)
>> -------------------------------------------------------
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
> 1.0//EN"
>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
>>     <validators>
>>
>>         <field name="currentUsu.chv">
>>             <field-validator type="requiredstring">
>>                 <message>Favor inserir um nome de usuário</message>
>>             </field-validator>
>>         </field>
>>
>>         <field name="currentUsu.email">
>>             <field-validator type="requiredstring">
>>                 <message>Favor inserir um e-mail</message>
>>             </field-validator>
>>             <field-validator type="email">
>>                 <message>Favor informar um e-mail válido</message>
>>             </field-validator>
>>         </field>
>>
>>         <field name="currentUsu.senha">
>>             <field-validator type="requiredstring">
>>                 <message>Favor inserir uma senha</message>
>>             </field-validator>
>>             <field-validator type="stringlength">
>>                 <param name="minLength">4</param>
>>                 <param name="maxLength">12</param>
>>                 <param name="trim">true</param>
>>                 <message>A senha deve conter no mínimo ${minLength} e no
>> máximo ${maxLength}</message>
>>             </field-validator>
>>         </field>
>>
>>     </validators>
>>
>>
>> Hope this can help you.
>> Sorry by text in portuguese on my code, but I guess it's gona be easy to
>> understand.
>>
>> []'s
>>
>> 2008/4/23, volker@magiccode.eu <vo...@magiccode.eu>:
>>> Hello y'all,
>>>
>>> i have quite an interesting problem again.
>>> In my struts.xml i have defined a method to call for my action
>>> (method="save")
>>>
>>> Every time my action class is called, the method validate() is executed.
>>> The same action class used with struts.xml-parameter method="save" is also
>>> executed as expected, but validate is never called.
>>> I thought, i could exclude methods from validation, but how do i include
>>> one ?
>>>
>>> this
>>> <interceptor-ref name="validation">
>>>   <param name="includeMethods">save</param>
>>> </interceptor-ref>
>>>
>>> does not work.
>>>
>>> thanks in advance
>>>
>>>   Volker
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.3 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>  
> iD8DBQFID3Dax4KjH6Tk4MMRAloBAJ92MhmLRMSDvtKDfE8KBbcH60dr/gCfbYbE
> w7bSzJSLamxDA+LPjNNpU2k=
> =sy2F
> -----END PGP SIGNATURE-----
> 
> 



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


Re: Validation on methods

Posted by Volker Karlmeier <vo...@magiccode.eu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
 
Hello Márcio*,

*thanks for your quick answer. Yes, that will work, you're right.
Unfortunately is is not exactly what i would want. You are doing your
validation in the method that is called.
I would prefer the framework to call *validate()* before (!) my method
(in that case "save" ) is called.
This should happen when I subclass from ActionSupport and implement
validate(), but it only seems to work
when the method execute() is called.

I could call validate() from the method save in it's beginning (that's
in fact what I do now), but that not the intention
of the interfaces Validatable/ValidationAware. So I hope, anyone can
give me a hint as I like to write clean, readable
code and do not want to create "workarounds" all the time ;)

Anyway, thanks for the effort.

  Volker

*
*Márcio Gurgel schrieb:
> Hi Volker,
>
> You can do it with struts xml validator:
>
> Include a xml into your action package like this:
>
> ActionName - alias from action -  validation.xml
> Example:
> ActionName-save-validation.xml
>
> * Note that "save" is the name of action in your struts.xml
>
> Nothing better than a real example:
>
> struts.xml -------------------------------------------------------
> <action name="salvar" method="salvar"
>             class="br.com.sgvdba.actions.usu.UsuAction">
>             <result name="success" type="redirectAction">
>                 <param name="idUsu">${currentUsu.cd}</param>
>                 <param name="actionName">abrirDados</param>
>                 <param name="namespace">/usuario</param>
>             </result>
>             <result type="tiles" name="input">default.usu</result>
>         </action>
>
> UsuAction (Action class) -----------------------------------------
> /**
>      * Persiste determinado usuário no banco de dados
>      * *     Para inserir um usuário deve ser confirmada a não-existência
>      *         de outro registro com o mesmo nome de usuário.
>      * @return
>      */
>     public String salvar(){
>         //Verifica se a tela possui erros
>         if (hasFieldErrors() || hasErrors()){
>             return Action.INPUT;
>         }
>
>         try {
>             if (!getUsuFacade().isSenhaValid(currentUsu.getSenha(),
> senhaConf)){
>                 addActionError("As senhas informadas devem ser iguais");
>                 return Action.INPUT;
>             }
>             else
>                 if (!getUsuFacade().isChvValid(currentUsu.getChv())){
>                     addActionError("Já existe um usuário com este nome,
> favor informar um nome diferente");
>                     return Action.INPUT;
>             }
>             else {
>                 NvlAcso nvlAcso =
> getNvlAcsoFacade().recuperaPorId(idNvlAcso);
>                 getUsuFacade().incluir(currentUsu, nvlAcso);
>                 setIdUsu(currentUsu.getCd());
>             }
>
>         } catch (Exception e) {
>             addActionError(SgvdbaConstants.ERRO_DEFAULT + e.getMessage());
>             return Action.INPUT;
>         }
>
>         return Action.SUCCESS;
>     }
>
> UsuAction-salvar-validation.xml (VALIDATION)
> -------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
1.0//EN"
> "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
>     <validators>
>
>         <field name="currentUsu.chv">
>             <field-validator type="requiredstring">
>                 <message>Favor inserir um nome de usuário</message>
>             </field-validator>
>         </field>
>
>         <field name="currentUsu.email">
>             <field-validator type="requiredstring">
>                 <message>Favor inserir um e-mail</message>
>             </field-validator>
>             <field-validator type="email">
>                 <message>Favor informar um e-mail válido</message>
>             </field-validator>
>         </field>
>
>         <field name="currentUsu.senha">
>             <field-validator type="requiredstring">
>                 <message>Favor inserir uma senha</message>
>             </field-validator>
>             <field-validator type="stringlength">
>                 <param name="minLength">4</param>
>                 <param name="maxLength">12</param>
>                 <param name="trim">true</param>
>                 <message>A senha deve conter no mínimo ${minLength} e no
> máximo ${maxLength}</message>
>             </field-validator>
>         </field>
>
>     </validators>
>
>
> Hope this can help you.
> Sorry by text in portuguese on my code, but I guess it's gona be easy to
> understand.
>
> []'s
>
> 2008/4/23, volker@magiccode.eu <vo...@magiccode.eu>:
>> Hello y'all,
>>
>> i have quite an interesting problem again.
>> In my struts.xml i have defined a method to call for my action
>> (method="save")
>>
>> Every time my action class is called, the method validate() is executed.
>> The same action class used with struts.xml-parameter method="save" is also
>> executed as expected, but validate is never called.
>> I thought, i could exclude methods from validation, but how do i include
>> one ?
>>
>> this
>> <interceptor-ref name="validation">
>>   <param name="includeMethods">save</param>
>> </interceptor-ref>
>>
>> does not work.
>>
>> thanks in advance
>>
>>   Volker
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
 
iD8DBQFID3Dax4KjH6Tk4MMRAloBAJ92MhmLRMSDvtKDfE8KBbcH60dr/gCfbYbE
w7bSzJSLamxDA+LPjNNpU2k=
=sy2F
-----END PGP SIGNATURE-----


Re: Validation on methods

Posted by Márcio Gurgel <ma...@gmail.com>.
Hi Volker,

You can do it with struts xml validator:

Include a xml into your action package like this:

ActionName - alias from action -  validation.xml
Example:
ActionName-save-validation.xml

* Note that "save" is the name of action in your struts.xml

Nothing better than a real example:

struts.xml -------------------------------------------------------
<action name="salvar" method="salvar"
            class="br.com.sgvdba.actions.usu.UsuAction">
            <result name="success" type="redirectAction">
                <param name="idUsu">${currentUsu.cd}</param>
                <param name="actionName">abrirDados</param>
                <param name="namespace">/usuario</param>
            </result>
            <result type="tiles" name="input">default.usu</result>
        </action>

UsuAction (Action class) -----------------------------------------
/**
     * Persiste determinado usuário no banco de dados
     * *     Para inserir um usuário deve ser confirmada a não-existência
     *         de outro registro com o mesmo nome de usuário.
     * @return
     */
    public String salvar(){
        //Verifica se a tela possui erros
        if (hasFieldErrors() || hasErrors()){
            return Action.INPUT;
        }

        try {
            if (!getUsuFacade().isSenhaValid(currentUsu.getSenha(),
senhaConf)){
                addActionError("As senhas informadas devem ser iguais");
                return Action.INPUT;
            }
            else
                if (!getUsuFacade().isChvValid(currentUsu.getChv())){
                    addActionError("Já existe um usuário com este nome,
favor informar um nome diferente");
                    return Action.INPUT;
            }
            else {
                NvlAcso nvlAcso =
getNvlAcsoFacade().recuperaPorId(idNvlAcso);
                getUsuFacade().incluir(currentUsu, nvlAcso);
                setIdUsu(currentUsu.getCd());
            }

        } catch (Exception e) {
            addActionError(SgvdbaConstants.ERRO_DEFAULT + e.getMessage());
            return Action.INPUT;
        }

        return Action.SUCCESS;
    }

UsuAction-salvar-validation.xml (VALIDATION)
-------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
    <validators>

        <field name="currentUsu.chv">
            <field-validator type="requiredstring">
                <message>Favor inserir um nome de usuário</message>
            </field-validator>
        </field>

        <field name="currentUsu.email">
            <field-validator type="requiredstring">
                <message>Favor inserir um e-mail</message>
            </field-validator>
            <field-validator type="email">
                <message>Favor informar um e-mail válido</message>
            </field-validator>
        </field>

        <field name="currentUsu.senha">
            <field-validator type="requiredstring">
                <message>Favor inserir uma senha</message>
            </field-validator>
            <field-validator type="stringlength">
                <param name="minLength">4</param>
                <param name="maxLength">12</param>
                <param name="trim">true</param>
                <message>A senha deve conter no mínimo ${minLength} e no
máximo ${maxLength}</message>
            </field-validator>
        </field>

    </validators>


Hope this can help you.
Sorry by text in portuguese on my code, but I guess it's gona be easy to
understand.

[]'s

2008/4/23, volker@magiccode.eu <vo...@magiccode.eu>:
>
> Hello y'all,
>
> i have quite an interesting problem again.
> In my struts.xml i have defined a method to call for my action
> (method="save")
>
> Every time my action class is called, the method validate() is executed.
> The same action class used with struts.xml-parameter method="save" is also
> executed as expected, but validate is never called.
> I thought, i could exclude methods from validation, but how do i include
> one ?
>
> this
> <interceptor-ref name="validation">
>   <param name="includeMethods">save</param>
> </interceptor-ref>
>
> does not work.
>
> thanks in advance
>
>   Volker
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>