Manually Set Map Value in Struts 2

本文详细介绍了如何使用Struts框架接收数据,包括列表类型数据的URL格式化,以及如何创建用于发送到地图的URL。同时讨论了映射数据的URL格式,并提到了特殊字符的正确转义方法。

you have to duplicate the format that Struts uses to receive the data. For example, to send a list of items (such as List<String> names;), you would format your URL to look like


&names=johnny&names=tim&names=bill

Maps are a little harder. To create a URL to send to a map, you can use the following format:


&variableName[key]=value

Say you have the following map:

Map<Integer, Integer> userIdOrderIdMap = new HashMap<Integer, Integer>();

To set the map from Struts 2, you’d use the following URL string:


&userIdOrderIdMap[0]=1&userIdOrderIdMap[10]=15

This is the equivalent of:


userIdOrderIdMap.put(0, 1);
userIdOrderIdMap.put(10, 15);

You will probably have to manually call encodeURIComponent() on the URL string to make sure the brackets and any other special characters are escaped properly, but you already knew that, right?

You can also use a dot notation (such as &variableName.key=value), but I find that a bit counterintuitive to the way KVC types are intended to be represented.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值