为Spring MVC / AOP应用程序实现动态菜单 [英] Implementing dynamic menu for Spring MVC/AOP application
问题描述
我希望为我的Spring MVC应用程序实现动态可更改的菜单(无论何时添加带注释的方法或控制器)。
I wish to implement dynamically changeable menu (updating whenever annotated method or controller added) for my Spring MVC application.
我想要的是引入新的注释( @RequestMenuMapping
)这将转到 @Controller
bean及其方法(就像 @RequestMapping
工作)。
What i want is to introduce new annotation (@RequestMenuMapping
) which will go to @Controller
beans and their methods (just like @RequestMapping
works).
Heres是我想要的,用户
类,生成菜单如
Heres is what i want, User
class, producing menu like
Users
Index | List | Signup | Login
:
@Controller
@RequestMapping("user")
@RequestMenuMapping("Users")
public class User {
@RequestMapping("")
@RequestMenuMapping("Index")
public String index(/* no model here - just show almost static page (yet with JSP checks for authority)*/) {
return "user/index.tile";
}
@RequestMapping("list")
@RequestMenuMapping("List")
public String list(Model model) {
model.addAttribute("userList",/* get userlist from DAO/Service */);
return "user/list.tile";
}
@RequestMapping("signup")
@RequestMenuMapping("Signup")
public String signup(Model model) {
model.addAttribute("user",/* create new UserModel instance to be populated by user via html form */);
return "user/signup.tile";
}
@RequestMapping("login")
@RequestMenuMapping("Login")
public String login(Model model) {
model.addAttribute("userCreds",/* create new UserCreds instance to be populated via html form with login and pssword*/);
return "user/login.tile";
}
}
我认为Spring AOP可以帮助我切入方法使用 @RequestMenuMapping
注释并通过 @AfterReturning
将代表网站菜单的内容添加到模型中。
I think that Spring AOP may help me to pointcut methods with @RequestMenuMapping
annotation and via @AfterReturning
add something representing web-site menu to model.
但这提出了两个问题:
- 我如何获得
模型
实例@AfterReturning
通知方法,以防顾问方法中缺少(如.index()
)? - 我如何获得所有方法(如java反射
方法
)和类(如java反射<$) c $ c> Class )用@RequestMenuMapping
注释以构建完整的菜单索引?
- How do i get
Model
instance in@AfterReturning
advice method in case it is missing in adviced method (as in.index()
)? - How do i get all methods (as in java reflection
Method
) and classes (as in java reflectionClass
) annotated with@RequestMenuMapping
in order to build complete menu index?
推荐答案
InterceptorDemo:
InterceptorDemo:
@Aspect
@Component
public class InterceptorDemo {
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void requestMapping() {
}
@Pointcut("@annotation(you.package.RequestMenuMapping)")
public void requestMenuMapping() {
}
@AfterReturning("requestMapping() && equestMenuMapping()")
public void checkServer(JoinPoint joinPoint,Object returnObj) throws Throwable {
Object[] args = joinPoint.getArgs();
Model m = (Model)args[0];
// use joinPoint get class or methd...
}
}
如果你想用自己的方法拦截控制器,你可以用另一个切入点和 ProceedingJoinPoint
对象来获得你想要的东西。
If you want to intercept Contoller with you own, you can wirte another pointcut and ProceedingJoinPoint
object can get what you want.
这篇关于为Spring MVC / AOP应用程序实现动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!