0

I have the following JSF page:

<h:head>
    <title>Admin Page</title>        
</h:head>
<h:body>

    <h1>Users List</h1>

    <h:dataTable value="#{adminBean.linkArr}" var="o" >

        <h:column>
            <f:facet name="header">User name</f:facet>
            #{o.username}
        </h:column>

    </h:dataTable>        

</h:body>

And a managed bean like this:

@ManagedBean(name="adminBean")
@SessionScoped
public class AdminBean {

private static UserLink[] linkArr;

public AdminBean() {
    FacesContext context = FacesContext.getCurrentInstance();  
    HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
    HttpSession session = request.getSession();

    String values[] = (String[])request.getSession().getAttribute("userList");

    int userNos = values.length;
    linkArr = new UserLink[userNos];

    for(int i = 0; i < userNos; ++i) {
        String tmp = values[i];
        linkArr[i] = new UserLink(tmp);
    }                
}
public UserLink[] getLinkArr() {
    return linkArr;
}

public static class UserLink {

    public String username;

    public UserLink(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }
}
}

What I am actually trying to do is:

Save some data from a previously called servlet by creating a session. Assuming that a managed session bean is only created when its associated XHTML file is called, I am getting the attributes from the session object and using then to populate a JSF table.

However, instead of a JSF table, I am getting the following output:

Users List

User name #{o.username}

Am I assuming a wrong lifecycle for a managed session bean or there is some problem in my JSF page?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rafay
  • 6,108
  • 11
  • 51
  • 71

1 Answers1

2

So, EL isn't evaluated at all? That can only mean that the current request URL (as you see in browser's address bar) didn't match the <url-pattern> of the FacesServlet as configured in webapp's web.xml. It's namely the one responsible for parsing the XHTML file into a JSF component tree and letting it to evaluate all EL expressions and generate valid HTML code. If you have done rightclick, View Source in the webbrowser, you should also have noticed that: all JSF tags are left unparsed instead of that their HTML representation is been generated.

So, to fix this problem, you need to make sure that the current request URL matches the URL pattern of the faces servlet. So, if you have mapped it on *.jsf, then you should open the page by page.jsf instead of by page.xhtml. Alternatively, you can also just change the URL pattern of the faces servlet in web.xml to be *.xhtml, so that you never need to worry about virtual URLs.

Please note that this problem is unrelated to the session management. You might perhaps run into problems related to that sooner or later given the strange design, but that's thus a different problem.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot. This did the trick. Also please let me know whether my understanding of managed session beans is right? – Rafay Jan 09 '13 at 20:55
  • And I am using sessions to store data and retrieve them in the bean? Is this the right approach or there are better ways to do it? – Rafay Jan 09 '13 at 20:56
  • 2
    The answer would depend on why you're using a servlet. Further I wonder about the necessity to make the bean session scoped and to massage the list like that and to have a static variable as property. Even more, I wonder the usefulness of the whole bean. You could just do `#{username}` without the need for a bean. Perhaps the servlet's job should be done by the bean instead? – BalusC Jan 09 '13 at 21:00
  • Well I am completely new to JSF so you can expect some stupidity from me. I am still unclear about lifecycles of different beans (Would be helpful if you can point to some very basic and easy JSF tutorials). – Rafay Jan 09 '13 at 21:04
  • 1
    [This article](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html) should get you started to some extent. – BalusC Jan 09 '13 at 21:05
  • Furthermore, it is getting difficult for me to distinguish between the usage of jsp, servlets and jsf. I am taking a course in web programming and things are getting messed up here. I don't understand which one has advantage over the other. – Rafay Jan 09 '13 at 21:07
  • 2
    You could try to see JSF as an abstraction over Servlet API. E.g., `ExternalContext#getSessionMap()` returns a mapping of all attributes of `HttpSession`. JSF session scoped managed beans are basically stored as attributes of `HttpSession`. EL `#{}` will resolve attributes in respectively request, session and application scopes until first non-null attribute is found. So, `session.setAttribute("foo", foo)` in servlet makes it available by `#{foo}` in EL as well (and by `@ManagedProperty("#{foo}")` in JSF as well!) Also, our JSF wiki page may be helpful: http://stackoverflow.com/tags/jsf/info – BalusC Jan 09 '13 at 21:08
  • Well that makes some sense. Thanks a lot with your blog link and your help. – Rafay Jan 09 '13 at 21:12