有没有办法配置Struts绑定null而不是空String? [英] Is there any way to configure Struts to bind null instead of empty String?

查看:153
本文介绍了有没有办法配置Struts绑定null而不是空String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户决定将表单中的字段保留为空时,Apache Struts将空 String 绑定为 ActionForm 。有没有办法全局修改行为并选择 null 而不是空字符串

When user decides to leave the field in the form empty the Apache Struts binds empty String as value for properties in the ActionForm. Is there any way to modify the behavior globally and opt for null instead of empty String?

我知道Spring MVC完全相同,但也有 StringTrimmerEditor ,可以注册为属性编辑器,将字符串修剪为 null

I know that Spring MVC does exactly the same, but there is also StringTrimmerEditor that can be registered as property editor to trim strings to null.

推荐答案

一个可能的解决方案 - 允许所有String字段的单个转换入口点 - 将注册一个自定义转换器,但不是使用Struts,而是使用 BeanUtils

A possible solution - one that will allow a single conversion entry point for all your String fields - would be to register a custom convertor, but not with Struts but with BeanUtils.

为了将请求参数映射到表单属性,Struts使用填充其RequestUtils类的方法。这个类反过来使用 BeanUtils 实现来完成它的工作。

For mapping request parameters to form properties, Struts makes use of the populate method of its RequestUtils class. This class in turn uses a BeanUtils implementation do do its work.

一个简单的流程就像这样的东西Struts的 RequestUtils > BeanUtils > < a href =http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/BeanUtilsBean.html =nofollow> BeanUtilsBean > ConvertUtils > ConvertUtilsBean > 转换器

A simplistic flow would be something of the likes of Struts' RequestUtils > BeanUtils > BeanUtilsBean > ConvertUtils > ConvertUtilsBean > Converter.

有趣的是,还有一个< a href =http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/converters/StringConverter.html =nofollow> StringConverter 从String转换为... aaaaaa ... String!

The interesting thing is that there is also a StringConverter which converts from String to... aaaaaa... String!

ConvertUtils类有一个寄存器方法,可用于注册转换器,覆盖现有的转换器。这意味着您可以自己编写自定义字符串转换器,它为空字符串返回null,然后您可以等待Struts应用程序完全加载,这样您就不会感到意外(即确保您的转换器是最后注册的类型字符串)。

The ConvertUtils class has a register method which you can use to register convertors, overwriting the existing ones. This means you could write yourself the custom String convertor which returns null for empty strings and then you could wait for your Struts application to load completely so that you have no surprises (i.e. make sure your converter is the last registered for type String).

加载应用程序后,您将介入并使用您自己的实现覆盖默认的String转换器。例如,您可以使用 ServletContextListener 和在 contextInitialized 方法中调用 ConvertUtils.register(...)

After the application loads, you step in and overwrite the default String convertor with your own implementation. For example, you could do that using a ServletContextListener and call the ConvertUtils.register(...) in the contextInitialized method.

然后在 web.xml 中配置监听器并且应该不错

You then configure the listener in web.xml and you should be good to go.

这篇关于有没有办法配置Struts绑定null而不是空String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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