升级到 struts 2.3.15.1 不会在操作类上设置 HashMap 值

Upgrading to struts 2.3.15.1 does not set HashMap values on action class(升级到 struts 2.3.15.1 不会在操作类上设置 HashMap 值)

本文介绍了升级到 struts 2.3.15.1 不会在操作类上设置 HashMap 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 2.1.6 升级到 2.3.15.1,因为最新版本中提供了安全修复程序.但是,现在表单字段值不会发布到 Action 类.基本上,当提交表单时,我将 JSP 中的 HashMap props 填充到 Action 类中.当我将 struts 版本升级到 2.3.15.1 时,这不起作用.没有代码更改.当我调试代码时,我注意到 setProps 方法没有被调用.这已经不允许了.有什么解决方法吗?一旦我恢复 struts 库的更改,一切都会完美.请帮忙.

I upgraded from 2.1.6 to 2.3.15.1 because of the security fixes available in the latest version. However, now the form field values are not posted to the Action class. Basically, I populate the HashMap props from the JSP into the Action class, when the form is submitted. When I upgraded the struts version to 2.3.15.1 this does not work. There has been no code change. When I debugged the code, I noticed that the setProps method is not invoked. Is this not allowed anymore. Is there any workaround? As soon as I revert the struts library changes, everything works perfect. Please help.

我的代码如下所示:

动作类:

    private Map<String, Wall> props;

    public void prepare(){
          //fill up props map here.
        }
    public String view(){
        return INPUT;
    }

    public String save(){
        myService.setProps(props);
        return INPUT;
    }

    public void setProps(Map<String, Wall> props) {
        this.props = props;
    }

    public Map<String, Wall> getProps() {
        return props;
    }

JSP:

<s:iterator value="props.entrySet()" id="prop" status="propStatus">
    <s:textfield name="props['%{#prop.key}'].value" value="%{#prop.value.value}" />
</s:iterator>

推荐答案

自 Struts 2.1.6 以来发生了巨大的变化.如果您没有明确告诉它这样做,Struts 2 将不会为您创建对象.prepare 方法在 params 拦截器将 props 设置为操作之前调用,并且您评论说您填充了地图

There have been huge changes since Struts 2.1.6. Struts 2 will not create objects for you if you don't explicitly tell it to do. The prepare method calls before the params interceptor sets props to the action, and you commented that you populate the map

//在此处填写道具图.

//fill up props map here.

毫不奇怪,Struts 不会调用该 setter setProps,因为它已经包含一个地图实例.因此,它只是调用 getProps.然后它应该将索引属性值设置为地图.但它不知道作为要转换为集合元素的对象的类型,以及它是否应该为元素创建一个新对象(如果它是 null).通过在 props 字段上添加注释,它应该可以解决在提交时填充地图的问题.

not surprisingly, that Struts not call that setter setProps because it already contains a map instance. So, it simply calls getProps. Then it should set the indexed property values to the map. But it doesn't know a type of the object that is an element of the collection to convert to, and if it should create a new object for element if it's null. By putting annotations on the props field it should solve the problem populating a map on submit.

@Element(value = Wall.class)
@CreateIfNull(value = true)
private Map<String, Wall> props = new HashMap<>();

我猜它会自动确定的关键.如果您在 Action-conversion.properties

I guess the key it will determine automatically. The same could be done if you specify it in Action-conversion.properties

Element_props=Wall
CreateIfNull_props=true

接下来你的 JSP 可以被替换为

Next your JSP could be replaced to

<s:iterator value="props">
    <s:textfield name="props['%{key}'].value" />
</s:iterator>

最后是你还没有发布的 Wall 类,应该是这样的

And last the Wall class you haven't posted, should look like

public class Wall {
  String value;
  //getter and setter here
} 

这篇关于升级到 struts 2.3.15.1 不会在操作类上设置 HashMap 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:升级到 struts 2.3.15.1 不会在操作类上设置 HashMap 值

基础教程推荐