Rob
Rob

Reputation: 3574

jsf bean not working

This is my xhtml:

<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>Keep alive</title>
    </h:head>
    <f:metadata>
        <f:viewParam name="value" id="value" value="#{myBean.val}" ></f:viewParam>
    </f:metadata>
    <h:body>

        Hello.<h:form><h:outputLabel value="#{myBean.val}"></h:outputLabel></h:form>
    </h:body>
</html>

And this is my bean:

 import javax.faces.bean.RequestScoped;
    import javax.faces.bean.ManagedBean;

        import javax.persistence.EntityManager;
        import javax.persistence.PersistenceContext;


        @RequestScoped
        @ManagedBean
        public class MyBean {


        @PersistenceContext(unitName = "myPUhere")
        private EntityManager em;
        /**
         * Creates a new instance of myBean
         */
        public MyBean() {
            System.out.println("mybeanload");
             if (getWaarde() == "yes") {
                System.out.println("IT WORKS!!");
            }

    }
    private String val;

    public String getVal() {
        System.out.println("getting value");
        return val;
    }



     public void setVal(String value) {
            System.out.println("setting value to " + value);
            this.val = value;
        }
}

My Bean does not respond to this, what don't I see here? It does not display the value I enter in the URL, nor it displays my outputLabel.

Upvotes: 0

Views: 2621

Answers (1)

BalusC
BalusC

Reputation: 1109570

So, you're retrieving the raw JSF source code in the browser instead of its generated HTML output. Browsers obviously don't understand JSF code (like as it doesn't understand JSP/PHP/ASP/etc code), but it only understands HTML code. This can happen when the FacesServlet hasn't been invoked, it's namely the one responsible for all the JSF works.

Perhaps your FacesServlet is based on some tutorial or IDE-autogenerated code been mapped on an URL pattern different than *.xhtml, such as *.jsf or *.faces. In that case, you've 2 options:

  1. Fix the request URL in your browser's address bar to match exactly that URL pattern. So, assuming that it's *.jsf, then don't open the page by

    http://localhost:8080/context/index.xhtml
    

    but instead by

    http://localhost:8080/context/index.jsf
    
  2. Fix the URL pattern to be *.xhtml directly. This wasn't possible back in JSF 1.x as the FacesServlet would otherwise call itself in an infinite loop, but this is quite possible in JSF 2.x and a lot of books/tutorials/resources/IDEs didn't take this into account.

    <url-pattern>*.xhtml</url-pattern>
    

See also:

Upvotes: 1

Related Questions