简体   繁体   English

处理会话过期后 JSF 页面变空白

[英]Handling JSF page getting blank after Session has expired

I have JSF pages which have links to other JSF pages .我有 JSF 页面,其中包含到其他 JSF 页面的链接。 When I click on the link and if the session has already expired , control goes to login page.当我点击链接并且会话已经过期时,控制转到登录页面。 And I will login with username password and again come to same page where I have clicked on link.我将使用用户名密码登录,然后再次来到我点击链接的同一页面。 Now I will click on the link, a blank page is getting displayed, and if i do page refresh by F5, the expected page loads.现在我将点击链接,显示一个空白页面,如果我按 F5 刷新页面,预期的页面将加载。

And also I have checked the console on my Jboss server, the View Expired exception is not appearing.而且我还检查了我的 Jboss 服务器上的控制台,没有出现 View Expired 异常。

SO I am bit confused which way I handle this to avoid blank page getting displayed.所以我有点困惑我用哪种方式处理这个以避免显示空白页面。

Please help.请帮忙。

This can happen if you're performing link navigation by ajax and the page returned after login was been served from the browser cache (and thus contains a form with an outdated view state identifier).如果您通过 ajax 执行链接导航并且登录后返回的页面是从浏览器缓存提供的(因此包含具有过时视图状态标识符的表单),就会发生这种情况。 You need to tell the browser to not cache restricted pages.您需要告诉浏览器不要缓存受限页面。 You can achieve this with the following filter:您可以使用以下过滤器实现此目的:

@WebFilter(servletNames={"Faces Servlet"})
public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        if (!req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            res.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    }

    // ...
}

Note that this problem thus also suggests that you're using (ajax) command links for page-to-page navigation.请注意,此问题因此也表明您正在使用 (ajax) 命令链接进行页面到页面导航。 This is a bad practice.这是一个不好的做法。 Those links are not SEO friendly nor bookmarkable.这些链接不是 SEO 友好的,也不是书签。 Command links should be used for form submits only.命令链接应仅用于表单提交。 Use normal links for page-to-page navigation.使用普通链接进行页面到页面导航。 See also When should I use h:outputLink instead of h:commandLink?另请参阅何时应该使用 h:outputLink 而不是 h:commandLink?

@BaluSC : Thanks for the reply. @BaluSC:感谢您的回复。

Used h:outputLink wherever possible.尽可能使用 h:outputLink。
And also I have handled the ViewExpiredException through the tag in web.xml.而且我还通过 web.xml 中的标记处理了 ViewExpiredException。

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/ErrorHandler</location>
  </error-page>

<servlet>
    <servlet-name>ErrorHandler</servlet-name>
    <servlet-class>com.common.beans.ErrorHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ErrorHandler</servlet-name>
    <url-pattern>/ErrorHandler</url-pattern>
  </servlet-mapping>

Used a servlet which handles this exception and redirects to the same page and loads the page properly.使用了处理此异常并重定向到同一页面并正确加载页面的 servlet。 And the doGet method of Servlet checks for ViewExpiredException. Servlet 的 doGet 方法检查 ViewExpiredException。

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
 {

      if (request.getAttribute("javax.servlet.error.exception") != null
         && request.getAttribute("javax.servlet.error.exception")
            .toString().contains("ViewExpiredException"))
      {

            String[] attributes =
               request.getAttribute("javax.servlet.error.request_uri")
                  .toString().split("/");
            StringBuilder redirectViewId = new StringBuilder("");
            for (int i = 2; i < attributes.length; i++)
            {
               redirectViewId.append("/").append(attributes[i]);
            }
            response.sendRedirect(FacesUtils.getFullRequestUrl(request)
               + redirectViewId);
         }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM