比如页面上有
页面被提交后,在后台action用List kp = new ArrayList(); 就可以得到3个input的值了
我知道同名checkbox可以用系统自带的拦截checkbox的拦截器拦截获得选中的值,但是多个name相同的input text是怎么在后台得到的数据,是什么原理?是param拦截器来做这个事吗?有谁能给讲解下原理吗?谢谢
比如页面上有
页面被提交后,在后台action用List kp = new ArrayList(); 就可以得到3个input的值了
我知道同名checkbox可以用系统自带的拦截checkbox的拦截器拦截获得选中的值,但是多个name相同的input text是怎么在后台得到的数据,是什么原理?是param拦截器来做这个事吗?有谁能给讲解下原理吗?谢谢
[quote]我这里想知道的是同名的多个参数,会被自动的放置在List或者数组中,我想知道是怎么实现的,因为取一个参数和取多个同名的参数是不同的方法:
一个是request.getParameter
一个是request.getParameterValues [/quote]
[b]先解释一下:[/b]
[color=blue]
Struts或则XWorlk提供的Interceptor,操作的是已经被封装好的Parameters的Map了。相信你应该看到过这句话。(在CheckboxInterceptor中有)[/color]
[code="java"] Map parameters = ai.getInvocationContext().getParameters();[/code]
[b]关于你的问题:[/b]
[b]如果你说的是,同名的多个参数如何以List或者数组的方式,放在Parameter的Map中。[/b]
[color=blue][b]这个应该是属于Servlet规范中的内容。在HTTP协议的Request传递到ServletContainer中后,Container就会为该次请求生成一个HTTPServletRequest对象,在HTTPServletRequest对象中,参数和值,放入到了Map中。
在Tomcat的实现代码中,可以看到,同名的参数的值,被加入的String数组中。单个的参数,也是放在数组中的。[/b][/color]
[code="java"]
/**
* Put name and value pair in map. When name already exist, add value
* to array of values.
*
* @param map The map to populate
* @param name The parameter name
* @param value The parameter value
*/
private static void putMapEntry( Map map, String name, String value) {
String[] newValues = null;
String[] oldValues = (String[]) map.get(name);
if (oldValues == null) {
newValues = new String[1];
newValues[0] = value;
} else {
newValues = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
newValues[oldValues.length] = value;
}
map.put(name, newValues);
}[/code]
[quote]因为取一个参数和取多个同名的参数是不同的方法:
一个是request.getParameter
一个是request.getParameterValues[/quote]
[color=blue][b]都可以使用request.getParameterValues的方法,通过循环来得到值、或者赋值。[/b][/color]
[b]如果你说的是,同名的多个参数如何以List或者数组的方式,放在Action中。[/b]
[color=blue][b]
那就需要看Struts的OGNL实现的源码,Interceptor只是将值转移到ValueStatck上,而后由ONGL进行赋值的。[/b][/color]
[b]
总结:你在Interceptor中是看不到你期望的判断语句的。因为,封装的判断语句在Servlet的容器中实现;解封装的语句,在struts2的OGNL中实现。Interceptor只是对其中值,稍微做些处理[/b]