0

I have a page containing a p:commandLink that calls a very simple method on a session-scoped backing bean. The backing bean method simply logs a message to the console and redirects to a welcome page.

When run, the link does nothing. There are no errors reported in h:messages and no stack traces on the server console. However, if I use the exact same code on another page then the link works fine.

The main difference between the pages is that the page containing the 'dead' link is protected by a login filter (the page is in the 'restricted' folder) whereas the page that contains the 'working' link is not in the restricted folder.

I've followed the advice in several other threads about action links that don't work. I don't have nested forms or rendering problems, etc. I'm wondering if the login filter may be having some effect on the link.

Here's the page containing the link that doesn't work (it resides in location: restricted/secret.xhtml). I've deliberately minimised the contents to eliminate other potential causes.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Restricted Page</title>
    </h:head>

    <h:body>
        <h:form>
            <h:messages />

            <br/>
            <p:commandLink action="#{loginBean.testMethod}" value="Test" />
        </h:form>
    </h:body>
</html>

Here's the method in the backing bean (which DOES work if called using the exact same commandLink code on an unprotected page:

public String testMethod() {
    logger.info("xxxxxxxxxxxxxx Test method called xxxxxxxxxxxxxx");
    return("WelcomeMember.xhtml");
}

The login filter is as follows:

package com.mymato.coop;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/restricted/*")
public class AuthenticationFilter implements Filter {

      @SuppressWarnings("unused")
      private FilterConfig config;

      public void doFilter(ServletRequest req, ServletResponse resp,
          FilterChain chain) throws IOException, ServletException {
        if (((HttpServletRequest) req).getSession().getAttribute(
            LoginBean.AUTH_KEY) == null) {
          ((HttpServletResponse) resp).sendRedirect("../login.xhtml");
        } else {
          chain.doFilter(req, resp);
        }
      }

      public void init(FilterConfig config) throws ServletException {
        this.config = config;
      }

      public void destroy() {
        config = null;
      }
}

For future reference, is there a good way to debug this type of problem? I've run into the dead link problem a few times now. It's very difficult to solve because it fails silently. At least if there was an error message then that would be a clue.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tommccann
  • 133
  • 1
  • 10
  • I haven't debugged the logic of this. I'm using code offered on another stackoverflow page. The filter appears to work if I request the secret.xhtml page. I do get redirected to the login page. – tommccann Jul 23 '16 at 11:36
  • Update: I'm not completely sure, but I think there is some sort of redirection loop going on. The restricted page is referring to itself and causing another redirection to the login page which is causing a redirection to the login page. I've been playing around with the redirection addresses and managed to get this working. – tommccann Jul 24 '16 at 07:38

0 Answers0