Posts

Showing posts from August, 2013

How to trim all strings in a bean (pojo/value object) in Java

One of my favourite frameworks is webfirmframework

Forms are used everywhere in a web/desktop/other applications. In these forms, the user may enter values starting and/or ending with spaces. But we may not need theses spaces at the starting and ending, so we may want to trim it. As we know, we may simply use a trim() method to trim a string. But, what if we want to trim all strings in a bean/pojo/value object. Suppose, consider a form having some fields, first name, last name, address etc... The user may enter the values as

First Name :

Last Name :

Address :  House name, Place, Distring

The values have spaces in the beginning and ending. So, to write code to trim every string separately is a kind of messing sometimes.

Here is a method which can trim all strings in a bean/pojo/value object (Only the strings will be trimmed, so it doesn't matter if it contains data types other than strings). Before that let us make a value object (POJO/bean) for these fields.
Consider …