java - 如何将 Map<Object, List<Object>> 传递给 Struts 2 中的操作

标签 java jsp type-conversion struts2 ognl

我有一个事件对象,里面有一个Map<ObjectA, List<ObjectB>> , ObjectA是标签,list<ObjectB>是表行。使用以下代码,我可以正确显示表格,但是当我将表格提交给 Action 时类, map 是null在 Activity 中。

JSP 代码:

<s:iterator value="event.planMap" var="map" >
    <h4>Plan Type: <s:property value='key' /></h4>
    <table id="plan">
    <s:iterator value="value" status="stat" var="detail" >
        <tr>
            <td><input type="text" id="name" name="event.planMap['%{#map.key}'][%{#stat.index}].name" value="<s:property value='name'/>"/></td>
            <td><input type="text" id="text" name="event.planMap['%{#map.key}'][%{#stat.index}].text" value="<s:property value='text'/>"/></td>
            <td><input type="text" id="contact" name="event.planMap['%{#map.key}'][%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
        </tr>
        </s:iterator>
    </table>
</s:iterator>

@Andrea & Roman,所以我修改了代码。显示表格是正确的,但我得到了错误,它进入了结果输入。如果我删除 planMap , 行动成功。所以至少我知道错误是planMap .修改后的代码为:

Event定义:

public Event {
    private Map<Object_A, Object_B> planMap;
    public Map<Object_A, Object_B> getPlanMap {
           return this.planMap;
    }

    public void setPlanMap(Map<Object_A, Object_B> planMap) {
           this.planMap = planMap;
    }
}

Object_B定义:

public Object_B {
    private List<Object_C> details;
    
    public List<Object_C> getDetials() {
           return this.details;
    }
    public void setDetails(List<Object_C> details) {
           this.details = details;
    }
}

JSP代码是:

<s:iterator value="event.planMap" status="mStat"  >
    <h4>Plan Type: <s:property value='key' /></h4>
    <table id="plan">
    <s:iterator value="value.details" status="stat">
    <tr>
        <td><input type="text" id="name" name="event.planMap['% {#mStat.index}'].details[%{#stat.index}].name" value="<s:property value='name'/>"/></td>
        <td><input type="text" id="text" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].text" value="<s:property value='text'/>"/></td>
        <td><input type="text" id="contact" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
    </tr>
    </s:iterator>
</table>
</s:iterator>
      

最佳答案

Struts 可以访问 Action bean 的索引属性,例如 List , Map等,但要求元素类应该是一个 Java Bean,如 Map<ObjectA, ObjectC> , 其中ObjectC包裹 List<ObjectB> .

然后使用Struts type conversion像这个答案中那样填充索引属性 How to get updated list values from JSP in my action .

Indexing a collection by a property of that collection

It is also possible to obtain a unique element of a collection by passing the value of a given property of that element. By default, the property of the element of the collection is determined in Class-conversion.properties using KeyProperty_xxx=yyy, where xxx is the property of the bean Class that returns the collection and yyy is the property of the collection element that we want to index on.

For an example, see the following two classes:

MyAction.java

/**
 * @return a Collection of Foo objects
 */
public Collection getFooCollection()
{
    return foo;
}

Foo.java

/**
 * @return a unique identifier
 */
public Long getId()
{
    return id;
}

关于java - 如何将 Map<Object, List<Object>> 传递给 Struts 2 中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23250299/

相关文章:

java - Servlet request.getParameter() 总是返回 "null"

C# 动态泛型列表

java - 除非明确捕获,否则异常不会显示在控制台中

java - 切换按钮 onCheckChangeListener

java - 从 tomcat 8.0 迁移到 8.5 时出现 ElException

java - getRequestDispatcher() 和forward() 方法如何工作?

c++ - 如何从文件中读取原始 PCM 数据并转换为 float ?

java - 将电话号码从 double 转换为整数

java - GOTO/继续 Java 动态编程

java - 如何防止 Spring Boot Batch 应用程序在执行测试时运行?