数组作为 Struts 2 中的获取参数 [英] Array as get parameter in Struts 2

查看:18
本文介绍了数组作为 Struts 2 中的获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的动作

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";
    }
}

我正在尝试通过在地址栏 http://localhost:8080/sm-shop/compare?pids=12,23,34 中键入以下 url 来发送数组.我想要的输出是

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

但我得到的是

pid = 122334

我尝试了谷歌搜索,但找不到如何做到这一点.请帮我找出问题所在.

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

推荐答案

需要多次传递参数pids:

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

如果您将 pids 属性声明为数组,Struts2 会自动将多个参数映射到数组.

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

这篇关于数组作为 Struts 2 中的获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆