开发者

navigation issues with Primefaces mobile using HttpServletRequest

I'm performing url redirects between primefaces mobile pages (pm:page). For instance from login.jsf to /secure/myPage.jsf, both pm:pages. After successful authentication the user should be redirect to myPage.jsf. The login is triggered like this:

       <pm:commandButton value="login" update="messages"                         
                     开发者_高级运维    actionListener="#{loginbean.doLogin}" >
             <f:param name="targetUrlParam" value="defaultTarget" />
       </pm:commandButton>

and the redirect within the method:

public void doLogin(ActionEvent e) throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext ec = context.getExternalContext();
    try {
        HttpServletRequest req = (HttpServletRequest) ec.getRequest();

        Authentication authentication = SecurityContextHolder.
                                        getContext().getAuthentication();

    ... // Authentication stuff with Spring Security

        try {
            HttpSession session = req.getSession(false);
            String cp = ec.getRequestContextPath();
            String redirectUrl = cp;

    ... //performing some filtering depending on Roles and target-urls
            }

            String encodedURL = ec.encodeResourceURL(redirectUrl);
            ((HttpServletResponse) ec.getResponse()).sendRedirect(encodedURL);

        } catch (AuthenticationException ae) {
            UtilBean.addErrorMessage("bad_credential");
        }

Unfortunately the redirect doesn't occur! It might have to do with the lifecycle of primefaces mobile 3.0M3 because everything works fine with normal JSF pages.

Any suggestions? Thanks


This is not entirely the right way to send a redirect in JSF. I'm not sure why it works in "normal" JSF (that should fail over there as well!). You basically need to call FacesContext#responseComplete() after the redirect to instruct JSF that it should not navigate to the default outcome. However, much better is to perform the redirect using ExternalContext#redirect() as it will do that implicitly.

So in your case, replace

((HttpServletResponse) ec.getResponse()).sendRedirect(encodedURL);

by

ec.redirect(encodedURL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜