Spring MVC/AOP应用的动态菜单实现 [英] Implementing dynamic menu for Spring MVC/AOP application

查看:26
本文介绍了Spring MVC/AOP应用的动态菜单实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为我的 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).

这是我想要的,User 类,生成菜单如

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.

但这提出了两个问题:

  1. 我如何在 @AfterReturning 建议方法中获取 Model 实例,以防它在建议方法中丢失(如 .index())?
  2. 我如何获得用 @RequestMenuMapping 注释的所有方法(如在 Java 反射 Method 中)和类(如在 Java 反射 Class 中)为了建立完整的菜单索引?
  1. How do i get Model instance in @AfterReturning advice method in case it is missing in adviced method (as in .index())?
  2. How do i get all methods (as in java reflection Method) and classes (as in java reflection Class) 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...
  }
}

如果你想拦截你自己的Contoller,你可以写另一个切入点,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆