请求--拦截器--action经过
引用
我這里想知道的是同名的多個參數,會被自動的放置在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只是對其中值,稍微做些處理
轉載于:https://www.cnblogs.com/Struts-pring/p/3937787.html
總結
以上是生活随笔為你收集整理的请求--拦截器--action经过的全部內容,希望文章能夠幫你解決所遇到的問題。