Search code examples
javajsfseam

Could JBoss Seam redirect user's "Last Visited Page" after the use logged in login page?


Note

I have ServletFilter which is used to check whether user already logged or not by using <url-pattern>. If the user not logged, the redirect to login.xhtml.

My Problem

After the user logged, my program is always redirect the dashboard.xml(based on the navigation-rule). I would like redirect last visited page automatically. Could you provide the possible way for that?

Currently My Soluction is work for that

But, I am not happy to use it. Does Seam support? Could you provide the better way?

In my ServletFilter, I keep last visited page as below

AuthenticationFilter.java

httpSession.setAttribute(Constants.ORIGINAL_VIEW_KEY, requestPath);

In my LoginBean, to redirect the last visited page after the user logged.

LoginBean.java

ELContext elContext = facesContext.getELContext();
Application application = facesContext.getApplication();
ExpressionFactory eFactory = application.getExpressionFactory();
ValueExpression binding = eFactory.createValueExpression(elContext, "#{" + Constants.VISIT_KEY_SCOPE + Constants.VISIT_KEY + "}", Visit.class);
binding.setValue(elContext, visit);

ValueExpression originalViewBinding = eFactory.createValueExpression(elContext, "#{" + Constants.ORIGINAL_VIEW_SCOPE + Constants.ORIGINAL_VIEW_KEY + "}", String.class);

String originalViewId = (String) originalViewBinding.getValue(elContext); <--- last visited view id.

UIViewRoot viewRoot = application.getViewHandler().createView(facesContext, originalViewId) ;
facesContext.setViewRoot(viewRoot);
facesContext.renderResponse();

Solution

  • If Identity and Credentials is used, the page redirected last visited page(to access the page, the use need to login) automatically after the logged. We have to need configure in component.xml as below.

    component.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <components ----->
       <event type="org.jboss.seam.security.loginSuccessful">
          <action execute="#{redirect.returnToCapturedView}"/>
       </event>
    </components>