我想将一个对象填充为我的spring容器的每个新请求的模型属性。
我做了以下事情。 - 添加了一个新的拦截器,在其中将对象添加到请求对象 - 注册拦截器
拦截器正在执行,但控制器正在返回一个所有值为null的雇员对象。我做错了吗?
public class UserDetailsInterceptor extends AbstractInterceptor {
private static Class<? extends Annotation> annotationName = PopulateUserDetails.class;
public UserDetailsInterceptor() {
super(annotationName);
}
@Override
protected boolean handle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod)
throws Exception {
log.error("running my interceptor handle now");
Employee empoyee = populateEmployeeDetails();
request.setAttribute("employee",employee");
return true;
}
}
@Slf4j
@RestController
@CrossOrigin
public class TestController {
@RequestMapping(value = "/tst1", method = RequestMethod.GET)
@PopulateEmployee
public Employee getEmployee(@ModelAttribute("employee") Employee employee) {
return employee;
}
}
答案 0 :(得分:0)
而不是使用AbstractInterceptor使用HandlerInterceptorAdapter
UserDetailsInterceptor将是这样的
public class UserDetailsInterceptor extends HandlerInterceptorAdapter
{
Employee empoyee = null;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.error("running my interceptor handle now");
empoyee = populateEmployeeDetails();
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
ModelAndView modelAndView) throws Exception {
request.setAttribute("employee",employee");
}
}