注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

How-to: Modular Java EE Applications with CDI and PrettyFaces(收藏)  

2012-05-29 22:24:10|  分类: javaEE |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

原文地址:http://ocpsoft.org/java/jsf-java/how-to-modular-jsf-applications-with-cdi-and-prettyfaces/

I was recently asked the question, “Is it possible to create a modular JSF application, where JAR-files act as plug-ins and allow xhtml views, images, css, navigation rules, and managed beans to be added as modules to the application?”

The answer to this question is, “of course,” but there is no pre-set way of accomplishing such a task, so you’ll have to be a little creative. I’ve come up with a conceptual architecture that “would work,” if properly implemented, but keep in mind that this is just something I threw together in a few minutes of thinking about the problem.

What we need to accomplish our goal

0. Generate a barebones Java EE web-application.

Each module must have the ability to:

  1. Package .xhtml views, CSS, JavaScript, and images
  2. Bundle and register Managed Beans
  3. Add navigation rules
  4. (Bonus) Add bookmark-able URLs for each module
  5. Include Unit & Integration tests

Firstly, most new technologies in the Java EE ecosystem support configuration of resources in JAR-files simply by adding a descriptor in “jar:/META-INF/[web.xml, faces-config.xml, beans.xml]” and so on. This means that all you need to do to bundle artifacts from the various specifications (JPA, JSF, EJB, CDI, and so on) is place a descriptor in the right spot; your artifacts will be discovered and included in the larger application. (For more details on these descriptors, see this post.)

To run your application with the least amount of effort, you should use a full Java EE Application Server such as JBoss Application Server 6.0 (download,) since it comes pre-configured with JSF and all necessary Java EE modules for this architecture.

Getting started quickly with a Java EE 6 / JSF web-application

First, we’ll need a project with which to experiment, so to create a simple web-application that we can use as the basis for our concept, we can use Seam Forge: a next generation project generation and enhancement tool from JBoss. With Forge you can have a web-application built in, literally, two command-line commands.

  1. Install and run Forge
  2. $ new-project –named {projectname} –topLevelPackage {com.yourpackage}
  3. $ scaffold setup
  4. $ build

1. Package .xhtml views, CSS, JavaScript, and images

Now that we have a simple web-application running with JSF, let’s look at the first problem. How do we get JSF .xhtml files bundled in a JAR file? If we are using JSF 1.2, this article about packaging Facelets in a JAR file will come in handy, but if using JSF 2, then the hard part is already done for us – it’s already supported!

All files in jar:/META-INF/resources/* will automatically be made available to JSF as if they were in the /webapp/resources folder.

We can even put Facelets (.xhtml files) themselves in the jar:/META-INF/ folder, and they will be accessible as if they were in the main web-application itself.

2. Detect and register Managed Beans

This part is simple. As mentioned in the same article, simply create an empty jar:/META-INF/beans.xml. This will denote your archive as a “beans archive”, causing all beans in your JAR to be accessible to the CDI BeanManager / your application.

3. Add navigation rules

This part is also simple. JSF allows for thefaces-config.xml file to be split into parts and automatically integrated/merged from multiple JAR files. This means all you need to do is create a jar:/META-INF/faces-config.xml file, which contains your navigation (and other) rules, and you’ll see those configurations picked up by the application.

4. (Bonus) Add bookmark-able URLs for each module

This is the coolest part (in my opinion.) Not only can you add new JSF pages to your application, you can also ensure that those pages are accessible via bookmark-able URLs. This is done using PrettyFaces, an open-source URL-rewrite filter tool created by OCPSoft and community.

First, you should know that PrettyFaces was initially designed for JSF, but now functions within a pure servlet or Java EE environmenet – you no longer need JSF JAR files on your classpath.

Second, you should know that like JSF, PrettyFaces allows for modular configuration, so all you need to do to add new URL-mappings to your application for each JAR is to create a jar:/META-INF/pretty-config.xml file which contains your new URL-mappings:

<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.2.0"                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.2.0                                       http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.2.0.xsd">        <!-- Begin UrlMappings         // These are examples of URL mappings, and should be customized for your application.           <url-mapping id="home">             <pattern value="/summary/" />             <view-id>/faces/module/summary.jsf</view-id>        </url-mapping>            <url-mapping id="store">             <pattern value="/detail/" />             <view-id>/faces/module/detail.jsf</view-id>        </url-mapping>         -->     </pretty-config>

Notice that the view-id defined in each URL-mapping is the location of your Facelet file in theMETA-INF folder contained within your module’s JAR file. This is about as simple as it gets.

For an extra kick, if you wanted to create a self-updating menu with links to your new modules (a navigation area,) all you would need to do in your application is call:

PrettyContext.getCurrentInstance(request).getConfig().getMappings();

This will provide you a list of all URL-mappings in the application, at which point you can decide on a naming-standard which would enable you to identify individual modules that should be included in the navigation area, and create links accordingly.

5. Include Unit & Integration tests

For this topic, all I can really say is that Arquillian, by JBoss is your new best friend – this integration-testing framework brings full-scale web and Java EE testing to your fingertips, and it’s almost as simple as writing a JUnit test; in fact, your tests will be written in JUnit.

For an example of a working Arquillian Web-application test, you can inspect the PrettyFaces JSF2-testing project, which uses all of the concepts you would need to apply (Take a look at the PrettyContextTest, in which case you would do something similar, but replace PrettyFaces with your JAR module in theMavenArtifactResolver.)

Conclusion

I hope this is enough to stimulate some thought. I have not been able to actually put this together myself, but in concept at least, there is nothing to stop one from creating a system like the one I have described. I’m sure there would be a few kinks, but this is what the power of Java EE has enabled us to do. A fully modular web-application with very little actual configuration. Just put files in the right place, and you’re good to go. I’d be very interested to hear how people do, if anyone decides to give this a try. Please post your experiences below!

  评论这张
 
阅读(758)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017