Filter mapped on forward dispatcher isn't invoked when JSF navigation is performed

前端 未结 1 376
小鲜肉
小鲜肉 2021-01-14 18:18

I\'m trying to code a simple JSF web application with a login system using Tomcat 7.

I have got two pages: index.xhtml and /restricted/welcome.xhtml.

Pages b

1条回答
  •  -上瘾入骨i
    2021-01-14 19:11

    The FORWARD dispatcher is only triggered when RequestDispatcher#forward() is been invoked somewhere in the webapp's code. The standard JSF navigation handler doesn't do that. It just invokes ViewHandler#createView() and sets it as current view by FacesContext#setViewRoot().

    Send a redirect instead:

    public String proceed() {
        user.setLoggedIn(true);
    
        return "restricted/welcome.xhtml?faces-redirect=true";
    }
    

    This is by the way also the recommended practice. Now it's a bookmarkable page (the URL change is now reflected in browser's address bar) and refreshing/F5'ing the page won't result in the POST unnecessarily being re-executed and pressing back button won't result in surprises.

    If you really insist in invoking the FORWARD dispatcher using JSF, you can always use ExternalContext#dispatch() method, but this is not the recommended way.

    public void proceed() throws IOException {
        user.setLoggedIn(true);
    
        FacesContext.getCurrentInstance().getExternalContext().dispatch("restricted/welcome.xhtml")
    }
    

    0 讨论(0)
提交回复
热议问题