This discussion is archived
11 Replies Latest reply: Jun 7, 2012 7:24 AM by gimbal2 RSS

Fundamental JSF problem

janpla Newbie
Currently Being Moderated
I'm using GlassFish 3 (not sure about the release level - where can I see it?), NetBeans 7.1.1, Firefox 3.5.16. I am developing a very simple application using JSF; I have a working sample app (PetCatalog, which comes with NetBeans). As far as I can see my app is almost identical to the sample; but it does not work. This is the general idea: I create a list of users ('listusers') from a database - this works. Each user ID is a commandLink to an edit page ('useredit'). I have created a simple phase listener, from which I get this output:

Entering phase: RESTORE_VIEW 1. <--- /faces
Exiting phase: RESTORE_VIEW 1
Entering phase: RENDER_RESPONSE 6
Exiting phase: RENDER_RESPONSE 6--->/index.xhtml
==================================
Entering phase: RESTORE_VIEW 1. <--- /faces
Exiting phase: RESTORE_VIEW 1
Entering phase: RENDER_RESPONSE 6
initialise
Exiting phase: RENDER_RESPONSE 6--->/listusers.xhtml
==================================
Entering phase: RESTORE_VIEW 1. <--- /faces
Exiting phase: RESTORE_VIEW 1
Entering phase: RENDER_RESPONSE 6
initialise
Exiting phase: RENDER_RESPONSE 6--->/listusers.xhtml
==================================
Entering phase: RESTORE_VIEW 1. <--- /faces
Exiting phase: RESTORE_VIEW 1
Entering phase: RENDER_RESPONSE 6
initialise
Exiting phase: RENDER_RESPONSE 6--->/listusers.xhtml
==================================

The first entry (until '======') is from the index.xhtml; the second is from when I navigate to listusers. The last two are from clicking on a user ID - they should have called a method that would navigate to useredit, but clearly it just re-initialises listusers (the line with 'initialise' comes from a @PostContruct method).

I realise that I must have committed some fundamental error somewhere, but I have been over the files again and again, comparing to the PetCatalog app, and I can't spot any significant difference. I would have inserted some code, but there doesn't seem to be a good way of displaying it in this forum - or uploading files.

Does anybody have any idea what might be the cause of this problem?

Edited by: janpla on Jun 7, 2012 12:25 AM
  • 1. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    Use \
     tags to post code.
    
    If navigation doesn't work, you made a typo in the element's action attribute. Its as simple as that. You might have forgotten to make it an EL by wrapping it in #{} for example. Keep looking, it will be nothing mysterious, just something silly.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 2. Re: Fundamental JSF problem
    janpla Newbie
    Currently Being Moderated
    Hi gimbal2,

    Thanks for answering - I hope you are right.

    This is listusers.xhtml:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
        <h:head>
            <title>STAF User List</title>
        </h:head>
        <h:body>
            <h:outputStylesheet library="css" name="liststyle.css"  />
            <div id="header">
                <h1>STAF Users</h1>
            </div>"
            <div id="content">
                <h:form id="userform">
                    <h:dataTable id="users" value="#{userlist.userList()}" var="ul">
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="[ ]"/>
                            </f:facet>
                            <h:selectBooleanCheckbox id="usercheck" value="#{ul.checked}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="UID"/>
                            </f:facet>
                            <h:commandLink action="#{userlist.edituser(ul)}" value="#{ul.uid}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Name"/>
                            </f:facet>
                            <h:outputText value="#{ul.name}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Trust"/>
                            </f:facet>
                            <h:outputText value="#{ul.trust}"/>
                        </h:column>
                    </h:dataTable>
                </h:form>
            </div>
        </h:body>
    </html>
    {code}
    
    And userlist.java:
    
    {code}
    package userface;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.ejb.EJB;
    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;
    import userdb.Users;
    import userejb.UserList;
    
    /**
     *
     * @author Jan
     */
    @ManagedBean
    @SessionScoped
    public class userlist implements Serializable{
        private List<ctlruser> ulist=new ArrayList<ctlruser>();
        private String tracer;
        @EJB UserList ul;
        private Integer seluid;
        private useredit ue;
        private String debugstr;
    
        @PostConstruct 
        void initialise(){
            System.out.printf("initialise\n");
            seluid=-1;
            Integer i=0;
            for (Iterator<Users> it = ul.getUlist().iterator(); it.hasNext();) {
                ulist.add(i++,new ctlruser(it.next()));
            }
        }
    
        public String edituser(ctlruser u){
    //        ue=new useredit(u.getUid());
            System.out.printf("edituser\n");
            
            return "edituser.xhtml";
        }
    
        public String getTrace(){
            return tracer;
        }
    
        public String getDebugstr() {
            return debugstr;
        }
    
        public List<ctlruser> userList(){
            return ulist;
        }
    }
    {code}
    
    As you can see, I have commented out the first statement in edituser(), so if it gets called at all, it will print "edituser" - which it doesn't. And, of course, the output from the phase listener confirms this. I can't see where I have done anything wrong - and it's not for want of trying.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 3. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    Please, use the standard Java naming convention. Classnames start with a capital, so it is Userlist and Ctlruser. Or perhaps even UserList and CtrlUser.

    Hm, you are passing a bean to the action method from the view. Is this ctrluser class Serializable? Myself, I tend to pass a unique identifier which I can use to retrieve the object again on the server side, like that UID for example. As long as it is a primitive (int, long, String), not a complex object.

    So it could become:
    <h:commandLink action="#{userlist.edituser(ul.uid)}" value="#{ul.uid}"/>
    And on the server side:
      public String edituser(String uid){
            
         ctrluser u = someDao.getCtrluserByUID(uid); // whatever code you have that fetches the ctrluser object
         // do things and stuff with ctrluser
            
         return "edituser.xhtml";
      }
  • 4. Re: Fundamental JSF problem
    janpla Newbie
    Currently Being Moderated
    Is the Java naming convention important for JSF to work? I am reluctant to mess with it at this point - these files are checked into Perforce, and changing class names means changing file names which means having to do horrifying things on the perforce server (well, not really, but it is a lot more work than I want to do in the middle of debugging an urgent error).

    ctlruser was not Serializable, but I changed that and implemented your other suggestions:
    <h:commandLink action="#{userlist.editUser(ul.uid)}" value="#{ul.uid}"/>
    And:
        public String editUser(String uid){
    //        ue=new useredit(uid);
            System.out.printf("edituser\n");
            
            return "edituser.xhtml";
        }
    It doesn't make any difference, though. I have tried changing the parameter for editUser() to Integer, int and now String - same result.

    When I click on the commandLink, shouldn't JSF cycle into other phases than Restore View and Render Response? Even if it fails spectacularly?
  • 5. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    I'm not sure if the naming convention is important, I've never had to try it because I just follow the convention without thinking about it. What I do know is that JSF does a translation if you don't specify a managed bean names yourself; if your classname is UserList then on the facelets side (xhtml) the name is 'userList', so the first letter is lowercased (which makes it in line with the naming convention - variable names start with a lowercase letter.

    When in doubt, you can try giving the managed bean a specific name like @ManagedBean(name="userlist").
    public String editUser(String uid){
    so you changed the EL mapping in your xhtml file to editUser() also? Because previously it was edituser(), all lowercase.
  • 6. Re: Fundamental JSF problem
    janpla Newbie
    Currently Being Moderated
    Yeah, I changed the EL as well. Setting the name of the bean explicitly didn't help.

    Is there any more I can do in the way of tracing/debugging to see what is actually happening? Is it possible to debug what happens on the browser side (using Firefox)? I imagine the server knows, initially when I click the commandLink, that this is what I am doing; but I haven't found any way to confirm this in the phase listener.
  • 7. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    The trouble with JSF is that it manages its stuff by dumping large quantities of javascript in the view; you can't really deduce from the source or the HTTP request what is really going to happen.

    What is happening here is pretty screwy, so there must be something that is not as it is supposed to be. Do you perhaps suffer from a nested form (a form within a form)? That is not allowed and creates this type of unexpected behavior.
  • 8. Re: Fundamental JSF problem
    janpla Newbie
    Currently Being Moderated
    No, no nested forms - the page is literally what I posted earlier, very small and simple.

    I got hold of the JavaScript Debugger add-on, and I can see what you mean; that innocent 1 line in jsf.js turns out to be a rather large .js file without any line-breaks.

    Is it possible that there may be anything fishy in a configuration file somewhere?
  • 9. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    janpla wrote:
    Is it possible that there may be anything fishy in a configuration file somewhere?
    Not that I can think of really, especially in JSF 2.0. You may want to post the content of your web.xml just to be sure there is nothing fishy in there.

    EDIT: actually yes, make sure the servlet specification version that is noted in the web.xml webapp header is at least 2.5, but preferably even 3.0.


    Adding (outdated) libraries to the application that you shouldn't be adding is another cause of misery. There are no jars in the WEB-INF/lib of your war file right?

    Check out this excellent tutorial on how to setup a JSF webapp on Glassfish:

    http://balusc.blogspot.com/2011/01/jsf-20-tutorial-with-eclipse-and.html

    (Eclipse is used, but the IDE used should not make any difference). Perhaps you can spot a difference there.
  • 10. Re: Fundamental JSF problem
    janpla Newbie
    Currently Being Moderated
    This is my web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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-app_3_0.xsd">
        <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Production</param-value>
        </context-param>
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>faces/index.xhtml</welcome-file>
        </welcome-file-list>
    </web-app>
    I don't even have a lib directory under WEB-INF, so I guess that will be a no.

    I will check out the tutorial, I haven't come across that one before; thank you for all your help. Even though I still have the same problem, I have learned quite a few things. Maybe what I should do is start over, following the book exactly, step by step, and comparing with my working sample. That ought to work, or perhaps I should start with a JSF page and a shallow backing bean that just displays a hard-coded list of users - when the navigation works, I can start adding more layers and hopefully I find out which part breaks it.
  • 11. Re: Fundamental JSF problem
    gimbal2 Guru
    Currently Being Moderated
    Well one more then. Don't do this:
     return "edituser.xhtml";
    but this:
     return "edituser";
    In stead of a hard page you turn it into an "outcome". According to the conventions built into JSF 2, that will actually translate to navigating to the "edituser.xhtml" view because you don't have any navigation rules defined in the faces-config.xml file.

    Starting from a fresh project might be a good idea at this point. I would recommend not using any wizards built into the IDE; try to create everything from scratch (except the project itself of course!). Don't try to copy what you already saw, start from a requirement ("I want to show a list of users") and implement that according to information you can find.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points