tr:commandLink操作方法不导航到下一页。再次加载同一页面

时间:2011-11-17 12:55:03

标签: jsf myfaces jsf-1.2

在我的JSF应用程序中,成功登录后,我将导航到我的应用程序的主页,其中有Logout链接。

以下是用于注销链接的相关html。

<div id="top_white">
        <div style="float: right"><tr:form><tr:commandLink id="logout" action="#{Controller.Logout}"><img alt="logout" src="../../images/logout.gif"/></tr:commandLink></tr:form></div>
    </div>

这是我的Controller sessionscoped

public class Controller {

    private static Logger LOGGER = Logger.getLogger(Controller.class);

    public Controller() {
        req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        session = req.getSession(false);
        String userid = session.getAttribute("userid").toString();
        if(List == null){
            List = new ArrayList<List>();
            List = new DAO().loadListByid(Long.valueOf(userid));
            LOGGER.debug("successfully loaded the list for the user");
        }
    }

    private long Username;
    private String Password=null;
    HttpServletRequest req;
    HttpSession session;


    private List<List> List=null;
    // getter and setters


    public String Logout(){
        session.invalidate();
        LOGGER.debug("In LogOut");
        return "loggedout";
    }
}

在构造函数中,我检查会话中的userid并将特定列表加载到用户。

我还有ServletFilter检查每个会话中的一些id。如果找不到,它会重定向到InvalidUser页面。我的过滤器被映射到/faces/jsp/*,以便jsp中的所有jsp在会话中检查文件夹的ID。

以下是我的过滤器的doFilter方法

public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

            LOGGER.debug("In doFilter");
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            HttpSession session = request.getSession(false);
            LOGGER.debug("request:" + request.getRequestURL().toString());
            try{
                if (session.getAttribute("id") != null) {
                    LOGGER.debug("id found:" + session.getAttribute("id"));
                    chain.doFilter(req, res); // id found, so just continue request.
                } 
            }catch(NullPointerException e){
                LOGGER.debug("No id found Exception: ", e);
                response.sendRedirect(properties.getProperty(INVALID_USER_REDIRECTURL, true)); // No id found, so redirect to Invalid user page.

            }

    }

我还添加了PhaseListener来调试JSF生命周期,该生命周期取自here

首次加载页面时,我看到以下只包含JSF生命周期的第一和最后阶段的服务器日志

["http-bio-8090"-exec-114] DEBUG SessionFilter - In doFilter
["http-bio-8090"-exec-114] DEBUG SessionFilter - request:http://my homepageurl
["http-bio-8090"-exec-114] DEBUG SessionFilter - id found
["http-bio-8090"-exec-114] ERROR org.apache.myfaces.config.FacesConfigurator - Configuration objects do not support clean-up. Update aborted
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE RESTORE_VIEW(1)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE RESTORE_VIEW(1)
["http-bio-8090"-exec-114] DEBUG org.apache.myfaces.lifecycle.LifecycleImpl - exiting from lifecycle.execute in RESTORE_VIEW(1) because getRenderResponse is true from one of the after listeners
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE RENDER_RESPONSE(6)
["http-bio-8090"-exec-114] INFO  org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory - Using LifecycleProvider org.apache.myfaces.config.annotation.AllAnnotationLifecycleProvider
["http-bio-8090"-exec-114] DEBUG Controller - id found in controller
["http-bio-8090"-exec-114] DEBUG DAOProperties - dao.properties loaded successfully
["http-bio-8090"-exec-114] DEBUG SvrConnection - In JNDI
["http-bio-8090"-exec-114] DEBUG DAO - Successfully connected to database
["http-bio-8090"-exec-114] DEBUG DAO - connection closed
["http-bio-8090"-exec-114] DEBUG Controller - successfully loaded the list for the user
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE RENDER_RESPONSE(6)

上面的日志是预期的,因为没有表单提交类型的东西。所以只列出了第一个和最后一个阶段。

但是当我点击我在本文顶部提到的Logout commandLink时,真正的问题就来了。

点击Logout链接后,这是服务器日志。

["http-bio-8090"-exec-114] DEBUG SessionFilter - In doFilter
["http-bio-8090"-exec-114] DEBUG SessionFilter - request:http://my homepage url
["http-bio-8090"-exec-114] DEBUG SessionFilter - appid found
["http-bio-8090"-exec-114] ERROR org.apache.myfaces.config.FacesConfigurator - Configuration objects do not support clean-up. Update aborted
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE RESTORE_VIEW(1)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE RESTORE_VIEW(1)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE APPLY_REQUEST_VALUES(2)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE APPLY_REQUEST_VALUES(2)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE PROCESS_VALIDATIONS(3)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE PROCESS_VALIDATIONS(3)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE UPDATE_MODEL_VALUES(4)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE UPDATE_MODEL_VALUES(4)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE INVOKE_APPLICATION(5)
["http-bio-8090"-exec-114] DEBUG Controller - In LogOut
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE INVOKE_APPLICATION(5)
["http-bio-8090"-exec-114] DEBUG org.apache.myfaces.lifecycle.LifecycleImpl - exiting from lifecycle.execute in INVOKE_APPLICATION(5) because getRenderResponse is true from one of the after listeners
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - START PHASE RENDER_RESPONSE(6)
["http-bio-8090"-exec-114] DEBUG LifeCycleListener - END PHASE RENDER_RESPONSE(6)
["http-bio-8090"-exec-116] DEBUG SessionFilter - In doFilter
["http-bio-8090"-exec-116] DEBUG SessionFilter - request:http://my homepage url
["http-bio-8090"-exec-116] DEBUG SessionFilter - No id found Exception:
java.lang.NullPointerException

从上面的日志中,正如预期的那样,在INVOKE_APPLICATION中调用了我的操作方法,并调用了我的控制器的Logout()方法,并且会话失效并返回字符串loggedout

我在faces-config.xml

中有以下导航规则
<navigation-rule>
    <from-view-id>/jsp/Services.jsp</from-view-id>
    <navigation-case>
      <from-outcome>loggedout</from-outcome>
      <to-view-id>/others/Logout.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>

但是这个navigation-rule没有执行。相反,在RENDER_RESPONSE阶段,它会尝试重新加载我的同一主页网址。显然,会话中没有导致id的{​​{1}}。

我在上面给出的各个文件夹中都有jsp。我使用的是JSF 1.2和myFaces 1.2.9。

1)为什么导航案例不会执行?

2)为什么它会重新加载我的主页网址?

请有人帮忙。

1 个答案:

答案 0 :(得分:0)

我发现这是JSF中的注销问题。对于tr:commandLink没有问题。

我在SO上阅读了一些关于JSF注销问题的帖子,并实现了相同的目标。

它按预期工作。