修剪Struts2文本字符串输入 [英] Trimming Struts2 textfield string input

查看:96
本文介绍了修剪Struts2文本字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的方法来修剪这个字符串/哪里是最好的地方放置修剪代码?

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

说我在我的jsp中有以下textfield: / p>

Say I have the following textfield in my jsp:

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

动作类:

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

bean: / p>

The bean:

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

我可以更改默认设置这个bean似乎是一个黑客:

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

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

编辑:我也看到这个问题: struts2修剪从表单获取的所有字符串,其中也是有人认为正确的方法是使用拦截器。

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?

推荐答案

简短回答是不是默认情况下,那里没有建立这样做的机制,你需要在你的动作类中执行,或者某种类型的java脚本会为你做。

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.

我相信拦截器是一个很好的方法,它更好地拥有这样的拦截器带有S2。

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

这篇关于修剪Struts2文本字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆