Ciao Simone, have a look at VisitorFieldValidator [1] [2] and model driven. IMO it's better to keep the annotations inside the action rather than the bean (you can have different use cases for the same model).
[1] http://struts.apache.org/release/2.3.x/docs/visitorfieldvalidator-annotation.html [2] http://struts.apache.org/release/2.2.x/docs/using-visitor-field-validator.html Twitter :http://www.twitter.com/m_cucchiara G+ :https://plus.google.com/107903711540963855921 Linkedin :http://www.linkedin.com/in/mauriziocucchiara VisualizeMe: http://vizualize.me/maurizio.cucchiara?r=maurizio.cucchiara Maurizio Cucchiara On 28 June 2013 07:08, Simone Camillo Buzzi <simonebu...@gmail.com> wrote: > Hi, I have some doubt about how to use validation annotation. > I'm changing my code to make it more readable and maintainable. > > I started from an action where I had all setters with annotation and i put > data in a private local variable > > ex. > private String email; > > @RequiredStringValidator(key="fieldError.required", message = "*") > @EmailValidator (key="fieldError.emailFormat", message="*") > public void setEmail(String email) { > this.email = email; > } > > It functioned well but I neeed to reorganize my code. > So, I create e class User where I directly put data coming from the web and > I erase all the private variable in this way > > User user = null; > public User getUser() { > if (user == null) > user = new User(); > return user; > } > > @RequiredStringValidator(key="fieldError.required", message = "*") > @EmailValidator (key="fieldError.emailFormat", message="*") > public void setEmail(String email) { > getUser().setEmail(email); > } > > My bean receives data but validators stop to function, so I receive bad > validation messages > Does validator need a getter (not present also in the first release) or use > reflection on local variables to check data? > How can I implement my new data model in Struts2 > > <dependency> > <groupId>org.apache.struts</groupId> > <artifactId>struts2-core</artifactId> > <version>2.3.14</version> > </dependency> > > > Kind regards > Simone Buzzi >