標籤:http java 使用 io ar 問題 cti 代碼 sp
引用
我這裡想知道的是同名的多個參數,會被自動的放置在List或者數組中,我想知道是怎麼實現的,因為取一個參數和取多個同名的參數是不同的方法:
一個是request.getParameter
一個是request.getParameterValues
先解釋一下:
Struts或則XWorlk提供的Interceptor,操作的是已經被封裝好的Parameters的Map了。相信你應該看到過這句話。(在CheckboxInterceptor中有)
Java代碼 收藏代碼
Map parameters = ai.getInvocationContext().getParameters();
關於你的問題:
如果你說的是,同名的多個參數如何以List或者數組的方式,放在Parameter的Map中。
這個應該是屬於Servlet規範中的內容。在HTTP協議的Request傳遞到ServletContainer中後,Container就會為該次請求產生一個HTTPServletRequest對象,在HTTPServletRequest對象中,參數和值,放入到了Map中。
在Tomcat的實現代碼中,可以看到,同名的參數的值,被加入的String數組中。單個的參數,也是放在數組中的。
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);
}
引用
因為取一個參數和取多個同名的參數是不同的方法:
一個是request.getParameter
一個是request.getParameterValues
都可以使用request.getParameterValues的方法,通過迴圈來得到值、或者賦值。
如果你說的是,同名的多個參數如何以List或者數組的方式,放在Action中。
那就需要看Struts的OGNL實現的源碼,Interceptor只是將值轉移到ValueStatck上,而後由ONGL進行賦值的。
總結:你在Interceptor中是看不到你期望的判斷語句的。因為,封裝的判斷語句在Servlet的容器中實現;解鎖裝的語句,在struts2的OGNL中實現。Interceptor只是對其中值,稍微做些處理
請求--攔截器--action經過