大家好,我会快到这里是我的 HandlerInterceptorAdapter
public class PropertiesInterceptor extends HandlerInterceptorAdapter {
private final static Logger logger = Logger.getLogger(PropertiesInterceptor.class);
@Autowired
SessionManagerService sessionManagerService;
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (sessionManagerService.getSessionAttribute(request, AppProperties.USER_LOGGED.toString()) != null) {
modelAndView.addObject(AppProperties.USER_LOGGED.toString(), sessionManagerService.getSessionAttribute(request, AppProperties.USER_LOGGED.toString()));
}
}
}
正如你所看到的那样特别。这是控制器中的一些方法:
@RequestMapping(value = "/admin/user/profile/edit", method = RequestMethod.POST)
public String editProfile(@ModelAttribute("user") UserImpl user, HttpServletRequest request, ModelMap model, RedirectAttributes redirectAttributes) {
user = userService.saveEditedUser(user);
redirectAttributes.addFlashAttribute("infoMessage", true);
redirectAttributes.addFlashAttribute("successMessage", settingsService.getTranslate("user.message.editPersonalData", null, request));
sessionManagerService.setSessionAttribute(request, AppProperties.USER_LOGGED.toString(), user.getFirstName() + " " + user.getLastName() + " (" + user.getUsername() + ")");
return "redirect:/admin/user/profile";
}
在添加我的 HandlerInterceptorAdapter 之前,FlashAttributes完美地工作现在我只在顶部URL中看到来自 AppProperties.USER_LOGGED 的属性名为userLogged,有什么想法为什么它不再起作用?
编辑:
弹簧配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
">
<context:component-scan base-package="pl.allegroapp"/>
<mvc:interceptors>
<bean class="pl.allegroapp.interceptors.PropertiesInterceptor" autowire="constructor" />
</mvc:interceptors>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<jpa:repositories base-package="pl.allegroapp"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="transactionPersistenceUnit"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- Internationalization -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/languages/messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="pl"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
</beans>
我的春季版本是 4.1.4.RELEASE