Xhtml页面和HttpSession测试,没有jstl?

发布于 2024-09-07 11:28:37 字数 424 浏览 3 评论 0原文

我有一个 Java EE 中的动态 Web 应用程序,包含 JSF、Facelets、Richfaces。 我的页面都是xhtml页面。 所以 JSTL 不起作用。 为了使我的帐户页面和所有其他私有页面可访问,我想测试用户是否已连接,因此 HttpSession 中的属性 session 是否不为空。如果为空,用户将被重定向到欢迎页面。

我在我的 xhtml 页面中尝试过:

<jstl:if test="${sessionScope['session']==null}">
 <jstl redirect...>
</jstl:if>-->

但由于它不是 jsp 页面,因此无法工作。那么我应该在哪里测试会话是否不为空以允许用户查看他的私人页面? 在中央管理 bean 中?

I have a dynamic web application in Java EE with JSF, Facelets, Richfaces.
My pages are all xhtml pages.
So JSTL isn't working in it.
For my account pages and all other private pages to be reachable, I want to test if the user got connected, so if the attribute session in HttpSession is not null. If it's null, the user gets redirected in the welcome page.

I tried in my xhtml page :

<jstl:if test="${sessionScope['session']==null}">
 <jstl redirect...>
</jstl:if>-->

but as it's not jsp page it won't work. So where am I supposed to test if the session is not null to allow the user to see his private pages ?
in a central managed bean ?

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

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

发布评论

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

评论(1

停滞 2024-09-14 11:28:37

通常的位置是Filter

创建一个实现的类javax.servlet.Filter 并在 doFilter() 方法中编写以下逻辑:

if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {
    // Not logged in, so redirect request to login page.
    ((HttpServletResponse) response).sendRedirect("/login.jsf");
} else {
    // Logged in, so just continue request.
    chain.doFilter(request, response);
}

将此过滤器映射到 web.xml 上诸如 /private/*/secured/*/restricted/* 之类的 url-pattern,等等。

<filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.example.LoginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/private/*</url-pattern>
</filter-mapping>

如果您在 /private 文件夹中有私有页面,那么将调用此过滤器并相应地处理会话中登录用户的存在。

请注意,我将属性名称 session 重命名为 user,因为这样更有意义。 HttpSession 本身已经是会话。否则,对于检查/维护您的代码的其他开发人员来说,这会过于模糊和混乱。

The normal place for this is a Filter.

Create a class which implementsjavax.servlet.Filter and write the following logic in the doFilter() method:

if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {
    // Not logged in, so redirect request to login page.
    ((HttpServletResponse) response).sendRedirect("/login.jsf");
} else {
    // Logged in, so just continue request.
    chain.doFilter(request, response);
}

Map this filter in web.xml on an url-pattern of something like /private/*, /secured/*, /restricted/*, etc.

<filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.example.LoginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/private/*</url-pattern>
</filter-mapping>

If you have the private pages in the /private folder then this filter will be invoked and handle the presence of the logged-in user in the session accordingly.

Note that I renamed attribute name session to user since that makes much more sense. The HttpSession itself is namely already the session. It would otherise been too ambiguous and confusing for other developers checking/maintaining your code.

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