Array as get parameter in Struts 2

◇◆丶佛笑我妖孽 提交于 2019-11-28 07:48:14

问题


I have an action like below

public class CompareAction {

    private Long[] pids;

    public Long[] getPids() {
        return pids;
    }

    public void setPids(Long[] pids) {
        this.pids = pids;
    }

    public String displayComparison() {
        for (Long pid : pids) {
            System.out.println("pid = " + pid);
            System.out.println();
        }
        return "success";
    }
}

I'm trying to send an array by typing following url in the addressbar http://localhost:8080/sm-shop/compare?pids=12,23,34. The output I want is

pid = 12

pid = 23

pid = 34

But what I'm getting is

pid = 122334

I tried googling but couldn't find how to do this. Please help me figure out whats wrong.


回答1:


You need to pass parameter pids multiple times:

http://localhost:8080/sm-shop/compare?pids=12&pids=23&pids=34

If you declared your pids property as array Struts2 will automatically map multiple parameters to array.




回答2:


If you want to keep this(http://localhost:8080/sm-shop/compare?pids=12,23,34) url format either you have to add a custom converter or you can make pids a String in your action and parse the array by splitting it at commas.



来源:https://stackoverflow.com/questions/19385476/array-as-get-parameter-in-struts-2

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