简体   繁体   English

修剪Struts2文本字符串输入

[英]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: 假设我的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: 我可以更改bean中的默认设置,但这似乎是一个黑客:

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. 编辑 :我也看到了这个问题: struts2修剪从表单获取的所有字符串,其中一些人也建议“正确”的方法是使用拦截器。

Why is an interceptor the "correct" way? 为什么拦截器是“正确的”方式? What is so wrong about changing the bean's setters? 改变bean的二传手有什么不对?

It can be done with Struts2 converters . 它可以使用Struts2转换器完成。

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 它必须在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. 如果您有默认的struts2拦截器,它将起作用。 Quoted from struts2 doc: 引自struts2 doc:

By default, the conversion interceptor is included in struts-default.xml in the default stack 默认情况下,转换拦截器包含在默认堆栈中的struts-default.xml中

Plus I have it working in my struts2 app. 另外,我在struts2应用程序中使用它。

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. 简短的回答是默认情况下 ,没有构建机制来执行此操作,您需要在您的操作类中执行此操作,或者某种类型的java脚本将为您执行此操作。

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. 我相信Interceptor是一个很好的方法来实现这一点,最好有这样的拦截器来自S2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM