Struts 2 : Passing array of String as static param

前端 未结 5 442
生来不讨喜
生来不讨喜 2020-12-21 07:11

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



        
相关标签:
5条回答
  • 2020-12-21 07:23

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

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

    <interceptor-ref name="staticParams">
          <param name="parse">true</param>
          <param name="overwrite">false</param>    
    </interceptor-ref>
    

    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.

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

    0 讨论(0)
  • 2020-12-21 07:28

    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>
    
    0 讨论(0)
  • 2020-12-21 07:33

    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);
    }
    
    0 讨论(0)
  • 2020-12-21 07:36

    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

    0 讨论(0)
  • 2020-12-21 07:36

    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>
    
    0 讨论(0)
提交回复
热议问题