- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Spring MVC Web 应用程序在页面顶部有一个导航菜单,其中包含指向项目列表的链接。从数据库中检索项目列表。此菜单对应用程序中的所有页面都是通用的。目前,在每个 Controller 中,我检索并存储 session 中的项目列表(如果尚未在 session 中),以便它在每个页面上都可用。它工作正常,但我觉得应该有更好的方法来做到这一点。为了避免这种冗余,我现在尝试使用 HandlerInterceptorAdapter。我能够让它工作,但并不完美。第一次加载页面时,我没有看到我在 session 中设置的对象。但是,如果我刷新页面,我会在 session 中看到该对象。
我是这样创建拦截器的:
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
private MyService myService;
//Constructor
public MyHandlerInterceptor(MyService myService) {
this.myService = myService;
}
//Overridden method
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("In posthandle...");
if (request.getSession().getAttribute("items") == null) {
request.getSession().setAttribute("items", myService.getCategories());
}
}
}
我声明拦截器:
<mvc:interceptors>
<bean class="com.gyanify.webapp.interceptors.MyHandlerInterceptor"
autowire="constructor"/>
</mvc:interceptors>
我正在检查在 jsp 呈现时是否在 session 中设置了对象:
...
Session Attributes <br/>
<%
System.out.println("Printing from the jsp...");
Enumeration keys = session.getAttributeNames();
while (keys.hasMoreElements())
{
String key = (String)keys.nextElement();
out.println(key + ": " + session.getValue(key) + "<br>");
}
%>
...
这样,当我第一次加载页面时,我会在控制台中看到以下打印内容:
...
Returning view....(this is printed from the controller)
Printing from the jsp...
In posthandle...
...
我的理解是posthandle方法是在页面渲染之前调用的。但是,根据控制台上的输出,我看到 jsp 在拦截器的 posthandle 方法之前被渲染。
谁能帮我理解为什么会这样?
最佳答案
经过进一步调查,我发现问题出在我配置 spring 上下文 xml 文件的方式上。我有两个文件 - 一个用于应用程序上下文,另一个用于 Web 上下文。最初我在应用程序上下文 xml 中定义了 component-scan 和 mv:annotation-driven 标签:
<context:component-scan base-package="com.webapp" />
<mvc:annotation-driven validator="validator"/>
并在 web 上下文 xml 中定义了我的拦截器。
在 Debug模式下,我看到拦截器在处理请求后被映射到上下文。
为了让它工作,我将应用程序上下文中的配置更改为以下内容:
<context:component-scan base-package="com.webapp">
<context:exclude-filter type="regex" expression="com\.webapp\.controllers\.*"/>
</context:component-scan>
并将以下内容添加到网络上下文 xml 中:
<context:component-scan base-package="com.webapp.controllers" />
<mvc:annotation-driven validator="validator"/>
我仍在努力理解为什么这行得通而早期的配置却行不通。
关于java - 为什么在呈现页面后调用 HandlerInterceptorAdapter postHandle 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14092552/
请帮助 Spring MVC 新手。我很难做 HandlerInterceptorAdapter工作。 这是代码和配置文件。 mvc-dispatcher-servlet.xml:
之前的设计 我有一个如下构建的 Spring Controller : @RequestMapping(value = "/rest/funf/{datatype}", method = Reques
我正在尝试为我的 JSON 网络服务创建一个验证系统。这个想法是让一个拦截器捕获每个 Json @RequestBody,通过 JacksonMapping 用它创建一个 JSON 对象。然后从中读取
假设我有一些 FooInterceptor: public class FooInterceptor extends HandlerInterceptorAdapter { // ... } 在
可以使用 Zuul 配置添加 HandlerInterceptorAdapter。我需要拦截对特定资源的请求,但我想因为我有 Zuul 过滤器配置,所以拦截器永远不会被调用。 可以这样做吗? 最佳答案
我有一个扩展HandlerInterceptorAdapter的类: public class AreaRiservataInterceptorAdapter extends HandlerInter
我的 Spring MVC Web 应用程序在页面顶部有一个导航菜单,其中包含指向项目列表的链接。从数据库中检索项目列表。此菜单对应用程序中的所有页面都是通用的。目前,在每个 Controller 中
我目前有两个处理类似功能的拦截器。我想合并这些。 一个拦截器是一个访问请求记录器,它显示登录的用户、 session ID 和请求的 URL。 另一个拦截器是进程时间记录器。 访问记录器为了记录所有必
我试图将正文从 request.getReader() 中拉出来,但它已经被读取了。 我怎样才能得到拦截器中的请求体? 最佳答案 如您所说,请求正文只能从Reader 中读取一次。这不是特定于拦截器,
目前我正在从 Jersey 迁移到 Spring MVC,需要提供相同的行为和功能。 在以前的情况下,我们有一个 Jersey ContainerRequestFilter 来注册 Prometheu
我创建了一个自定义 HandlerInterceptorAdapter覆盖 postHandle方法: public class AcmeInterceptor extends HandlerInte
我正在尝试将授权 header 添加到我的请求中,作为我们在切换环境时的临时解决方法。我正在尝试在扩展 HandlerInterceptorAdapter 的拦截器中处理它。 我使用了 Mutable
这个问题在这里已经有了答案: Why is my Spring @Autowired field null? (21 个回答) 关闭8年前。 我在尝试 @Autowire 我的 @Service 时遇
我正在开发 Spring Web 服务。我想测试我的端点,但由于某种原因,我在运行测试时总是遇到以下异常: Caused by: org.springframework.beans.factory.N
我正在尝试记录(为了简单起见,现在只是为了控制台编写)将由 HttpServletResponse 返回的最终呈现的 HTML。 (即主体)为此,我使用 Spring MVC 中的 HandlerIn
在我的 Spring Boot 应用程序中,我试图在 afterCompletion() 中的响应中添加 header 我的方法HandlerInterceptorAdapter类。 Controll
我想在 session 超时时执行一些 Activity ,我使用了 HandlerInterceptorAdapter,如下所示 package com.practice.security; imp
我正在尝试记录(为了简单起见,现在只是为了控制台编写)将由 HttpServletResponse 返回的最终呈现的 HTML。 (即主体)为此,我使用 Spring MVC 中的 HandlerIn
经过长时间调试我的应用程序后,我发现了一些对我来说毫无意义的东西。每当我调用如下例所示注释的 Controller 时,我的拦截器就会执行两次。 @RequestMapping(method = Re
我有将 JSON 返回给客户端的 Controller 。 Controller 方法使用 mvc 注释进行标记,例如: @RequestMapping("/delete.me") public @R
我是一名优秀的程序员,十分优秀!