2011-11-05 38 views
3

我正在開發一個計劃將來變得相當龐大的Java EE項目,並且我想盡可能多地分享代碼。這不僅包括Java代碼,還包括用於UI元素的代碼。我考慮基於消息,bean等基於明確定義的主題(如用戶管理,稅收,產品)開發企業組件。我還考慮給所有這些組件提供一些託管的bean和JSF組合以提供一些基本功能,以便稍後在Web UI中使用。這是上下文...部署用於共享的JSF複合組件

爲了使其具體:讓我們說我有一個EAR(um.ear)用戶管理。在它內部,我有一些用於數據庫連接的JPA實體(jpa.jar),並且我有一些企業bean用於基本功能,如身份驗證(ejb.jar)。另外,我想將另一個jar文件放入(jsf.jar)中,該文件包含一個託管bean LoginController,以便與複合組件(帶有用戶名和密碼輸入)login_box.xhtml一起使用,我想稍後將其放到我的Web前端到不同的位置,取決於實際的頁面。

的login_box.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:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:composite="http://java.sun.com/jsf/composite" 
    xmlns:e="http://java.sun.com/jsf/composite/login"> 
<h:head> 
    <title>Login Box</title> 
</h:head> 
<h:body> 
    <composite:interface> 
    </composite:interface> 
    <composite:implementation> 
     <h:outputStylesheet library="css" name="login.css" target="head" /> 
     <div class="login_box"> 
      <h:form> 
       <h:messages /> 
       <h:panelGrid columns="2" columnClasses="rightAlign,leftAlign"> 
        <h:outputText value="Email address:" /> 
        <h:inputText label="Email address" value="#{LoginController.email}" 
         required="true" size="8"> 
         <f:validator validatorId="emailAddressValidator" /> 
         <f:validateLength minimum="5" maximum="128" /> 
        </h:inputText> 
        <h:outputText value="Password:" /> 
        <h:inputText label="Password" value="#{LoginController.password}" 
         required="true" size="8" /> 
       </h:panelGrid> 
       <h:commandButton action="${LoginController.login()}" 
        value="Login..." /> 
      </h:form> 
     </div> 
    </composite:implementation> 
</h:body> 
</html> 

在我的主要應用程序,我想用JSF模板合作,產生的頁面,我希望把登錄框與標籤進入頁面需要。

目前的設置是:

jsf.jar inside um.ear: 
|- META-INF/ 
| |- faces-config.xml 
| |- web.xml 
| |- sub-web.xml 
| \- resources 
|  \- login 
|   \- login_box.xhtml 
\- com 
    |- inside it the managed bean classes 

主叫XHTML的樣子:

<?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:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:e="http://java.sun.com/jsf/composite/login"> 
<h:head> 
    <title>Title</title> 
</h:head> 
<h:body> 
    <e:login_box /> 
</h:body> 
</html> 

當我使用這種方式,我沒有得到任何錯誤,在我生成的HTML我看到了而不是想要的登錄框。

當我在一個EAR內完成這項工作時,我將EAR中的託管bean和JSF東西放入WAR(將類移入WEB-INF/classes和資源到WEB-INF/resources)工作正常。但是,如何部署託管的bean和JSF內容以供以後在其他EAR或JAR中使用?我讀過/META-INF/faces-config.xml足以強制容器掃描JSF的東西。

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

我與JBoss工作6.1決賽,我嘗試這是合乎邏輯的,我的JAR內的所有可能的位置。我把類和資源目錄放在根目錄'/',/ META-INF和WEB-INF中。所有這些都不起作用。有沒有人有建議,這裏有什麼不對?

回答

3

該JAR必須在WAR的內部/WEB-INF/lib內(它可以是EAR的一部分)結束。

你的JAR的文件結構很好。

+0

謝謝,快速回答。我會試試看。但是,有沒有辦法將um.ear放到JBoss中並從那裏使用組合? EJB僅部署一次... –

+0

不可能。在JBoss中,WAR有自己的類加載器,它不能訪問EAR中的庫。 – BalusC

+0

謝謝! JSF的東西現在可以工作。我現在明白了,JSF的東西需要在一個地方進行渲染。現在我遇到了跨EAR的遠程EJB問題。這可能會成爲另一個問題,直到明天我才明白...... ;-) –