14 Replies Latest reply on Nov 14, 2009 6:10 AM by luxspes

    Weld + JSP: Any examples somewhere?

      I could not find an example of Weld and plain old JSP...
      AFAIK it should be possible to call a Weld managed bean from JSP page...
      Is that not accurate? Do I need to use JSF?

        • 1. Re: Weld + JSP: Any examples somewhere?

          Found the place here I read that Weld and JSP should play well together: here it is.


          So... theoretically all I have to do is have this in web.xml:


          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5"
             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_2_5.xsd">
             
             <display-name>Weld JSP example</display-name>
                
             <listener>
                <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
             </listener>
             
             <session-config>
                <session-timeout>10</session-timeout>
             </session-config>
          
             <resource-env-ref>
                <description>Object factory for the CDI Bean Manager</description>
                <resource-env-ref-name>BeanManager</resource-env-ref-name>
                <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
             </resource-env-ref>
          
          </web-app>
          



          And then I should be able to use Weld beans in my Jsp pages as if I were using jsp:useBean ?


          • 2. Re: Weld + JSP: Any examples somewhere?
            asookazian

            why would you need to use JSP with Weld?  Legacy app??

            • 3. Re: Weld + JSP: Any examples somewhere?

              Legacy app(s) (lots of them) are a reason, lots of developers with experience with JSP are another reason, not being so convinced that JSF is a such a good idea after using it for a couple of years, one more reason.

              • 4. Re: Weld + JSP: Any examples somewhere?

                So wrote this class:


                package testingweld;
                
                import javax.enterprise.context.SessionScoped;
                import javax.inject.Named;
                
                @Named(value="greeter") @SessionScoped
                public class Greeter {
                
                     public String getMessage(){
                          return "Hello";
                     }
                }
                
                



                and this JSP file:


                <?xml version="1.0" encoding="ISO-8859-1" ?>
                <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                    pageEncoding="ISO-8859-1"%>
                <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
                <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
                <!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">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
                <title>Hello</title>
                </head>
                <body>
                
                
                <c:out value="${greeter.message}"/>
                
                </body>
                </html>
                



                that should create an web page with Hello but just silently fails:


                <?xml version="1.0" encoding="ISO-8859-1" ?>
                
                
                    
                <!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">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
                <title>Hello</title>
                </head>
                <body>
                
                
                
                
                </body>
                
                </html>
                



                Any hints?

                • 5. Re: Weld + JSP: Any examples somewhere?

                  Going back to the reasons theme, I believe that one of the reasons Spring is (so far) more popular than Seam is that Spring embraces all technology (like POJOs, JDBC, JSP, JPA, JSF, JEE, etc) while Seam is more only bleeding edge (JPA, JSF, JEE).
                  Why is that relevant? Because we should not not forget that the number of developers playing with the latest and greatest is always smaller than those playing with the good old stuff, being able to integrate with legacy technology can be the difference between wide adoption and failure.


                  Where I work now, introducing Spring was really easy, any JSP project was using Spring for configuration in 1 day, and Spring-MVC for cleaner code in about a week, and Spring-DWR for AJAX in about 2 weeks, introducing Seam was much harder, and 2 of the 4 projects where it was introduced ended up dropping it. I want to be very clear on this:


                  I do NOT think they dropped Seam because it was inferior, they simply dropped it because they could not find and easy way to mix it in, the effort of learning JSF, JPA, and Seam was just too much for them.


                  And of the  2 that are project that is still using Seam, one is a new project (so no need to maintain legacy compatibility at the java language level) that uses Ibatis for persistence (so no need to learn JPA). And the other only uses Seam-Remoting (for AJAX) (persistence is done with an home-made framework, and UI is 99.999999% html/javascript and 0.0000001% JSF.


                  • 6. Re: Weld + JSP: Any examples somewhere?

                    Y tried with #{} instead of ${}:


                    <?xml version="1.0" encoding="ISO-8859-1" ?>
                    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                        pageEncoding="ISO-8859-1"%>
                    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
                    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
                    <!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">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
                    <title>Hello</title>
                    </head>
                    <body>
                    
                    
                    <c:out value="#{greeter.message}"/>
                    
                    </body>
                    </html>
                    



                    but all I got was:


                    <?xml version="1.0" encoding="ISO-8859-1" ?>
                    
                    
                        
                    <!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">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
                    <title>Hello</title>
                    </head>
                    <body>
                    
                    
                    #{greeter.message}
                    
                    </body>
                    </html>
                    



                    as output... :'(.


                    Do I need to do something special to enable Unified EL?

                    • 7. Re: Weld + JSP: Any examples somewhere?

                      According to this article Weld should register an ELResolver to be recognized by JSP...

                      • 8. Re: Weld + JSP: Any examples somewhere?

                        Mmm, according the the docs:




                        JSP


                        If you are integrating Weld into a JSP environment you must register org.jboss.weld.el.WeldELContextListener as an EL Context listener.

                        • 9. Re: Weld + JSP: Any examples somewhere?

                          Shouldn't this lines in web.xml alredy be doing that:


                          <listener>
                              <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
                          </listener>  
                          



                          I mean, inside the org.jboss.weld.environment.servlet.Listener at the method contextInitialized I can see:


                           if (JspFactory.getDefaultFactory() != null)
                                {
                                   JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(sce.getServletContext());
                                   
                                   // Register the ELResolver with JSP
                                   jspApplicationContext.addELResolver(manager.getELResolver());
                          



                          Shouldn't that make it work?

                          • 10. Re: Weld + JSP: Any examples somewhere?

                            Grrrrrrrrrrrrrrrrrrr! I feel so stupid! All my problem was that I did not had the file beans.xml in my WEB-INF folder!!!!!!!


                            It would be nice if Weld printed a warning saying: Couldn't find any beans.xml file, maybe NOTHING is going to work :'(

                            • 11. Re: Weld + JSP: Any examples somewhere?
                              sboscarine

                              Francisco. 


                              You're mixing commentary and questions.  Your comments of frustration could be mistaken for flame bait. 


                              I think you'd get better results if you keep questions and complaints separate.  If you want to have a discussion about why Spring is wonderful and what Seam/Weld can learn from it, I think you'd have a better result if you started a separate thread.  It makes for an interesting debate.  I am a Spring user myself and adore many features of annotated Spring MVC.  In the end, I am personally going to go with JSF2 and weld for new projects for many reasons which are more appropriate to discuss in another forum. 


                              You want to keep the question threads focused on your question because someday someone else will want to know how to use Weld with just JSP and probably doesn't want to listen to complaints about JSF or Seam to find the info. 


                              As far as getting started, Weld will have Maven archetypes soon.  They just got into the JBoss SVN repo this week and would have saved you the step of creating a beans.xml file.  The JBoss team is working hard to improve their documentation and make Weld as easy to learn as possible.  I have a feeling things will get better soon.


                              • 12. Re: Weld + JSP: Any examples somewhere?
                                gavin.king

                                Spring is more popular than Seam or Guice because it was there first. And yes, because it does support a greater range of user interface technologies.


                                One of the reasons for doing CDI - especially the portable extension SPI - was to open up an ecosystem where it is easy for web and UI framework creators to provide integration with CDI. There's nothing in the CDI spec that is really specific to JSF. Now, JSF and CDI work beautifully together. But that doesn't mean that wicket and CDI, or struts2 and CDI or Flex and CDI aren't going to be just as compelling combinations.

                                • 13. Re: Weld + JSP: Any examples somewhere?

                                  Steven Boscarine wrote on Nov 14, 2009 04:02:


                                  Francisco. 

                                  You're mixing commentary and questions.  Your comments of frustration could be mistaken for flame bait. 


                                  I apologize if they are being seen as flame bait, I was only answering to Arbi's question:



                                  Arbi wrote:
                                  why would you need to use JSP with Weld? Legacy app??



                                  I think you'd get better results if you keep questions and complaints separate.  If you want to have a discussion about why Spring is wonderful and what Seam/Weld can learn from it, I think you'd have a better result if you started a separate thread.  It makes for an interesting debate.  I am a Spring user myself and adore many features of annotated Spring MVC.  In the end, I am personally going to go with JSF2 and weld for new projects for many reasons which are more appropriate to discuss in another forum. 


                                  Again, sorry if I offended anyone, of all the... 10 posts in this thread I only wrote 1 comparing Seam with Spring, and only because I was asked why would you need to use JSP with Weld?.



                                  You want to keep the question threads focused on your question because someday someone else will want to know how to use Weld with just JSP and probably doesn't want to listen to complaints about JSF or Seam to find the info. 


                                  I have no interest in discussing that, as I told you, I was answering a question... I guess I just got excited explaining my reasons



                                  As far as getting started, Weld will have Maven archetypes soon.  They just got into the JBoss SVN repo this week and would have saved you the step of creating a beans.xml file.  The JBoss team is working hard to improve their documentation and make Weld as easy to learn as possible.  I have a feeling things will get better soon.



                                  Please do no take it the wrong way, but I really do not like Maven (I prefer Ivy, and I might adopt Gradle in the future, but Maven, no thanks).   I would discuss why with you, but someday someone else will want to know how to use Weld with just JSP and probably doesn't want to listen my complaints about Maven ;-).


                                  • 14. Re: Weld + JSP: Any examples somewhere?

                                    Gavin King wrote on Nov 14, 2009 05:21:


                                    Spring is more popular than Seam or Guice because it was there first. And yes, because it does support a greater range of user interface technologies.


                                    I totally agree with that statement.



                                    One of the reasons for doing CDI - especially the portable extension SPI - was to open up an ecosystem where it is easy for web and UI framework creators to provide integration with CDI. There's nothing in the CDI spec that is really specific to JSF.


                                    Which is why I am happy I was able to use Weld with JSP :-). Congratulations on this great work!



                                    Now, JSF and CDI work beautifully together. But that doesn't mean that wicket and CDI, or struts2 and CDI or Flex and CDI aren't going to be just as compelling combinations.


                                    Which is one of the reasons I am very interested in playing with Weld! I think that with Weld the core and the plugins for other technlogies are more clearly defined, and it is going to be exciting to play in this new ecosystem