2014-09-28 24 views
0

我已經看過JSF和facelets的一些資源,但不明白一些配置點。什麼是之間的區別:facelets模板和客戶端的web.xml設置

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

和:

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

雖然我知道這是可能有若干個url-pattern元素,除非明確地使用.jsf頁,有這種映射沒有實際需要,正確?如果只有模板和客戶端正在使用,那麼它是無關的?

此外,如果facelet模板和客戶端在WEB-INF之內,它們是如何訪問的?

對於JSF和Facelets的最新版本,似乎沒有硬性要求faces-config.xml;正確?

最後,如果Glassfish與facelets客戶端/模板一起使用,那麼EL是通過CDI?

總體而言,爲什麼不是這樣的客戶:

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

使用該模板:

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

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

使用這個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; 
    } 

} 

相反,EL只顯示在頁面像這樣:

[email protected]:~$ 
[email protected]:~$ 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>[email protected]:~$ 
[email protected]:~$ 
[email protected]:~$ lynx http://localhost:8080/HelloExpressionLanguage/ -dump 
    top 

    expression language not evaluating? 
[email protected]:~$ 
[email protected]:~$ 

回答

1

目前尚不清楚的是在那裏你想看到預期的「hello弗雷德」輸出。 在這個例子中,你使用ui:insert,但是它用於模板。

,如果你想使用hello方法在豆你將需要使用一個EL表達式和輸出啊:outputLabel標籤:

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

UPDATE1(如果不處理JSF代碼):

你應該檢查你的web.xml。它必須包含一個servlet和這樣的監聽器:

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

注意:您使用.xhtml或.hello或別的東西,你必須匹配服務器映射的URL模式。

+0

爲什麼'ui:insert'用於模板?這個例子當然使用了模板,所以我不明白你在做什麼。使用'ui:insert'是不正確的? – Thufir 2014-10-29 13:15:19

0

所需的輸出(在一定程度上),在該 「杆」 是輸出,從喂豆,下面:

[email protected]:~$ 
[email protected]:~$ 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 
[email protected]:~$ 

索引。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> 

菜單:

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

鸚鵡:

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

鶻,講,到這樣的程度:

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

然而,輸出從directoryBean沒有按放不下去 網頁。

模板:

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

目錄豆:

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

} 

你好豆(注意,這是@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; 
    } 

} 

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>