2013-08-16 36 views
0

我有幾個項目,在同一程序中,用粗糙的結構:使用豆類

    • EJB
    • 持久
    • web

我使用Maven來解決所有的依賴關係,並將一切都很好地捆綁到一個WAR中,我稍後將它部署在JBoss上。

我的問題是,我的模型大部分是在ejb項目中,我需要在Backing Bean邏輯(在web中)或直接在JSF .xhtmls(也在web中)調用這些bean。

我搜索了互聯網,包括堆棧溢出,並找到了大量關於SPRING的示例以及如何導入/導出模塊。那麼,因爲我沒有使用它,我不知道如何繼續下去。

那麼如何從其他項目導入/使用這些bean?

任何幫助表示讚賞!

回答

1

由於您使用的是您提到的技術堆棧,因此也可以使用CDI。無論如何,如果你將所有東西都打包成WAR,那只是運行時的一個模塊。假設你有以下結構:

--- WAR 
|--- any *.xhtml files can live here (public) 
|--- WEB-INF 
    |--- web.xml (optional) 
    |--- persistence.xml (optional) - declare persistence unit used from WEB-INF/classes 
    |--- beans.xml (optional) enable CDI annotation scanning for WEB-INF/classes 
    |--- faces-config.xml (optional) enable JSF annotation scanning in WEB-INF/classes 
    |--- *.xhtml files can live here (private - usually templates, ui:composition, etc.) 
    |--- classes 
    |--- com.example - put managed beans here, EJBs, JPA etities or just about any other *.class 
    |---lib 
    |--- jpa.jar 
     |--- META-INF 
     |--- persistence.xml - persistence unit used in this jar 
     |--- com.example - JPA entities can live here, gets mapped to WEB-INF/classes at runtime 
    |--- ejb.jar 
     |--- META-INF 
     |--- ejb-jar.xml (optional) but can declare resources here 
     |--- com.example - ejbs can live here, gets mapped to /WEB-INF/classes at runtime 
    |--- faces.jar 
     |--- META-INF 
     |--- faces-config.xml - enables scanning JSF specific annotations in this jar 
     |--- resources - this gets mapped to the WAR root at runtime, .xhtml can live here 
     |--- com.example - put your @ManagedBean s here and @EJB inject anything from ejb.jar, gets mapped to /WEB-INF/classes at runtime 
    |--- cdi.jar 
     |--- META-INF 
     |--- beans.xml - marks this as CDI bean archive 
     |--- com.example - CDI beans can live here, gets mapped to WEB-INF/classes at runtime 

注意,即使我已經明確分開jpa.jar,ejb.jar,faces.jar和CDI.jar在上述結構中,沒有人說你有 - 你可以根據需要混合搭配。該點帶走:

  1. 一戰的WEB-INF/lib類中的任何JAR在運行時獲取映射到/WEB-INF/classes
  2. beans.xml使CDI
  3. faces-config.xml使JSF註釋掃描
  4. ejb-jar.xml不被任何必需的,但你可以利用它
  5. web.xml可以住在WEB-INF/,但不是必需的
  6. web-fragment.xml可以在WEB-INF/lib住在什麼罐子的META-INF文件夾和被合併到web.xml
  7. .xhtml JSF文件可以在WEB-INF/(私人)生活在Web應用程序根目錄(公開),並在裏面WEB-INF/lib任何廣口瓶META-INF/resources - 所有這些文件被映射到Web應用程序根目錄(即。Ë他們可以在邏輯裏面WEB-INF/結束,如果在罐子裏他們是內META-INF/resource/WEB-INF
  8. persistence.xml可以住在WEB-INF或內部的任何廣口瓶的META-INFWEB-INF/lib

這應該只是掩蓋它 - 讓我知道如果什麼都不清楚或者可以加入。

+0

好吧,不知何故,我的Maven包裝奇怪!並非所有的bean都以WEB-INF/classes結尾。它們在我配置它們的單獨快照中。什麼錯誤的是,實現只在WEB-INF/classes中搜索...... – Tibbers

+0

通過bean,你是指JSF管理的bean或CDI bean或EJB? – rdcrng

+0

但它並不重要,它大致相同,只是不是所有的類都映射到WEB-INF/classes。這實際上是我的問題。感謝您的解釋,它有助於找到我的包裝問題。我已經用Maven修復了它們!無論如何,我會認爲這是正確的答案,因爲您提供有關如何打包bean以使其可訪問的見解! – Tibbers