生成网页时使用多态性?

发布于 2024-09-24 14:33:50 字数 612 浏览 2 评论 0原文

这是我遇到的一类问题的一个例子。

当用户来到我的应用程序的主页时,有两种可能性,他/她是:

  1. 已登录
  2. 未登录

如果用户已登录,我想在右上角显示用户名和一个显示“注销”的链接。如果用户未登录,我想显示一个显示“登录或注册”的链接。

以前,我会从 JSP 调用一些代码,这些代码会返回一段适当的 HTML,我会将其粘贴到我的页面中。但这可能不是最好的做法,尽管它很简单。

现在我相信我应该将对主页的初始请求发送到servlet(而不是jsp),它将实例化一些“HomePage”类型的对象,该对象具有子类“LoggedInHomePage”和“NotLoggedInHomePage”,具体取决于用户ID是否可用会话变量。我必须使用 if 或 case 语句确定正确的构造函数。

到目前为止,它看起来更整洁。但此时它再次变得混乱,因为我现在需要知道我的“Request 对象”中是否提供了 userId,为此我需要使用一些 if 语句,或者我使用不同的 jsp 模板,这意味着有两个服务器上的每个页面都有几乎相同的 jsp。

两者看起来都不优雅。非常感谢帮助。如果您对这个问题有一个巧妙的解决方案,请忽略我的描述并将其列出来,我不介意扔掉我在这里提出的任何想法。

This is an example of a type of problem I have been having.

When a user comes to the home page of my app there are two possibilities, he/she is:

  1. logged in
  2. not logged in

If the user is logged in I want to display the username in the top right and a link which says "logout". If the user is not logged in I want to display a link which says "login or signup".

Previously I would have called some code from the JSP which would return an appropriate piece of HTML that I would stick into my page. But this is probably not the best thing to do, although it is simple.

Now I believe I should send the initial request for the homepage to a servlet (instead of jsp), which will instantiate some object of type "HomePage" which has subclasses "LoggedInHomePage" and "NotLoggedInHomePage" depending on whether a user id is available in the session variable. I will have to determine the correct constructor with an if or case statement.

So far it seems neater. But at this point it get's messy again, because I now need to know whether or not I have userId being supplied in my "Request object" for which I need to use some if statements, or I use a different jsp template which implies having two almost identical jsps sitting on the server for every page.

Neither seems elegant. Help most appreciated. If you have a neat solution to this problem please just ignore my description and lay that out, I don't mind throwing away whatever ideas I have presented here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-01 14:33:51

我将简单地创建一个主页,其中包含一个登录组件,其职责是显示当前状态并在登录时执行必要的身份验证。

这是一种“有一个”关系,而不是“是一个”关系

I would simply create a single home page, with a login component whose responsibility is to display the current status and to perform the necessary authentication when logging in.

This is a 'has a' relationship rather than a 'is a'

风吹雪碎 2024-10-01 14:33:51

一种可能的解决方案:

HomePage 添加方法原型。称其为 getNavigationArea()getNavigationLinks() (比如说)。此方法将返回一组可以使用合适的 HTML 呈现的 Link 对象。

LoggedInHomePageNotLoggedInHomePage 都将重写此方法。前者的实现将提供一个表示“注销”的 Link 实例,而后者将提供一个表示“注册/注册”的实例。

JSP 将呈现导航区域,而不知道其内容是什么。这样,无论用户状态如何,您都将只有一个 JSP 页面。您还可以使用不同的导航选项添加其他类型的用户,例如 RegularUserAdminUser 等,而无需添加新的 JSP 页面。

One possible solution:

Add a method prototype to HomePage. Call it getNavigationArea() or getNavigationLinks() (say). This method will return say a set of Link objects that can be rendered using suitable HTML.

Both LoggedInHomePage and NotLoggedInHomePage will override this method. The implementation of the former will supply a Link instance that says "logout" while the latter will supply an instance that says "sign up/register".

The JSP will render the navigation area without knowing what its contents are. This way you will only have one JSP page irrespective of user status. Also you can add other type of users, say RegularUser, AdminUser etc. with different navigation options without having to add new JSP pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文