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

查看:59
本文介绍了升级到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拦截器将props设置为动作之前,prepare方法会调用,并且您已注释您填充了地图

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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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