【发布时间】:2011-05-03 06:02:22
【问题描述】:
Spring 的 AOP 功能非常棒,它可以很容易地为控制器添加很酷且有用的注解。例如,我编写了一个 @Authenticated 注释,它允许经过身份验证的用户通过控制器方法或重定向到登录页面。有趣的东西。
然而,Spring 的控制器可以返回各种不同的类型。它们可以返回字符串、ModelAndView 对象,甚至是 void。我的代码库中有使用所有三种类型的方法。但是,我想更改我的 @Authenticated 注释以呈现并返回特定页面,我希望通过返回 ModelAndView 对象来做到这一点。是通过要求我的所有控制器方法返回 ModelAndView 来实现此目的的唯一方法吗?
我想要的控制器示例:
@Controller
public class MyController() {
@Authenticated
@RequestMapping("/myscore")
public String myScorePage(ModelMap model) {
return "myScorePage";
}
@Authenticated
@RequestMapping("/anotherPage")
public ModelAndView something() {
return new ModelAndView("anotherPage",someModelStuff());
}
}
@Aspect
public class NotVeryUsefulAspect {
@Around("@annotation(Authenticate)")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
if( isAuthenticated() ) {
return pjp.proceed();
} else {
return /* Oh no what goes here, I want to render a FAILURE page without redirecting */
}
}
}
【问题讨论】:
标签: spring spring-mvc spring-aop