Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: [s2] Validation 'magic' on 'input'

Laurie Harper

2007-02-27

Replies:

Just to follow up on this for the archives, there is now an even easier
way: annotate methods for which no validation should fire with
@SkipValidation. I believe (?) that annotation is new with 2.0.6, or at
least it's a recent addition. Anyway, it works very well.

L.

Don Brown wrote:
> Nah, there is a better way. Take a look at the struts-default.xml
> file and notice how the validation interceptor is configured to only
> skip validation on certain methods. Create a new base package that
> contains a default interceptor stack which has the validation
> interceptor configured differently, say to skip "execute" as well.
> Then, use the "struts.codebehind.defaultPackage" setting to point the
> codebehind plugin at your new parent package.
>
> Don
>
> On 2/12/07, Laurie Harper <laurie@(protected):
>> Hmm, if that's true it'll mean I'm forced to split my user admin action
>> into multiple actions (and same for every other master-detail view I
>> have) :-( Does the same limitation apply when using XML-based validation?
>>
>> L.
>>
>> Musachy Barroso wrote:
>> > I think when you annotate a class with @Validations, "input" is the
>> only
>> > method that will be called without invoking the validation first.
>> >
>> > musachy
>> >
>> > Laurie Harper wrote:
>> >> I'm trying to get validation working (using annotations) in a Struts2
>> >> project and I can't seem to get it to quite cooperate :-) I'm using
>> >> the Zero Configuration and Code Behind plugins, so this may be a
>> >> result of some unintended interaction between those and the validation
>> >> framework.
>> >>
>> >> The use case is a master/detail view, where both are handled by the
>> >> same action. The (default) master view (/admin/users.jsp) lists all
>> >> the users and has an Add User link pointing to
>> >> /admin/users!add.action. The problem is, as soon as I add any
>> >> validation rules, the action is never executed and Struts loads the
>> >> default /admin/users.jsp view, instead of /admin/users-edit.jsp.
>> >> Without validation rules specified (including if I have an empty
>> >> @Validations annotation), everything works as it should.
>> >>
>> >> I've tried using an @SkipValidation annotation as mentioned on the
>> >> Validation page [1] in the documentation, but that annotation doesn't
>> >> seem to exist anywhere in Struts2 or XWork...!
>> >>
>> >> Now, the magic: if I add an 'input' method to the action with 'return
>> >> "edit";', and hit /admin/users!input.action (instead of ...!add...),
>> >> it works fine again. It appears that the token 'input' is somehow
>> magic?
>> >>
>> >> A stripped down copy of my action follows [2]. How do I get validation
>> >> to *only* be applied when calling the save() method?
>> >>
>> >> [1] http://struts.apache.org/2.x/docs/validation.html
>> >>
>> >> [2] action code:
>> >>
>> >> package ...admin;
>> >> @Results(
>> >>   @Result(value = "users", type = ServletActionRedirectResult.class)
>> >> // XXX why doesn't this work?
>> >> )
>> >> @Validation
>> >> public class UsersAction extends ActionSupport {
>> >>   public String add() {
>> >>      System.out.println("ADD");
>> >>      user = userService.createUser();
>> >>      return "edit";
>> >>   }
>> >>
>> >>   public String execute() {
>> >>      System.out.println("EXECUTE");
>> >>      return "edit";
>> >>   }
>> >>
>> >>   public String input() {
>> >>      System.out.println("INPUT");
>> >>      return "edit";
>> >>   }
>> >>
>> >>   @Validations(
>> >>      requiredStrings = {
>> >>         @RequiredStringValidator(
>> >>              fieldName = "user.login",
>> >>              key = "user.login.required",
>> >>              message = "user.login.required")},
>> >>      ...
>> >>   )
>> >>   public String save() {
>> >>      System.out.println("SAVE");
>> >>      ...
>> >>
>> >>      return SUCCESS;
>> >>   }
>> >> }
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@(protected)
>> >> For additional commands, e-mail: user-help@(protected)
>> >>
>> >>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@(protected)
>> For additional commands, e-mail: user-help@(protected)
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.