Search code examples
jsfjsf-2jboss7.x

JSF facelet app not working in jboss AS 7


I am trying to set up environment for Java/JSF app in my windows 7 machine. I am using eclipse juno with Jboss AS 7 downloaded from the eclipse market. Also I am using JDK 1.7. I created a very simple app containing just a h:outputLabel tag. Everything looks fine until deployed and run, but the tag doesn't render. What I mean to say is that I am getting a blank page. The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar. And the usual commons and jstl jars.

There are no errors or any exceptions. Am I missing anything here? Please pardon me if its too simple but this is just taking too much time.

EDIT: My auto-created faces-config xml:

<?xml version="1.0" encoding="UTF-8"?>

   <faces-config
    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-facesconfig_2_0.xsd"
version="2.0">

</faces-config>   

My web xml:

 <?xml version="1.0"?>
<web-app version="3.0" 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_3_0.xsd">
    <display-name>test</display-name>
   <welcome-file-list>
   <welcome-file>NewFile.xhtml</welcome-file>
   </welcome-file-list>
 <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>
<servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
 <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
 </web-app>

Thanks in advance.


Solution

  • The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar. And the usual commons and jstl jars.

    There are quite a lot of mistakes right here. First of all, you don't need and even should not include the JSF jars. Those are already part of Java EE, which is implemented by JBoss AS 7.

    Secondly, you definitely don't need and absolutely should not use the separate Facelets jar in combination with JSF 2.x. Facelets is already part of JSF 2.x.

    You also should not include the JSTL jar. That one too is provided by Java EE/Jboss AS. If with commons jars you mean Apache commons, then those are fine but they are NOT needed for JSF. Include them only if you want to use them directly in your application code.

    If you would need any of those jars (you don't, but suppose), it's also best practice to take the latest ones if you're just starting. From the version numbers it kinda looks like you just took a random old version. But again, you don't need any of those jars. They are provided by Java EE/JBoss AS 7.

    My auto-created faces-config xml:

    You don't need an empty faces-config.xml. If you're just starting, it might be better to remove everything you don't need. If there's later something you need to configure, you can always add it.

    My web xml:

    For JSF you don't really need to map the FacesServlet to the extensions you used. Those are already the default. If you leave out the entire web.xml, your Facelets (.xhtml) page can be requested by changing the .xhtml extension to .jsf or .faces. E.g. if your page is 'NewFile.xhtml' you can request it using localhost:8080/NewFile.jsf or 'localhost:8080/NewFile.faces` or 'localhost:8080/faces/NewFile.xhtml'.

    Unfortunately, the only (IMHO) useful mapping is NOT provided as a default by JSF 2.1 and for that one you do need to add a mapping entry in web.xml:

    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>      
    </servlet-mapping>
    

    Because your welcome page is NewFile.xhtml, you need either this *.xhtml mapping, OR you can remove the mapping entirely and change the welcome-file content to e.g. NewFile.jsf.

    Update:

    If the welcome page still doesn't show, there must be something else in your project that you either don't know about yourself or aren't showing us.

    Try to start over with a very simple project and see if it works there:

    1. In Eclipse, create a new Dynamic Web Project
    2. Use project name: welcome and Target runtime: JBoss 7.1 Runtime
    3. Delete WebContent/META-INF and WebContent/WEB-INF/lib
    4. Copy the 3 files from http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java-ee-app-without-any.html to your project. Afterwards your workspace should look exactly like the one in the picture.
    5. Add a WebContent/WEB-INF/web.xml with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
    
        <servlet>
            <servlet-name>facesServlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>facesServlet</servlet-name>
            <url-pattern>*.xhtml</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>page.xhtml</welcome-file>
        </welcome-file-list>
    
    </web-app>
    

    Double check you have exactly 4 files in your entire 'welcome' project, not more and not less.

    Deploy your project to JBoss AS 7.1. To be sure, right click on the runtime server in the Servers view and click on "Clean..." Start your server and request localhost:8080/welcome or localhost:8080/welcome/.

    I just tested this locally using the exact same steps I outlined, and it worked.