Reputation: 3667
In Spring 3:
My Bean:
public class UserFormBean {
private String userEmail;
private String userMobNo;
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserMobNo() {
return userMobNo;
}
public void setUserMobNo(String userMobNo) {
this.userMobNo = userMobNo;
}
}
And my request handler in Controller:
@RequestMapping(value = "/userData", method = RequestMethod.GET)
String userData(UserFormBean bean){
//code to handle incoming data
}
if user dosen't set any value in 'userEmail'
and 'userMobNo'
attributes at request time, spring by default sets null
in these attributes and when i am getting value of these attributes in my request handler by String str = bean.getUserEmail();
it returns me null in double quote
like "null"
.
Is there any way to convert these null
values as "" (blank)
when they contain null
because i made an extra handler to handle these "null"
values. Or some better idea to resolve this issue.
Thanks
Upvotes: 0
Views: 2226
Reputation: 4236
You could define a standard for naming your "web methods", for example:
@RequestMapping(value = "/userData", method = RequestMethod.GET)
String webUserData(UserFormBean bean){
//code
}
When all your methods start with web*, then you can write an aspect that will initalize all arguments as you would like.
If you need help about creating and defining an aspect, just ask.
UPDATE: creating and defining an aspect
I will not write the code for you, because it's much better for you to learn by reading the documentation - you will get to know a lot of extra details.
That said, the steps to create an aspect are:
In your aspect code you will have to access the current method arguments and do your logic for setting the empty string values for Strings.
Upvotes: 1
Reputation: 38300
I suspect the setter is called with "\"null\"" for the new value. Try this:
public void setUserEmail(final String newValue)
{
if (StringUtils.isNotBlank(newValue) &&
"\"null\"".equals(newValue))
{
userEmail = newValue;
}
else
{
userEmail = StringUtils.EMPTY;
}
}
Upvotes: 1
Reputation: 298898
I think the easiest way would be to initialize the properties to the empty String:
private String userEmail = "";
private String userMobNo = "";
Upvotes: 2