简体   繁体   English

Struts Action 中对象内地图属性的正确请求参数是什么?

[英]What is the correct request parameter for map proprerty inside an object in Struts Action?

Consider the following AnswerAction :考虑以下AnswerAction

public class AnswerAction extends CommonAction implements SessionAware {

    private CaseNumbering caseNumbering;
    (...)
}

And FormCaseNumberingFormCaseNumbering

public class FormCaseNumbering implements CaseNumbering {

private Duplicable innermostElement;
Map<Duplicable, Integer> caseNumbersOfDuplicableFormElements = new HashMap<>();
(...)
}

and DuplicableFormElementDuplicableFormElement

public class DuplicableFormElement implements Duplicable {

protected int id;
protected boolean duplicable;
}

In a Struts2 application where beans are injected via spring:在通过 spring 注入 bean 的 Struts2 应用程序中:

<bean id="caseNumbering" scope="prototype"
    class="model.FormCaseNumbering">
    <property name="innermostElement" ref="duplicableFormElement"></property>
</bean>


<bean id="answerAction" class="actions.AnswerAction" scope="prototype" autowire="byName">
    <property name="caseNumbering" ref="caseNumbering"/>
</bean>

Question:题:

How can you send a request parameter that populates the action with a caseNumbering containing a Map element with key duplicableElement.id=6 and value Integer=1 ?您如何发送请求参数,该参数使用 caseNumbering 填充操作,该 caseNumbering 包含键为 duplicableElement.id duplicableElement.id=6且值为Integer=1的 Map 元素?

My attempts:我的尝试:

Since the caseNumbering element is being injected via spring, I have no problem populating innermostElement , eg this works:由于caseNumbering元素是通过 spring 注入的,我没有问题填充innermostElement ,例如这有效:

(MockHttpServletRequest)request.setParameter("caseNumbering.innermostElement.id","9");

However the following:但是以下内容:

(MockHttpServletRequest)request.setParameter("caseNumbering.caseNumbersOfDuplicableFormElements[6]","1");

will fail with:将失败:

Invalid field value for field "caseNumbering.caseNumbersOfDuplicableFormElements[6]".字段“caseNumbering.caseNumbersOfDuplicableFormElements[6]”的字段值无效。

I have also tried adding an AnswerAction-conversion.properties with the following, with no luck:我还尝试使用以下内容添加 AnswerAction-conversion.properties ,但没有运气:

Key_caseNumbering.caseNumbersOfDuplicableFormElements=model.DuplicableFormElement
KeyProperty_caseNumbering.caseNumbersOfDuplicableFormElements=id
Element_caseNumbering.caseNumbersOfDuplicableFormElements=java.lang.Integer
CreateIfNull_caseNumbering.caseNumbersOfDuplicableFormElements=true

Is this possible in Struts2 (without needing to create a custom converter)?这在 Struts2 中是否可行(无需创建自定义转换器)?

This is a similar question: What is the correct request parameter for map proprerty inside an object in Struts Action?这是一个类似的问题: Struts Action 中对象内的映射属性的正确请求参数是什么?

And the answer, which can be found in struts2' mailing list , is still valid.并且可以在 struts2 的邮件列表中找到的答案仍然有效。

In short - there is a limitation in struts2 to handle this type of map and a custom converter is needed.简而言之 - 在 struts2 中处理这种类型的地图有一个限制,并且需要一个自定义转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM