让Struts为不同的角色显示不同的JSP
我们有一个 Struts 2 Web 应用程序,几乎每个员工都使用它来管理和配置我们服务器场的作业。 我们正在制定一项计划,为客户制作一个仪表板,以便他们可以看到自己的作业,并非常简单地显示其状态(“正在进行中”、“准备打样”、“已完成”等)。 客户可以看到的所有页面的信息都比员工查看的信息少得多,并且无法编辑或更改任何内容。 但最终,它们本质上是同一信息的两种不同视图:一种非常简单,一种更复杂且可控。
执行此操作的简单方法是在每个 jsp 中都包含 if/else:
<s:if test="user.role == 'customer'">
<!-- TODO - Display simple customer view -->
</s:if>
<s:else>
<!-- TODO - Display complex employee view -->
</s:else>
是否有更简单的方法来执行此操作? 我可以创建两个单独的 jsps 目录,一个名为“customer”,一个名为“employee”(或默认值或其他目录),然后让 Struts 在我的操作中关闭某个属性来决定要检查哪个目录?
或者还有其他方法可以做到这一点吗?
We have a Struts 2 web application that's used by pretty much every employee to manage and configure jobs for our server farm. We're working on a plan to make a dashboard for customers, so they can see their own jobs, and a very simple display of its status ("in process", "ready for proofing", "finished", etc). All of the pages the customers can see will have much less information than the employees' views, and there will be no way to edit or change anything. But in the end, they're essentially two separate views of the same information: one very simple, one more complex and controllable.
The naive way to do this, is to have if/elses in every single jsp:
<s:if test="user.role == 'customer'">
<!-- TODO - Display simple customer view -->
</s:if>
<s:else>
<!-- TODO - Display complex employee view -->
</s:else>
Is there a simpler way to do this? Can I create two separate directories of jsps, one named "customer" and one named "employee" (or default or something) and then have Struts key off of a property in my action to decide which directory to check?
Or is there another way that I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在您的控制器内管理它。 为客户添加新视图,然后检查控制器中的用户角色并显示适当的视图。 这将使您现有的视图保持不变,并增加为客户提供真正自定义视图的能力。
I would suggest managing this inside your controller. Add a new view for the customer, and check the user's role in your controller and display the appropriate view. This will leave your existing view's untouched, and adds the ability to have truly custom views for your customers.
从这个问题来看,并不清楚这两种观点是否完全不同,或者客户是否有额外的东西,或者员工是否有额外的东西。 如果一个角色有额外的东西,只需将常见的部分放在一个文件中,将不常见的部分放在自己的文件中,然后使用 include 或tiles(或类似的东西)来组合 JSP 来制作页面。
如果两个视图完全不同,则它们应该是单独的 JSP,在这种情况下,操作可以根据角色转发到适当的视图,如 Rich Kroll 所建议的那样。
至于您询问复制结果定义的评论,当您有两个单独的互斥功能时,您需要两个代码路径。 您能做的最好的事情就是将共性分解到共享代码文件中。
如果您经常使用这种模式,那么简化操作代码的一种方法是选择与您所需的转发名称最匹配的 ActionForward。
因此,如果通常您会这样做,
您可以这样做,
将其放入基类的方法中,然后您的操作不需要知道它们将要进入什么视图。 那么你的 struts-config 可以是这样的:
在这种情况下,你不会通过名称动态查找 jsps 来确定它们是否存在,但至少你可以保持操作代码简单。
It's not exactly clear from the question if the two views are completely different, or if the customers have something extra, or if the employees have something extra. If one role has something extra, just put the common parts in one file and the uncommon parts in their own files and use includes or tiles (or something similar) to combine JSPs to make a page.
If the two views are completely different, they should be separate JSPs, in which case the action can forward to the appropriate view based on the role, as suggested by Rich Kroll.
As for your comment asking about duplicating the result definitions, when you have two separate mutually-exclusive features, you need two code paths. The best you can do is factor out the commonality into shared code files.
One way you can simplify your action code, if this pattern is something you do a lot, is to pick the ActionForward that best matches your desired forward name.
So, if normally you would do
You can do something like this
Put that in a method in your base class and then your actions don't need to know what view they are going to. Then your struts-config can be something like this:
In this case you are not dynamically looking up the jsps by name to determine whether or not they exist, but at least you can keep the action code simple.