部署 JSF 复合组件以供共享使用
我从事一个 Java EE 项目,该项目计划在未来变得相当大,我想分享尽可能多的代码。这不仅包括 Java 代码,还包括 UI 元素的代码。我考虑开发基于明确定义的主题(如用户管理、税收、产品)的企业组件,这些组件在消息、bean 等的基础上进行交互。我还考虑为所有这些组件提供一些托管 bean 和 JSF 组合,以提供一些基本功能供以后在 Web UI 中使用。这就是上下文...
具体来说:假设我有一个用于用户管理的 EAR (um.ear)。在其中,我有一些用于数据库连接的 JPA 实体 (jpa.jar),还有一些用于身份验证等基本功能的企业 Bean (ejb.jar)。此外,我想在 (jsf.jar) 中放入另一个 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 内完成所有这些操作并将托管 bean 和 JSF 内容放入 EAR 内的 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-Final,并且尝试了 JAR 内所有可能的位置,这对我来说是合乎逻辑的。我将类和资源目录放在根“/”、/META-INF 和 WEB-INF 中。所有这一切都不起作用。有人有建议吗,这里出了什么问题?
I work on a Java EE project which is planned to become quite large in future and I want to share as much code as possible. This included not only Java code, but also code for UI elements. I think about developing enterprise components based on clear defined subjects (like user administration, taxes, products) which interact on basis of messages, beans and so on. I also think about giving all these components some managed beans and JSF composites to provide some basic functionality for later use in the web UI. That's the context...
To make it concrete: Let's say I have an EAR (um.ear) for user management. Within it I have some JPA entities for database connectivity (jpa.jar) and I have some enterprise beans for the basic functionality like authentication (ejb.jar). Additionally, I want to put another jar file in (jsf.jar) which contains a managed bean LoginController for use with a composite component (with an input for username and password) login_box.xhtml which I want to place later into my web front end to different locations, depended on the actual page.
The 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>
In my main application I want to work with JSF templates to generate the pages and I want to put the login box with the tag into the pages as needed.
The current setup is:
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
The calling xhtml looks like:
<?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>
When I use it that way, I do not get any errors and in my resulting HTML I see the instead of the wanted login box.
When I all do this within one EAR and I put the managed beans and the JSF stuff into a WAR (the classes are moved into WEB-INF/classes and the resources to WEB-INF/resources) inside the EAR everything works fine. But, how can I deploy the managed beans and the JSF stuff for later use in other EARs or JARs? I read that the /META-INF/faces-config.xml would be enough to force the container to scan for JSF stuff.
My 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>
I work with JBoss 6.1-Final and I tried all possible locations inside the JAR which was logical for me. I put the classes and the resource directory to root '/', to /META-INF and to WEB-INF. All of this did not work. Does anyone has suggestions, what is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 JAR 必须最终位于 WAR 的
/WEB-INF/lib
内部(WAR 又可以是 EAR 的一部分)。您的 JAR 的文件结构很好。
That JAR has to end up inside
/WEB-INF/lib
of WAR (which can in turn be part of EAR).Your JAR's file structure is fine.