tags:

views:

116

answers:

2

Hi is it the best, if I need to trim what user sends in a form to trim it in ActionForm?

e.g:

I have MyActionForm class in which I have property

private String name;

public void setName(String name) {

   if(name!=null) {

       this.name = name.trim();

    }

}

Or is there any other good way?

thx.

A: 

A slightly less cumbersome way is to use StringUtils from Apache Commons Lang:

private String name;

public void setName(String name) {
   this.name = StringUtils.trimToEmpty(name);
}

You can use either trimToEmpty or trimToNull, whichever makes more sense for your application. It's even neater if you use a static import.

skaffman
+1  A: 

It's better to do it in the Action.

You will know if the user left the field empty or if the user wrote some spaces in it.

So, your application could behave different in each case.

If you do the trim in the ActionForm you don't know what the user exactly did.

oscargm