【发布时间】:2017-07-10 00:29:42
【问题描述】:
基本上,我有一个控制器和两种不同的帐户类型。我想将视图显示为“adminDashboard.jsp”或“userDashboard.jsp”,具体取决于用户的类型,并将名为“user”的模型属性传递给这些视图中的每一个,但不同的对象具有不同的类,具体取决于在哪个视图上显示。目前,即使它隐藏在开关盒中,我的代码也只会读取先发生的那个,而不是属于“激活”开关的那个。
家庭控制器:
@Controller
@RequestMapping(value = "/dashboard")
public ModelAndView showDashboard(String userGroup) {
switch(userGroup) {
case "admin": {
ModelAndView("adminDashboard", "user", adminObject);
}
case "user": {
ModelAndView("userDashboard", "user", userObject);
}
}
}
adminDashboard.jsp
<html>
<head>
...
</head>
<body>
${user.adminId} <!-- reads this correctly -->
${user.userId} <!-- does not read this, which is good. -->
</body>
</html>
userDashboard.jsp
<html>
<head>
...
</head>
<body>
${user.adminId} <!-- reads this, which it should not -->
${user.userId} <!-- does not read this, but it should -->
</body>
</html>
有人可以向我解释为什么 admin 用户模型属性仍在传递,即使从技术上讲,代码甚至不应该进入那个 switch case?我想要做的甚至可能吗?如果有,有什么例子?
【问题讨论】:
-
假设你的 switch case 有问题。它不包含
break;你可以用if else块尝试相同的逻辑。 -
我已经按照您的建议进行了尝试,尽管它返回了 userDashboard 视图,但当我使用 ${user} EL 标记时它使用了管理员用户模型属性。很麻烦,因为模型属性只在与 OTHER 视图关联的 ModelAndView 对象中声明。
标签: java jsp spring-mvc tomcat el