java - How to set validation context with annotations -
imagine i've action myaction
, field user
getters , setters.
then have 2 exposed public methods want use visitor validation object user 2 different contexts, user validated in 2 different ways, depending on method has been called.
i want annotations only, no validation.xml.
see example
public class myaction extends actionsupport { private user user; public user getuser() { return this.user; } public void setuser(user user) { this.user=user; } public string execute() { //some code here ... return success; } @validations( visitorfields = {@visitorfieldvalidator(context="mycontext1", fieldname="user", key="error.message", message="a default error message", shortcircuit=true)} ) public string save() { //some code here ... return success; } @validations( visitorfields = {@visitorfieldvalidator(context="mycontext2", fieldname="user", key="error.message", message="a default error message", shortcircuit=true)} ) public string update() { //some code here ... return success; } //... }
now i'd specify context (mycontext1 , mycontext2) annotations inside user object, validators fire when save() method called on myaction while others fire when update() called.
i imagine this:
public class user implements serializable { private integer id; private string name; @requiredfieldvalidator(context="mycontext2", message = "you must enter value data.") public void setid(integer id) { this.id=id; } @requiredfieldvalidator(context="mycontext1", message = "you must enter value data.") public void setname(string name) { this.name = name; } //some code... getters, etc... }
i'd that, example, during creation operation (method save()) name required while during update operation (method update() ) id required. unfortunately seems me requiredfieldvalidator, other validator annotation in struts2, not support "context" field.
i know can use user-mycontext1-validation.xml, , user-mycontext2-validation.xml file (i've not tried think i've understood can done) i'd use annotations only.
do know how can annotations?
Comments
Post a Comment