0123xx的0123xx的稍微修改版本可以解決此問題。
而不是返回一個ViewContext你需要返回一個ViewToolContext。您還需要準備工具箱,並根據適用情況在會話/請求中設置它們:
您將需要以您需要的方式初始化toolContext(查看我提供的答案here有關如何使用更新。API,因爲你將需要訪問ToolboxFactory
修改後的createVelocityContext方法現在需要事先準備的工具箱以下列方式創建ViewToolContext:
protected Context createVelocityContext(Map <String, Object> model,
HttpServletRequest request,
HttpServletRespsone response)
throws Exception {
initVelocityContext(); //Still keep toolContext static
//will need to also add this to
//the servletContext -- left as an exercise
prepareToolboxes(request, response);
Context context =
new ViewToolContext(getVelocityEngine(), request,
response, getServletContext());
//Set model attrs to context
....
return context;
}
private void prepareToolboxes(final HttpServletRequest request,
final HttpServletResponse response) {
String key = Toolbox.class.getName();
if (factory.hasTools(Scope.REQUEST && request.getAttribute(key) == null) {
Toolbox requestTools = factory.createToolbox(Scope.REQUEST);
request.setAttribute(key, requestTools);
}
if (factory.hasTools(Scope.SESSION) {
HttpSession session = request.getSession();
synchronized(factory) {
//Follow pattern from above
}
}
}
事先警告,我有沒有完全測試所有的工具,但這確實解決了ParameterTool的問題。 – Scott