Jeremy Stein - Brain

« »

Simple JSF-Spring Example

If you want to see JSF and Spring working together, check out AppFuse. But, AppFuse also uses acegi, hibernate, sitemesh, and a bunch of other little packages. You probably should use most of them too, but you might not have realized it yet. So, until you do, you could start with an absolutely bare bones Hello World project to help you jumpstart your configuration. Here is my JSF-Spring Hello World example: [download].

You’ll need the latest versions of Maven and Tomcat. Unzip. Run “mvn”. Drop the war (or exploded war) in Tomcat. Start her up and hit http://localhost:8080/example/test.faces. If you don’t understand those instructions, you came to the wrong place. Try a JSP Book or two.

If you want to load it into MyEclipse 5, follow these instructions:

  1. mvn compile (if you haven’t already)
  2. mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo (see Maven Eclipse guide).
  3. mvn eclipse:eclipse -DdownloadSources=true
  4. Open MyEclipse
  5. File > Import…, “Existing Projects into Workspace”, Pick the “example” directory you created, Finish
  6. Select the project and use the menu bar (or right click it and use the context menu), MyEclipse > Add WebProject Capabilities…, set the Web Root Directory to /src/main/webapp, set the Web Context Root to whatever you want, do not create web.xml, do not add J2EE Libs to Buildpath.
    Add WebProject Capabilities screenshot
  7. MyEclipse > Add Spring Capabilities…, select no libraries, Next, Specify the existing configuration file: src/main/webapp/WEB-INF/applicationContext.xml, Finish.
  8. Backup web.xml
  9. MyEclipse > Add JSF Capabilities…, JSF Implementation: MyFaces 1.1.1, uncheck the Install checkboxes.
  10. Undo the change made by MyEclipse to web.xml (using the backup made earlier

Note that if you change the dependencies, you’ll want to re-run mvn eclipse:eclipse. This will mess up the build path configuration for MyEclipse. MyEclipse likes to compile directly to the WEB-INF/classes folder. Maven doesn’t. So, you’ll have to follow these steps:

  1. mvn eclipse:eclipse -DdownloadSources=true
  2. Open MyEclipse
  3. Right click the project > Build Path > Configure Build Path
  4. Uncheck “Allow output folders for source folders”
  5. Set the Default output folder to YourProjectName/src/main/webapp/WEB-INF/classes

September 13, 2006 12 Comments.

12 Comments

  1. Bansi replied:

    Jeremy
    Thank you so much for posting your Hello World application. I am sure it will be helpful to people who have gone thru similar experiences of you & mine. Few Clarifications

    -> Looks Like you are using MyFaces 1.1.1
    -> I downloaded you application but couldn’t find JAR files in WEB_INF/lib directory. Could you please let me know what JAR files i need to use as you suggest not to use the JAR files from MyEclipse in the above posting
    ->I am not familar with Maven. It would be great if you explain how to use with ANt
    -> I suspect the root cause for my el.EvaluationException error is the JAR files imported by MyEclipse OR it maybe also that i am not using MyFaces. Do i need to use MyFaces . I know its implementation of JSF. But is it reqd

    Pl mail me at mail2bansi@yahoo.com

    Regards
    Bansi

    September 13th, 2006 at 3:58 pm. Permalink.

  2. Jeremy replied:

    Maven is simple to use. It will download the required jars for you.

    September 13th, 2006 at 4:04 pm. Permalink.

  3. Bansi replied:

    Also i am getting following errors on using Maven 2.0.4
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\sn934c>mvn -version
    Maven version: 2.0.4

    C:\Documents and Settings\sn934c>cd C:\J2EE\JSF\JSF-Spring-Example\example

    C:\J2EE\JSF\JSF-Spring-Example\example>mvn
    [INFO] Scanning for projects…
    [INFO] ————————————————————————-

    [INFO] Building Example Project
    [INFO] task-segment: [package]
    [INFO] ————————————————————————-

    [INFO] artifact org.apache.maven.plugins:maven-resources-plugin: checking for up
    dates from central
    [WARNING] repository metadata for: ‘artifact org.apache.maven.plugins:maven-reso
    urces-plugin’ could not be retrieved from repository: central due to an error: E
    rror transferring file
    [INFO] Repository ‘central’ will be blacklisted
    [INFO] ————————————————————————
    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] The plugin ‘org.apache.maven.plugins:maven-resources-plugin’ does not exi
    st or no valid version could be found
    [INFO] ————————————————————————
    [INFO] For more information, run Maven with the -e switch
    [INFO] ————————————————————————
    [INFO] Total time: 3 seconds
    [INFO] Finished at: Wed Sep 13 12:44:58 PDT 2006
    [INFO] Final Memory: 1M/2M
    [INFO] ————————————————————————

    C:\J2EE\JSF\JSF-Spring-Example\example>mvn compile
    [INFO] Scanning for projects…
    [INFO] ————————————————————————-

    [INFO] Building Example Project
    [INFO] task-segment: [compile]
    [INFO] ————————————————————————-

    [INFO] artifact org.apache.maven.plugins:maven-resources-plugin: checking for up
    dates from central
    [WARNING] repository metadata for: ‘artifact org.apache.maven.plugins:maven-reso
    urces-plugin’ could not be retrieved from repository: central due to an error: E
    rror transferring file
    [INFO] Repository ‘central’ will be blacklisted
    [INFO] ————————————————————————
    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] The plugin ‘org.apache.maven.plugins:maven-resources-plugin’ does not exi
    st or no valid version could be found
    [INFO] ————————————————————————
    [INFO] For more information, run Maven with the -e switch
    [INFO] ————————————————————————
    [INFO] Total time: 2 seconds
    [INFO] Finished at: Wed Sep 13 12:47:59 PDT 2006
    [INFO] Final Memory: 1M/2M
    [INFO] ————————————————————————

    September 13th, 2006 at 4:05 pm. Permalink.

  4. Bansi replied:

    Jeremy
    Is it mandatory to use MyFaces. My plan was to implement Hello World application with JSF 1.1_01, Spring 1.2, Tomcat 5.0.28.

    As this is not working for me , wondering whether it is manadatory to use MyFaces

    Also maven 2.0.4 doesnt work for me . So could you please list the jar files which i need to use. In the meantime looking at pom.xml i manually copied the JAR files o WEB-INF/lib directory & it gives the following error. Looks like i am missing key JAR files

    Whats the problem in using JAR files shipped out-of-the-box from MyEclipe i.e. Add JSF Capabilities

    Regards
    Bansi

    September 13th, 2006 at 5:42 pm. Permalink.

  5. Bansi replied:

    Here is the error
    org.apache.jasper.JasperException
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.lang.NullPointerException
    javax.faces.webapp.UIComponentTag.setupResponseWriter(UIComponentTag.java:615)
    javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:217)
    org.apache.myfaces.taglib.core.ViewTag.doStartTag(ViewTag.java:71)
    org.apache.jsp.test_jsp._jspx_meth_f_view_0(test_jsp.java:95)
    org.apache.jsp.test_jsp._jspService(test_jsp.java:70)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    September 13th, 2006 at 6:10 pm. Permalink.

  6. Bansi replied:

    Pl ignore the error i figure out it is test.faces not test.jsp

    September 13th, 2006 at 6:14 pm. Permalink.

  7. Bansi replied:

    Jeremy
    Thanks for sharing your sample application it works for me. Still wondering do i really need MyFaces to make it work. Cant it work with just Spring and JSF i mean without MyFaces

    I once again thank you for your help and would like to know if you have any similar sample barbones application extending the Integration to Hibernate. When i tried extending the integration to Hibernate it results in following errors on Tomcat 5.0.28

    Sep 13, 2006 5:34:57 PM org.apache.catalina.core.StandardContext start
    SEVERE: Error listenerStart
    Sep 13, 2006 5:34:57 PM org.apache.catalina.core.StandardContext start
    SEVERE: Context startup failed due to previous errors

    Could you please share your sample application JSF+Spring+Hibernate

    Thanks for your time in advance

    Regards
    Bansi

    September 13th, 2006 at 7:52 pm. Permalink.

  8. Jeremy replied:

    (I replied on the original forum.)

    September 13th, 2006 at 10:21 pm. Permalink.

  9. Theone replied:

    Jeremy,

    Just downloaded the example. I’m getting these errors when run
    mvn eclipse:eclipse -DdownloadSources=true

    [WARNING] Unable to get resource from repository central (http://repo1.maven.org/maven2)

    for all dependencies

    Thank you

    September 23rd, 2006 at 5:46 pm. Permalink.

  10. Jeremy replied:

    Some dependencies may not have source available. No big deal. If you want it in Eclipse, you’ll have to download it from somewhere else.

    Also, in my experience, Maven tends to fail in a lot of the downloads. Try again and see if it doesn’t get further the next time.

    September 23rd, 2006 at 8:07 pm. Permalink.

  11. Theone replied:

    I’ve tried several times but no luck. I’ve downloaded the dependencies from the web to my local such as C:\Documents and Settings\Administrator\.maven\repository\log4j\jars\log4j-1.2.8.jar. But Maven’s created .classpath insisted on this path

    what is governing the path when maven runs?

    September 23rd, 2006 at 8:20 pm. Permalink.

  12. Jeremy replied:

    Try without -DdownloadSources=true

    Also, you don’t have to use the .classpath the Maven creates. If there are some dependencies that Maven can’t give you (which shouldn’t happen), then you can download those yourself from elsewhere and set up Eclipse’s classpath.

    September 23rd, 2006 at 8:54 pm. Permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »