3 Replies Latest reply: Feb 26, 2013 2:22 AM by gimbal2 RSS

    simple jsf 2.1/ajax page, example code?

    993257
      hi,

      ive created a dynamic web project in eclipse juno, it uses mojarra and tomcat.

      i just want some kind of ajax behavior to get started, this is my xhtml page:
      <!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:ui="http://java.sun.com/jsf/facelets"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:f="http://java.sun.com/jsf/core" >
      
      <head>
      <title>Insert title here</title>
      </head>
      <body>
           <h:form> 
           
                <h:inputText id="myinput" value="">
                     <f:ajax render="outtext" event="keyup" listener="#{loginBean.update}"/>
                </h:inputText>
                <h:outputText id="outtext" value="#{loginBean.eventCount}"/>        
                
           </h:form>
      </body>
      </html>
      and this is LoginBean.java:
      import javax.faces.event.AjaxBehaviorEvent;
      
      public class LoginBean {
           
           private int eventCount = 0;
           private String name;
           private String password;
      
      
           public String getName ()
           {
                return name;
           }
      
      
           public void setName (final String name)
           {
                this.name = name;
           }
      
      
           public String getPassword ()
           {
                return password;
           }
      
           public void setPassword (final String password)
           {
           this.password = password;
           }
           
           public void update (AjaxBehaviorEvent event)
          {
              this.setEventCount(this.getEventCount() + 1);
          }
      
           public int getEventCount() {
                return eventCount;
           }
      
      
           public void setEventCount(int eventCount) {
                this.eventCount = eventCount;
           }
      }
      the error i get is: (right after starting the application, without any input)
      Feb 25, 2013 10:14:58 PM com.sun.faces.renderkit.RenderKitUtils renderUnhandledMessages
      INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
      sourceId=null[severity=(ERROR 2), summary=(One or more resources have the target of 'head', but no 'head' component has been defined within the view.), detail=(One or more resources have the target of 'head', but no 'head' component has been defined within the view.)]
      how can i fix this?
        • 1. Re: simple jsf 2.1/ajax page, example code?
          gimbal2
          Jsf2 should have f:head and f:body tags. Seems like whatever code generator you're using is generating jsf 1.2 stuff.

          Tip: paste errors you get directly into google. You are not the first one to run into this.
          • 2. Re: simple jsf 2.1/ajax page, example code?
            993257
            theres quite a lot of outdated information out there, even if you specify last year as max time search in google.
            and there are even wrong ones, not just one. look at mykongs ajax example if you want to see ajax with a full page reload.

            now with h:head and h:body the error is gone, but yet no ajax update when i type :(
            updated login.xhtml:
            <!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:ui="http://java.sun.com/jsf/facelets"
                 xmlns:h="http://java.sun.com/jsf/html"
                 xmlns:f="http://java.sun.com/jsf/core" >
            
            <h:head>
            <title>Insert title here</title>
            </h:head>
            <h:body>
                 <h:form> 
                 
                      <h:inputText id="myinput" value="">
                           <f:ajax render="outtext" execute="@this" event="keyup" listener="#{loginBean.update}"/>
                      </h:inputText>
                      <h:outputText id="outtext" value="#{loginBean.eventCount}"/>        
                      
                 </h:form>
            </h:body>
            </html>
            why is the ajax part still not working?

            Edited by: 990254 on Feb 25, 2013 2:44 PM
            • 3. Re: simple jsf 2.1/ajax page, example code?
              gimbal2
              Because you did something wrong. Keep looking and you'll figure it out.

              And yes: performing research means you have to wade through the junk on the internet, its what we all have to do. You to. The alternative is that every time you get stuck you go to a forum and have to wait for answers - that is a big waste of your time because you'll be stuck a lot I can predict. JSF isn't easy. I tend to go for the stackoverflow links, they usually give me solutions but you have to take the time to read through the answers in the threads. Like this:

              http://stackoverflow.com/questions/5476510/hinputtext-value-not-updating