struts2修剪从表单中获取的所有字符串 [英] struts2 trim all string obtained from forms

查看:84
本文介绍了struts2修剪从表单中获取的所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用struts2开发Web应用程序。我想改进从表单中获取字符串。为此需要修剪所有字符串,如果获得的字符串为空,则将 null 设置为字段。

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.

为此,我创建了 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;
    }
}

接下来,我向xwork-conversion.properties添加了行

Next, I added row to xwork-conversion.properties

java.lang.String=com.mypackage.StringConverter

多数。但是我没有得到想要的结果。

Thats all. But I did not get the desired result.

在jsp构建表单时调用convertToString()方法,但是convertFromString()不会调用。

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. 使用javascript删除此类表单元素的值。

  2. create util方法将使用反射。然后为每个表单bean调用它。

提前致谢,
Alexey。

Thanks in advance, Alexey.

推荐答案

您将在StrutsTypeConverter类的代码中找到答案。基本上,在这个级别,类型转换器框架知道关于数据是来自还是到用户的任何信息,它只知道它正在从一种类型(String)转换为另一种类型type(也是String)。并且由于它首先检查to类型,它将总是调用convertToString。

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.

简而言之,当前版本的Struts(2.1.x是我正在使用的)不支持String-to-String类型转换器。毕竟,因为它们是类型的转换器,你可以说这是设计的。

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.

我也在寻找一种方法取得了类似的结果,但还没有找到一个非常好的解决方案。可能最正确的方法是编写一个拦截器(如提到的@leonbloy)。有几种方法可以解决这个问题,最简单的方法就是在动作上设置所有请求参数之前(即在params拦截器执行之前)修剪它们。

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).

这篇关于struts2修剪从表单中获取的所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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