我希望为我的 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
添加一些代表网站菜单的东西来建模。
但这提出了两个问题:
- 我如何在
@AfterReturning
建议方法中获取Model
实例,以防它在建议方法中丢失(如.index()
)? - 如何获取所有方法(如在 java 反射
Method
中)和类(如在 java 反射Class
中)用@RequestMenuMapping
注释为了建立完整的菜单索引?
最佳答案
我认为更好的解决方案是使用 bean 后处理器来扫描所有 Controller 类以查找 @RequestMenuMapping
和 HandlerInterceptor
以将菜单项添加到每个模型映射。
关于java - 为 Spring MVC/AOP 应用程序实现动态菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14003035/