在 Thymeleaf 方言处理器中访问 HttpServletRequest 和 HttpServletResponse [英] Access HttpServletRequest and HttpServletResponse in a Thymeleaf dialect processor
问题描述
我正在尝试创建一个执行 ServletDispatcher.include 的 Thymeleaf 方言处理器.我扩展了 AbstractElementTagProcessor 并覆盖了 doProcess 方法.相关代码片段为:
@Overrideprotected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler){ServletContext servletContext = null;//TODO: 获取 servlet 上下文HttpServletRequest request = null;//TODO: 获取请求HttpServletResponse 响应 = null;//TODO: 获取响应//将调度器检索到组件 JSP 视图RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/something");//创建包装器(作为响应,但将输出存储在 CharArrayWriter 中)CharResponseWrapper wrapper = new CharResponseWrapper(response);//运行包含dispatcher.include(request, wrapper);字符串结果 = wrapper.toString();//使用返回的字符串创建模型最终 IModelFactory modelFactory = context.getModelFactory();最终 IModel 模型 = modelFactory.parse(context.getTemplateData(), result);//指示引擎用指定的模型替换整个元素structureHandler.replaceWith(model, false);
我过去以自定义 JSP 标记的形式编写了类似的代码.问题是:我不知道如何访问 ServletContext、HttpServletRequest 和 HttpServletResponse!这完全可以完成,还是我应该接受 Thymeleaf 太擅长隐藏 HTTP 上下文?
您可以访问请求(通过使用 #request
对象,该对象使您可以直接访问 javax.servlet.http.HttpServletRequest
对象)参数和会话(使用 #session
对象,可让您直接访问 javax.servlet.http.HttpSession
对象)属性直接在 Thymeleaf 视图中:
${#request.getAttribute('foo')}${#request.getParameter('foo')}${#request.getContextPath()}${#request.getRequestName()}<p th:if="${#request.getParameter('yourParameter') != nullth:text="${#request.getParameter('yourParameter')}"}">请求参数</p>${#session.getAttribute('foo')}${#session.id}${#session.lastAccessedTime}<p th:if="${session != null}">th:text="${session.yourAttribute}"</p>
在此处了解更多信息.>
I'm trying to create a Thymeleaf dialect processor which performs a ServletDispatcher.include. I have extended the AbstractElementTagProcessor and overridden the doProcess method. The relevant code fragment is:
@Override
protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {
ServletContext servletContext = null; // TODO: get servlet context
HttpServletRequest request = null; // TODO: get request
HttpServletResponse response = null; // TODO: get response
// Retrieve dispatcher to component JSP view
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/something");
// Create wrapper (acts as response, but stores output in a CharArrayWriter)
CharResponseWrapper wrapper = new CharResponseWrapper(response);
// Run the include
dispatcher.include(request, wrapper);
String result = wrapper.toString();
// Create a model with the returned string
final IModelFactory modelFactory = context.getModelFactory();
final IModel model = modelFactory.parse(context.getTemplateData(), result);
// Instruct the engine to replace this entire element with the specified model
structureHandler.replaceWith(model, false);
I wrote similar code in the past in the form of a custom JSP tag. Problem is: I don't know how to access the ServletContext, HttpServletRequest and the HttpServletResponse! Can this be done at all, or should I just accept that Thymeleaf is too good at hiding the HTTP context?
You can access request (by using #request
object that gives you the direct access to javax.servlet.http.HttpServletRequest
object) parameters and session (with #session
object that gives you direct access to the javax.servlet.http.HttpSession
object) attributes directly in Thymeleaf views:
${#request.getAttribute('foo')}
${#request.getParameter('foo')}
${#request.getContextPath()}
${#request.getRequestName()}
<p th:if="${#request.getParameter('yourParameter') != null
th:text="${#request.getParameter('yourParameter')}"}">Request Param</p>
${#session.getAttribute('foo')}
${#session.id}
${#session.lastAccessedTime}
<p th:if="${session != null}"> th:text="${session.yourAttribute}"</p>
Read more here.
这篇关于在 Thymeleaf 方言处理器中访问 HttpServletRequest 和 HttpServletResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!