如何提供没有EL表达式的bean方法 [英] How to provide bean methods without EL expression

查看:85
本文介绍了如何提供没有EL表达式的bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在网站上被告知和发现的那样,只有在我的 .xhtml 上有调用它时,我的bean才会被执行。

As I was told and found on websites, my bean will only be executed if I have a call for it on my .xhtml.

是否可以在没有任何EL表达式的情况下调用我的bean?

Is it possible to call my bean without any EL expression?

我需要它是因为我的 HomeController 正在调用一种方法,该方法检查会话状态并在我的 home.xhtml 上进行检查,并且暂时不需要掉这个bean。

I need this because my HomeController is calling a method that checks for the session status and on my home.xhtml and don't have any need for fall this bean, for now.

推荐答案

您需要从另一个方向寻找解决方案。

You need to look for a solution in a different direction.

如果您正在使用用户身份验证而不是使用容器管理的身份验证,则通常针对特定的对象使用 servlet过滤器检查用户是否已登录的目的。

If you're homegrowing user authentication instead of using container managed authentication, then you normally use a servlet filter for the particular purpose of checking if an user is logged in or not.

servlet过滤器可以像servlet(例如 FacesServlet )映射到特定的URL模式一样。然后,它将在每个与该URL模式匹配的请求上调用它。您可以在过滤器中浏览请求/会话数据,然后通过继续过滤器链或发送重定向来相应地处理请求/响应。

The servlet filter can like as servlets (like FacesServlet) be mapped on a particular URL pattern. It will then be invoked on every request matching that URL pattern. You can explore request/session data in the filter and then handle the request/response accordingly by either continuning the filter chain, or by sending a redirect.

您需要实现 javax.servlet.Filter 接口。这是一个启动示例,假设您具有 @SessionScoped 托管bean,应如何实现 doFilter()方法 LoginController 。 JSF将会话范围的托管bean存储为 HttpSession 的属性。

You need to implement javax.servlet.Filter interface accordingly. Here's a kickoff example of how the doFilter() method should be implemented assuming that you've a @SessionScoped managed bean LoginController. JSF stores session scoped managed beans as attributes of HttpSession.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession(false);
    LoginController loginController = (LoginController) (session != null) ? session.getAttribute("loginController") : null;

    if (loginController == null || !loginController.isLoggedIn()) {
        response.sendRedirect(request.getContextPath() + "/login.xhtml"); // No logged-in user found, so redirect to login page.
    } else {
        chain.doFilter(req, res); // Logged-in user found, so just continue request.
    }
}

将此过滤器映射到覆盖受限页面的URL模式,例如 / app / *

Map this filter on an URL pattern covering the restricted pages, e.g. /app/*.

@WebFilter("/app/*")
public class LoginFilter implements Filter {
    // ...
}






更新,如果该URL模式也包含 login.xhtml ,而您确实无法更改,请更改 块如下:


Update if the login.xhtml is also covered by this URL pattern and you really can't change it, then change the if block as follows:

if (!request.getRequestURI().endsWith("/login.xhtml") && (loginController == null || !loginController.isLoggedIn())) {
    // ...
}

这篇关于如何提供没有EL表达式的bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆