struts2 trim all string obtained from forms
Posted
by aelkin
on Stack Overflow
See other posts from Stack Overflow
or by aelkin
Published on 2010-03-23T11:44:04Z
Indexed on
2010/03/23
12:13 UTC
Read the original article
Hit count: 397
Hi All,
I develop web application using struts2. I want to improve getting string from forms. For this need trim all string and if obtained string is empty then set null to field.
For this, I created StringConverter.
public class StringConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] strings, Class toClass) {
if (strings == null || strings.length == 0) {
return null;
}
String result = strings[0];
if (result == null) {
return null;
}
result = result.trim();
if (result.isEmpty()) {
return null;
}
return result;
}
@Override
public String convertToString(Map context, Object object) {
if (object != null && object instanceof String) {
return object.toString();
}
return null;
}
}
Next, I added row to xwork-conversion.properties
java.lang.String=com.mypackage.StringConverter
Thats all. But I did not get the desired result.
convertToString() method is called when jsp build form, but convertFromString() doesn't invoke.
What I do wrong? How can I get the same behaviour using another way?
Please, not offer solutions such as:
- remove the value of such form elements using javascript.
- create util method which will make it using reflection. Then call it for each form bean.
Thanks in advance, Alexey.
© Stack Overflow or respective owner