Struts 2 : Passing array of String as static param

自作多情 提交于 2019-12-23 19:08:10

问题


I wanted to declare an action in such a way that I could pass String array of static parameter. I tried the below code:

<action name="saveRecord" class="saveRecordAction">
        <result name="success" type="tiles">tiles:saveRecordSuccess</result>
        <param name="names">name1</param>
        <param name="names">name2</param>
        <param name="names">name3</param>
    </action>

I have a setter in my action class:

public void setNames(String[] name){
    mNames = name;
}

But I am only receiving one name which is the last one, "name3"

Is what I wanted possible? if yes, what is the correct way of doing it?


回答1:


The struts static param works like MAP. name been the KEY and value as VALUE. You can achieve your requirement by sending the values as comma separated and you can split it so that you can have your array there.

<param name="names">name1,name2,name3</param>

For more info on Static Parameters




回答2:


AFAIK static parameters are converted to String, that why various interceptors (e.g. FileUploadInterceptor) use static method commaDelimitedStringToSet of TextParseUtil to convert String to Set<String>. You can call this method inside setter for your property.

public void setNames(String name) {
    mNames = TextParseUtil.commaDelimitedStringToSet(name);
}



回答3:


Try this

    <action name="saveRecord" class="saveRecordAction">
            <result name="success" type="tiles">tiles:saveRecordSuccess</result>
            <param name="names" value="new java.lang.String[]{'name1', 'name2', 'name3'}"></param>

</action>



回答4:


try names[] instead names

 <action name="saveRecord" class="saveRecordAction">
            <result name="success" type="tiles">tiles:saveRecordSuccess</result>
            <param name="names[]">name1</param>
            <param name="names[]">name2</param>
            <param name="names[]">name3</param>
        </action>



回答5:


The static parameters processed by the staticParams interceptor via the action config where the parameters are defined as Map<String,String>.

You can see the example of using static parametesr in action config: Configure static parameter for Action class

For proper use of static parameters in the action config you should include reference to the interceptor staticParams or use the defaultStack.

You have also include setParams(Map<String,String> params) in the action to the interceptor set the values of the params. Use different keys name1, name2, name3 in your case to retrieve the values of the map. You can also try overwrite property (not documented) for the static params to not overwrite the value with the same key.



来源:https://stackoverflow.com/questions/14474492/struts-2-passing-array-of-string-as-static-param

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!