一种可能的解决方案 - 允许所有字符串字段使用单个转换入口点 - 是注册一个自定义转换器,但不是使用 Struts 而是使用BeanUtils http://commons.apache.org/beanutils/.
为了将请求参数映射到表单属性,Struts 使用RequestUtils 类的 populate 方法 http://struts.apache.org/1.1/api/org/apache/struts/util/RequestUtils.html#populate%28java.lang.Object,%20javax.servlet.http.HttpServletRequest%29。这个类又使用了一个BeanUtils
实施确实发挥了作用。
一个简单的流程类似于 Struts 的流程请求工具 http://struts.apache.org/1.1/api/org/apache/struts/util/RequestUtils.html > BeanUtils http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/BeanUtils.html > BeanUtilsBean http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/BeanUtilsBean.html > 转换工具 http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/ConvertUtils.html > 转换UtilsBean http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/ConvertUtilsBean.html > 转换器 http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/Converter.html.
有趣的是,还有一个字符串转换器 http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/converters/StringConverter.html它从字符串转换为...aaaaaa...字符串!
The ConvertUtils 类有一个寄存器 http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/ConvertUtils.html#register%28org.apache.commons.beanutils.Converter,%20java.lang.Class%29方法,您可以使用它来注册转换器,覆盖现有的转换器。这意味着您可以自己编写自定义字符串转换器,该转换器对于空字符串返回 null,然后您可以等待 Struts 应用程序完全加载,这样您就不会感到意外(即确保您的转换器是最后一个注册的类型)String
).
应用程序加载后,您介入并用您自己的实现覆盖默认的字符串转换器。例如,您可以使用ServletContextListener http://download.oracle.com/javaee/1.4/api/javax/servlet/ServletContextListener.html并致电ConvertUtils.register(...)
in the contextInitialized
method.
然后您在中配置侦听器web.xml
你呢应该可以走了 http://www.java-tips.org/java-ee-tips/java-servlet/how-to-work-with-servletcontextlistener-4.html.