java - 为 Spring MVC/AOP 应用程序实现动态菜单

标签 java spring spring-mvc aop spring-aop

我希望为我的 Spring MVC 应用程序实现可动态更改的菜单(每当添加注释方法或 Controller 时更新)。

我想要的是引入新的注解 (@RequestMenuMapping),它将转到 @Controller bean 及其方法(就像 @RequestMapping 作品)。

这就是我想要的,User 类,生成像这样的菜单

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 添加一些代表网站菜单的东西来建模。

但这提出了两个问题:

  1. 我如何在 @AfterReturning 建议方法中获取 Model 实例,以防它在建议方法中丢失(如 .index() )?
  2. 如何获取所有方法(如在 java 反射 Method 中)和类(如在 java 反射 Class 中)用 @RequestMenuMapping 注释为了建立完整的菜单索引?

最佳答案

我认为更好的解决方案是使用 bean 后处理器来扫描所有 Controller 类以查找 @RequestMenuMappingHandlerInterceptor 以将菜单项添加到每个模型映射。

关于java - 为 Spring MVC/AOP 应用程序实现动态菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14003035/

相关文章:

java - 如果我想让我的 nvarchar 类型在 sql server 中按与它们在 java 中使用 unicode 时相同的顺序排序,应该使用什么排序规则

java - 当使用Deque作为堆栈或队列时,java如何知道使用哪种peek方法?

java - AcceptHeaderLocaleResolver 错误

java - 使用注释创建有条件运行条件运行 hibernate

java - 添加到通用列表<?> 产生编译错误

java - 使用jtree从文本文件到树结构 View

java - 有没有办法为Spring生成applicationContext.xml?

java - Spring Security中的身份验证失败,为什么?

java - 将数据从Controller传递到jsp

带有 quartz 调度程序的 spring 4