web.xml setup for facelets template and client

假如想象 提交于 2019-11-28 12:50:01

问题


I've looked at a few resources for JSF and facelets, but don't understand a few configuration points. What's the distinction between:

<url-pattern>/faces/*</url-pattern>

and:

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

While I understand it's possible to have several url-pattern elements, unless .jsf pages are explicitly being used, there's no actual need for this mapping, correct? If only faces templates and clients are being used, then it's extraneous?

Furthermore, if the facelet template and client are inside WEB-INF, how are they accessed?

With the latest releases for JSF and Facelets, there seems no hard requirement for faces-config.xml; correct?

Finally, if Glassfish is being used with facelets clients/templates, then the EL is through CDI?

Overall, why isn't this client:

<?xml version='1.0' encoding='UTF-8' ?>
<!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"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <body>
        <ui:composition template="./template.xhtml">
            <ui:define name="top">
                top
            </ui:define>
            <ui:define name="content">
                expression language not evaluating?
                <h:outputLabel value="#{hello.hi(fred)}" />
            </ui:define>
        </ui:composition>
    </body>
</html>

using this template:

<?xml version='1.0' encoding='UTF-8' ?> 
<!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"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="./css/default.css"/>
        <h:outputStylesheet name="./css/cssLayout.css"/>
        <title>Facelets Template</title>
    </h:head>
    <h:body>
        <div id="top" class="top">
            <ui:insert name="top">Top</ui:insert>
        </div>
        <div id="content" class="center_content">
            <ui:insert name="content">Content</ui:insert>
        </div>
    </h:body>
</html>

with this web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/client.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

using this bean:

package pkg;

import javax.faces.bean.SessionScoped;
import javax.inject.Named;


@Named
@SessionScoped
public class Hello {

    public Hello() {
    }

    public String hi(String name) {
        return "hi " + name;
    }

}

Instead, the EL simply shows in the page as so:

thufir@dur:~$ 
thufir@dur:~$ lynx http://localhost:8080/HelloExpressionLanguage/client.xhtml -dump
<?xml version='1.0' encoding='UTF-8' ?>
<!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"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <body>
        <ui:composition template="./template.xhtml">
            <ui:define name="top">
                top
            </ui:define>
            <ui:define name="content">
                expression language not evaluating?
                <h:outputLabel value="#{hello.hi(fred)}" />
            </ui:define>
        </ui:composition>
    </body>
</html>thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ lynx http://localhost:8080/HelloExpressionLanguage/ -dump
   top

   expression language not evaluating?
thufir@dur:~$ 
thufir@dur:~$ 

回答1:


It is not clear that where do you want to see the expected "hello fred" output. In the example you use ui:insert, but it for templates.

if you want to use the hello method in the bean you will need to use an EL expression and for the output a h:outputLabel tag:

<h:outputLabel value="#{helloWorld.hello('fred')}" />

Update1 (if JSF code not processed):

You should check your web.xml. It have to contains a servlet and a listener like this:

<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>

Note: of you use .xhtml or .hello or something else, you have to match the url-pattern of the server-mapping.




回答2:


The desired output (to an extent), in that "bar" is output, from the Hello bean, below:

thufir@dur:~$ 
thufir@dur:~$ lynx http://localhost:8080/Birds/falcon.xhtml -dump
                           The Happy Birds Directory

Contents table
     __________________________________________________________________

   [1]Home
   [2]Parrot
   [3]Eagle
   [4]Falcon

                                     Falcon

   The Happy Birds Directory contains birds. bean says bar

References

   1. http://localhost:8080/Birds/falcon.xhtml
   2. http://localhost:8080/Birds/falcon.xhtml
   3. http://localhost:8080/Birds/falcon.xhtml
   4. http://localhost:8080/Birds/falcon.xhtml
thufir@dur:~$ 

index.xhtml:

<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <body>
        This and everything before will be ignored
        <ui:composition template="template.xhtml">
            <ui:define name="navigation">
                <ui:include src="menu.xhtml"/>
            </ui:define>
        </ui:composition>
        This and everything after will be ignored
    </body>
</html>

menu:

<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <body>
        This and everything before will be ignored
        <ui:composition>
            <h3>Contents table</h3>
            <hr/>
            <h:panelGrid columns="1">
                <h:commandLink value="Home" action="home" />
                <h:commandLink value="Parrot"
                               action="parrot" />
                <h:commandLink value="Eagle"
                               action="eagle" />
                <h:commandLink value="Falcon"
                               action="falcon" />
            </h:panelGrid>
        </ui:composition>
        This and everything after will be ignored
    </body>
</html>

parrot:

<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <body>
        This and everything before will be ignored
        <ui:composition template="template.xhtml">
            <ui:define name="navigation">
                <ui:include src="menu.xhtml"/>
            </ui:define>
            <ui:define name="main">
                <h1>Parrot</h1>
                <p>
                    Parrots are interesting birds...
                </p>
            </ui:define>
        </ui:composition>
        This and everything after will be ignored
    </body>
</html>

the falcon, speaks, to an extent:

<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <body>
        This and everything before will be ignored
        <ui:composition template="template.xhtml">
            <ui:define name="navigation">
                <ui:include src="menu.xhtml"/>
            </ui:define>
            <ui:define name="main">
                <h1>Falcon</h1>
                <p>
                    <p>The Happy Birds Directory
                        contains #{directoryBean.totalCount} birds.
                        bean says #{hello.foo()}
                    </p>
                </p>
            </ui:define>
        </ui:composition>
        This and everything after will be ignored
    </body>
</html>

however, output from the directoryBean doesn't get put into the web page.

the template:

<?xml version='1.0' encoding='UTF-8' ?> 
<!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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <head>
        <title>The Happy Birds Directory</title>
        <style type="text/css">
            <!--
            .box {
                float: right;
                width: 50%;
                border: black dotted 1px;
                padding: 5px
            }
            -->
        </style>
    </head>
    <body>
        <h:form>
            <h1>The Happy Birds Directory</h1>
            <div class="box">
                <ui:insert name="navigation"/>
            </div>
            <ui:insert name="main">
                Welcome to the nest!
            </ui:insert>
        </h:form>
    </body>
</html>

directory bean:

package dur;

import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class DirectoryBean {

    private int totalCount = 99;

    public DirectoryBean() {
    }

    public int getTotalCount() {
        System.out.println(totalCount);
        return totalCount;
    }

}

hello bean (note that it's @ManagedBean):

package dur;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
@ManagedBean(name = "hello")
public class Hello {

    public Hello() {
    }

    public String foo() {
        return "bar";
    }

    public String hi(String name) {
        return "hi " + name;
    }

}

the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>


来源:https://stackoverflow.com/questions/26083887/web-xml-setup-for-facelets-template-and-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!