Java – trim struts 2 text string input

What is the best way to trim this string? Where is the best place to place the trim code?

Suppose my JSP has the following text fields:

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

Action class:

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

bean:

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

I can change the default settings in the bean, but this seems to be a hacker:

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

Editor: I also saw this problem: struts 2 trim all string obtained from forms. Some of them also suggested that the "correct" method is to use interceptors

Why are interceptors the "right" way? What's wrong with changing bean's setter?

Solution

The short answer is that by default, there is no build mechanism to perform this operation. You need to perform this operation in your operation class, or some type of java script will perform this operation for you

Another possible approach is to create an interceptor to perform this operation to select exclusion or similar hiking

I believe interceptor is a good way to achieve this. It's best to have such an interceptor from S2

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>