Java Mailing List Archive

http://www.junlu.com/

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

[s2] Validation 'magic' on 'input'

Laurie Harper

2007-02-12

Replies:

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)

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