You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Simone Camillo Buzzi <si...@gmail.com> on 2013/06/28 07:08:29 UTC

Validator with annotations

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

Re: Validator with annotations

Posted by Simone Camillo Buzzi <si...@gmail.com>.
Hi, the goal that I'm trying to reach is to reduce the number of variables
and method into the Struts action to improve code readability and
maintainability.
I'm using hibernate, so I could obtain a good synergy if i can set value
coming from parameters directly into the hibernate model bean, maintaing
the validation at the action level as you recommends.
I prefer to don't insert struts annotation in hibernate bean, I don't know
which effect I can obtain in this way and I don't want to a dependency
between the model and struts.

I don't understand why I have this behaviour in the action. Does the
interceptor need a getter to evaluate the field? What's the convention?

Kind regards
Simone Buzzi

Re: Validator with annotations

Posted by Maurizio Cucchiara <mc...@apache.org>.
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 <si...@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
>