1

I am using JSF 2.0 in a simple application. I have three beans 1st which is login is in request scope while other 2 in view scope. I have configured in faces-config.xml.

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
 <navigation-rule>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <from-outcome>loginSuccess</from-outcome>
            <to-view-id>/pages/ReportSubmit.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>reportStatus</from-outcome>
            <to-view-id>/pages/ReportStatus.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>logout</from-outcome>
            <to-view-id>/pages/logout.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
    <managed-bean>
        <managed-bean-name>UserBean</managed-bean-name>
        <managed-bean-class>com.cognizant.reportgen.LoginBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

    <managed-bean>
        <managed-bean-name>ReportBean</managed-bean-name>
        <managed-bean-class>com.cognizant.reportgen.ReportGeneratorBean</managed-bean-class>
        <managed-bean-scope>view</managed-bean-scope>
    </managed-bean>

    <managed-bean>
        <managed-bean-name>ReportStatus</managed-bean-name>
        <managed-bean-class>com.cognizant.reportgen.ReportStatusBean</managed-bean-class>
        <managed-bean-scope>view</managed-bean-scope>
    </managed-bean>

</faces-config>

I have a menu.jsp which has below code

menu item1 <h:commandLink action="loginSuccess" value="Generate Reports"></h:commandLink>
menu item 2<h:commandLink action="reportStatus" value="Report Status"></h:commandLink>

In 2 beans, I have methods whose return type is void.

On Login request, I am creating session and setting user detail in session attribute.

Now The problem which I am facing is that

  1. I login with user1, select menu item 1, so corresponding data is displayed.

  2. I login with user2 in next browser window, select menu item 1, so corresponding data is displayed.

  3. I go back to browser window1 (user1), select menu item 1 again, but now in header it displays the user2 name., Also it displays data corresponding to user2.

Please help me with this issue.

//ReportStatusBean.java
public class ReportStatusBean {
    private List<ReportAttrDO> reportList;
    private HtmlDataTable reportStatusTable;
    // getters and setter for above included....
    public void checkReportStatus(ActionEvent event) {

        ReportGenService reportGenObj = new ReportGenServiceImpl();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        UserDetail user = (UserDetail)session.getAttribute("user");
        List<ReportAttrDO> reportList = reportGenObj.getReportStatusList(user.getUserId());
        setReportList(reportList);
        if(reportList.isEmpty())
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No Records to display.", null));
    }

    public void viewReport(ActionEvent event) {
        ReportAttrDO reportAttrDO = (ReportAttrDO)getReportStatusTable().getRowData();
        System.out.println(reportAttrDO.getRequestHeaderId());
    }

}

// ReportStatus.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html>
<head>
....some script functions..
</head>
<body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">

<center>
    <div class="wrapper">
    <%@include file="../include/pageheader.jsp"%>
    <%@include file="../include/menu.jsp"%>
    <h:form id="form2">
    <table border="0">
    <tr>
            <td colspan="2"><h:messages style="color:red;margin:8px;" /></td>
        </tr>
        <tr>
            <td colspan="2"><h:commandButton value="Check Status" actionListener="#{ReportStatus.checkReportStatus}" /></td>
        </tr>
    </table>
                <c:if test="${not empty ReportStatus.reportList}" >
                <div style="height:200px;width:600px;overflow:auto;">

                <h:dataTable id="table" border="1"
                    var="row" value="#{ReportStatus.reportList}" cellpadding="5" cellspacing="5"
                    binding="#{ReportStatus.reportStatusTable}">

                    <h:column>
                        <f:facet name="header">
                            <f:verbatim>
                                <h:outputText value="Application Name" />
                            </f:verbatim>
                        </f:facet>
                        <h:outputText id="applName" value="#{row.applicationName}" ></h:outputText>

                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <f:verbatim>
                                <h:outputText value="Report Name" />
                            </f:verbatim>
                        </f:facet>
                        <h:outputText id="reportReqName" value="#{row.reportRequestName}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <f:verbatim>
                                <h:outputText value="Generated Report" />
                            </f:verbatim>
                        </f:facet>
                        <h:commandLink id="viewReport" value="View" rendered="#{row.status == 'Completed'}" actionListener="#{ReportStatus.viewReport}"></h:commandLink>
                    </h:column>
                </h:dataTable>
                </div>
                </c:if>
    </h:form>
    </div>
</center>
</body>
</html>
</f:view>
Soheb
  • 93
  • 2
  • 12

2 Answers2

1

HTTP session is shared among browser's tabs, so what you're experiencing is an expected behaviour. When you did login in a next tab you most probably replaced the existing session attribute.

You can as well try it in different browsers, but not different tabs of the same browser, to see it work as you expect it to work. In the end, session is a per user construct and should be viewed as such. You must define the proper scopes for your beans for the application to run in accordance with your expectations and for good user experience.

Unrelated to your concrete problem, using command links that use POST requests for plain page-to-page navigation is considered to be a bad practice: you should use <h:link> instead. For details see the third reference below.

Also, using faces-config.xml to solely configure navigation rules and declare managed beans in a JSF 2.0 application is somewhat old-school, in my opinion. The last but not the least, JSP is a deprecated view technology nowadays, that was superseded by facelets, so developing new application using JSP as a view technology should be carefully rethought.

See also:

  1. How to choose the right bean scope?;
  2. Communication in JSF 2.0, section on bean scopes;
  3. When should I use h:outputLink instead of h:commandLink?.
Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • 2
    Looks like someone has been reading lot of answers from BalusC and trying to imitate his style :) – Luiggi Mendoza Apr 14 '13 at 14:41
  • By the way, the problem is not in managed bean scope definition, is in these two lines: `HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); UserDetail user = (UserDetail)session.getAttribute("user");` (and then it comes the explanation you give about session handling) – Luiggi Mendoza Apr 14 '13 at 14:43
  • One more thing, OP's using JSTL among with JSF 2 code, you could also add [JSTL in JSF2 Facelets… makes sense?](http://stackoverflow.com/q/3342984/1065197) question on your *See also* list. – Luiggi Mendoza Apr 14 '13 at 14:45
  • @skuntsel, i tried in different browser windows but still the issue was coming. – Soheb Apr 14 '13 at 15:20
  • 1
    @LuiggiMendoza Looking at about 10 minutes on my post while writing comments you should have noticed the absense of separator and that question was thrice edited as well, thus making two of your comments premature :) By the way, you comments really made me laugh outloud :) Also, I think that showing OP the context and turning him to possible other issues is a thing worth imitating, hope you'll *replicate* that too, what do you think? – skuntsel Apr 14 '13 at 15:41
  • @Soheb You should try **not** *different browser windows*, but *windows of different browsers* instead. Words, words, words! – skuntsel Apr 14 '13 at 15:45
  • 1
    @skuntsel we all learn from the one who knows more than ourselves :) but still *Unrelated to your concrete problem* is indeed a known phrase from BalusC answers =P. – Luiggi Mendoza Apr 14 '13 at 16:49
0

You need to render your menu conditionnaly of your session user and not your login result, for example :

<h:form>
    <h:panelGroup rendered="#{not userbean.isLogged}">
        <h:inputText value="#{userBean.username}" />
        <h:inputText value="#{userBean.password}" />
        <h:commandButton actionListener="#{userBean.login}" />
    </h:panelGroup>

    <h:panelGroup rendered="#{userBean.isLogged}">
        <h:commandLink rendered="#{userBean.user eq "user1"}" action="loginSuccess" value="Generate Reports"></h:commandLink>
        <h:commandLink rendered="#{userBean.user eq "user2"}" action="reportStatus" value="Report Status"></h:commandLink>
    </h:panelGroup>
</h:form>

Of course it will require some function inside userBean to get the username for example.

Also you mean that you have configured your bean using ViewScoped, they are all RequestScoped in the code provided.

I suggest you to use .xhtml file name extensions, which is the standard in JavaServer Faces views.

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72