Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trimming Struts2 textfield string input

What is the best way to trim this string/where is the best place to put the trim code?

Say I have the following textfield in my jsp:

<s:textfield label="First Name" name="person.firstname"/>

The action class:

public class BaseAction extends ActionSupport implements ServletRequestAware, SessionAware {
    private Person person;
    // Getters, setters and action logic
}

The bean:

public class Person implements Serializable {
    private String lastname;
    private String firstname;
    // Getters and setters
}

I can change the default setting in the bean but this seems like a hack:

public void setFirstname(String firstname) {
    this.firstname = firstname.trim();
}

EDIT: I did also see this question: struts2 trim all string obtained from forms where it's also suggested by some that the "correct" method is to use an interceptor.

Why is an interceptor the "correct" way? What is so wrong about changing the bean's setters?

like image 897
nmc Avatar asked Sep 13 '12 16:09

nmc


2 Answers

It can be done with Struts2 converters.

public class TrimmingStringConverter extends StrutsTypeConverter {

    public Object convertFromString(Map ctx, String[] values, Class arg2) {
        if (values != null && values.length > 0) {
            return values[0].trim();
        }

        return null;
    }

    public String convertToString(Map ctx, Object o) {
        if (o != null) {
            return o.toString();
        }
        else {
            return null;
        }
    }

    public Object convertValue(Map context, Object o, Class toClass)
    {
        if (o == null) {
            return null;
        } else if (toClass == java.lang.String.class) {
            if (o instanceof String[]) {
                String[] os = (String[]) o;

                if (os.length > 0) {
                    return os[0].trim();
                }
            }

            return o.toString().trim();
        }

        return super.convertValue(context, o, toClass);
   }
}

It must be registered in xwork-conversion.properties: java.lang.String=es.jogaco.webapp.TrimmingStringConverter

This will be applied to all user input.

It will work if you have the default struts2 interceptors. Quoted from struts2 doc:

By default, the conversion interceptor is included in struts-default.xml in the default stack

Plus I have it working in my struts2 app.

like image 73
jogaco Avatar answered Oct 27 '22 06:10

jogaco


Short answer is Not by default, there is no build in mechanism to do this and you either need to do it in your action class or some-kind of java-script will do that for you.

Other possible way is to create an interceptor to do this with option to excludes or something like on similar trek.

I believe Interceptor is a good way to do this,its better to have such interceptor comes with S2.

like image 36
Umesh Awasthi Avatar answered Oct 27 '22 06:10

Umesh Awasthi