Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struts2 trim all string obtained from forms

Tags:

java

struts2

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:

  1. remove the value of such form elements using javascript.
  2. create util method which will make it using reflection. Then call it for each form bean.

Thanks in advance, Alexey.

like image 606
user268292 Avatar asked Mar 23 '10 11:03

user268292


3 Answers

You will find the answer in the code of the StrutsTypeConverter class. Basically, at this level the type converter framework does not know anything about whether the data is coming "from" or "to" the user, it merely knows it is converting from one type (String) to another type (also String). And since it checks the "to" type first, it will always call convertToString.

To cut a long story short, the current version of Struts (2.1.x is what I'm using) does not and cannot support String-to-String type converters. Since they are, after all, type converters you can probably say that this is by design.

I'm also looking for a way to achieve a similar result, but haven't found a really good solution yet. Probably the most "correct" approach would be to write an interceptor (as @leonbloy mentioned). There would be several ways to approach this, the most straightforward being to trim all the request parameters before they are set on the action (i.e. before the "params" interceptor executes).

like image 163
Todd Owen Avatar answered Sep 22 '22 03:09

Todd Owen


Seems right to me. Are you sure convertFromString is not even called ?

Another approach you might try is to write an Interceptor that trims all params (one frequently wants that).

like image 43
leonbloy Avatar answered Sep 23 '22 03:09

leonbloy


I don't do Struts2, but similar problem has manifested in JSF until version 1.2 in 2006 (JSF is Sun's MVC framework, the competitor of Struts2). Converting to String was impossible in JSF as well "by design". Older JSF versions used to check if the target type equals java.lang.String and then it just sets the request parameter value in the model without attempting to convert it (because the request parameter value is already obtained as String). If the target type was been different, then it will locate and run any associated converter to convert it to the desired target type (which is other than String). Since JSF 1.2 they fixed it by removing the check of the target type and locating the converter at any way.

I won't be surprised if similar feature/bug exist in Struts2. I would look around at their homepage if there isn't already an issue/bug report about that and otherwise post one.

like image 26
BalusC Avatar answered Sep 23 '22 03:09

BalusC