简体   繁体   中英

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

Consider the following AnswerAction :

public class AnswerAction extends CommonAction implements SessionAware {

    private CaseNumbering caseNumbering;
    (...)
}

And FormCaseNumbering

public class FormCaseNumbering implements CaseNumbering {

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

and DuplicableFormElement

public class DuplicableFormElement implements Duplicable {

protected int id;
protected boolean duplicable;
}

In a Struts2 application where beans are injected via spring:

<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 ?

My attempts:

Since the caseNumbering element is being injected via spring, I have no problem populating innermostElement , eg this works:

(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]".

I have also tried adding an AnswerAction-conversion.properties with the following, with no luck:

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)?

This is a similar question: What is the correct request parameter for map proprerty inside an object in Struts Action?

And the answer, which can be found in struts2' mailing list , is still valid.

In short - there is a limitation in struts2 to handle this type of map and a custom converter is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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