13 Replies Latest reply on Mar 19, 2010 9:29 PM by Matthieu Chase Heimer

    Simplest Weld Example (Does not work)

    D. B. Newbie
      Hello,

      I have created a very simple dynamic web project using eclipse and added the following :

      - A class with its package, annotated with @Named

      package org.test;

      import java.io.Serializable;

      import javax.enterprise.context.SessionScoped;
      import javax.inject.Named;

      @Named
      @SessionScoped
      public class Hello implements Serializable {
           
           private static final long serialVersionUID = 1L;
           
           private final String text = "Hello!";

           public String getText() {
                return text;
           }

      }

      - A view called view.xhtml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
      <h:head>
           <title>Hello</title>
      </h:head>
      <h:body>
           <h1>Hello</h1>
           <p>My weld-injected bean says : <h:outputText value="#{hello.text}" /></p>
      </h:body>
      </html>

      - An empty beans.xml in WEB-INF

      - I have nothing i my lib folder

      - I have declared the JSF servlet in web.xml as follows :

           <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>*.jsf</url-pattern>
           </servlet-mapping>

      - I run this in JBoss jboss-6.0.0.M2

      ** The problem is that nothing gets displayed from my bean ! I only get the text : My weld-injected bean says :

      Anyone has an idea about what is going on !

      Thanks.
        • 1. Re: Simplest Weld Example (Does not work)
          Arbi Sookazian Master

          Have you added a debug brkpt in the getText() method?  Is the method invoked?

          • 2. Re: Simplest Weld Example (Does not work)
            Nicklas Karlsson Master

            add a faces-config.xml, too

            • 3. Re: Simplest Weld Example (Does not work)
              D. B. Newbie
              thx for your replies guys.

              I have added a faces-config and tested with the breakpoint too and getText does not get called, and i keep getting the same results.

              Here is what JBoss displays on startup :

              11:09:34,531 INFO  [org.jboss.weld.Version] WELD-000900 1.0.1 (CR1)
              11:09:34,813 INFO  [org.jboss.web.tomcat.service.deployers.TomcatDeployment] deploy, ctxPath=/JBossTest
              11:09:35,016 INFO  [javax.enterprise.resource.webcontainer.jsf.config] Initialisation de Mojarra 2.0.2 (FCS b10)

              My webapp is called JBoss test. When take of the beans.xml file i don't get the line :

              11:09:34,531 INFO  [org.jboss.weld.Version] WELD-000900 1.0.1 (CR1)
              • 4. Re: Simplest Weld Example (Does not work)
                Nicklas Karlsson Master

                Drop in the numberguess example, does it work?

                • 5. Re: Simplest Weld Example (Does not work)
                  D. B. Newbie
                  I have dropped the number guess in and this is what i get when JBoss starts up :

                  15:15:27,056 INFO  [Version] WELD-000900 1.0.1 (CR1)
                  15:15:27,085 WARN  [ControllerState] Deprecated usage of 'new ControllerState', use getInstance, caller: org.jboss.weld.integration.deployer.env.DynamicDependencyCreator.createDepenencies(DynamicDependencyCreator.java:73)
                  15:15:27,490 INFO  [TomcatDeployment] deploy, ctxPath=/numberguess
                  15:15:27,708 INFO  [config] Initializing Mojarra 2.0.2 (FCS b10) for context '/numberguess'

                  Now, http://localhost:8080/numberguess/home.jsf displays a page with :

                  Guess a number...
                  I'm thinking of a number between  and . You have  guesses remaining.
                  Your guess: Guess
                  Reset

                  The Guess button is disabled and no values are displayed at "between and ."

                  When i click on reset i get the following stack trace :

                  javax.servlet.ServletException: javax.el.PropertyNotFoundException: /home.xhtml @29,107 action="#{game.reset}": Target Unreachable, identifier 'game' resolved to null
                       javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
                       org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:66)


                                
                             

                             

                             


                     

                     
                     
                  • 6. Re: Simplest Weld Example (Does not work)
                    Nicklas Karlsson Master

                    Try updating Weld in the AS. Download 1.0.1-Final, unzip and go the the jboss-as folder and edit the jboss.home property to point to the AS home and then run mvn in that folder (we all have maven 2.0.10 installed, right?) ;-)

                    • 7. Re: Simplest Weld Example (Does not work)
                      Matthieu Chase Heimer Newbie
                      Your url-pattern is wrong, it should be *.xhtml.

                      Also you only need to be using the transitional DTD in your facelet.
                      • 8. Re: Simplest Weld Example (Does not work)
                        Nicklas Karlsson Master

                        you mean the default suffix param?

                        • 9. Re: Simplest Weld Example (Does not work)
                          Matthieu Chase Heimer Newbie
                          I mean the url-pattern in his web.xml. He has *.jsf. You need to either put all your facelet pages underneath a directory and use a "*" pattern or use the "*.xhtml" pattern which allows you to place the facelet pages in any location.

                          The OP is more than likely requesting the view.xhtml in his web browser which is by-passing the JSF servlet, returning the source as a static xhtml page. I bet if he views the source html is his web browser he will see the non-rendered <h:outputText value="#{hello.text}" /> tag.
                          • 10. Re: Simplest Weld Example (Does not work)
                            Nicklas Karlsson Master

                            Doesn't explain the numberguess issue.

                            • 11. Re: Simplest Weld Example (Does not work)
                              D. B. Newbie

                              So far, i have tried to put .xhtml as the default suffix param but this generates errors with a Stack Overflow.


                              Have not tried with the new weld version but instead created another project with the maven archetype and every thing works fine with this same version. I could repair my app by replacing my WEB-INF folder with the one generated by the maven archetype, but could not fix the number guess app !


                              Don't know what is going on ! Is it WTP that does not work correctly ?


                              Could anyone point me out to a simple WTP tutorial with Weld.

                              • 12. Re: Simplest Weld Example (Does not work)
                                D. B. Newbie

                                Just a precision : i used the weld maven archetype that i found here : http://www.seamframework.org/Documentation/WeldQuickstartForMavenUsers

                                • 13. Re: Simplest Weld Example (Does not work)
                                  Matthieu Chase Heimer Newbie
                                  >> So far, i have tried to put .xhtml as the default suffix param but this generates errors with a Stack Overflow.

                                  As I said, NOT the suffix setting. The url-pattern should either be *.xhtml" or "somedir/*". Don't even include the suffix setting in your web.xml. If you view your WEB-INF/web.xml you'll find a difference between the working and non-working versions.

                                  Just in-case you aren't clear, the suffix setting (which you shouldn't need) is:
                                     <context-param>
                                        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                                        <param-value>.xhtml</param-value>
                                     </context-param>

                                  The url-pattern, that you should have for your hello world app is:
                                     <servlet-mapping>
                                        <servlet-name>Faces Servlet</servlet-name>
                                        <url-pattern>*.xhtml</url-pattern>
                                     </servlet-mapping>

                                  Using *.jsf is a poor choice because it allows people to bypass the JSF implementation and view you facelet source. See http://weld-numberguess.appspot.com/home.xhtml and http://weld-numberguess.appspot.com/home.jsf for an example of what I'm talking about. It's confusing for developers and end-users.

                                  You probably have a deployment descriptor problem with the numberguess app, there are several web.xml files you have to choice from. You have to combine the contents of two different WEB-INF directories to get all the xml files you need. You probably either had the wrong or missing web.xml and/or a missing beans.xml in the numberguess app you deployed.

                                  As a suggestion, stop messing around with maven for now, just use the simplest method you have at your disposal to create a Java EE 6 web project.

                                  Please note that your initial problem wasn't Weld related. You are having issues correctly configuring your deployment descriptor for JSF. Start with a facelet page that just has <h:outputText value="Hello" />. Once you get that working you can add the beans.xml file and start trying out Weld.