为什么不JSF 2.0 RI(Mojarra)扫描我的班“的注解? [英] Why doesn't JSF 2.0 RI (Mojarra) scan my class' annotations?

查看:126
本文介绍了为什么不JSF 2.0 RI(Mojarra)扫描我的班“的注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的基于Eclipse的JSF项目中的战争与JAR项目。我决定使用注解来声明我FacesConverter,(其中无数的其他事情),而不是用我的faces-config.xml中声明。

I have a War and Jar project in my Eclipse-based JSF project. I have decided to use annotations to declare my FacesConverter, (among a myriad other things), rather than declare it using my faces-config.xml.

@FacesConverter(value="passwordFieldStringConverter")
public class PasswordFieldStringConverter implements Converter {

 public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
  try {
   return arg2.getBytes("UTF-16BE");
  }
  catch(UnsupportedEncodingException uee) {
   Assert.impossibleException(uee);
  }

  return(null);
 }

 public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
  try {
   return new String((byte[]) arg2, "UTF-16BE");
  }
  catch(UnsupportedEncodingException uee) {
   Assert.impossibleException(uee);
  }

  return(null);  
 }

}

然后我使用 passwordFieldStringConverter 直接在我的.xhtml:

And then I use passwordFieldStringConverter directly in my .xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<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:sec="http://www.springframework.org/security/facelets/tags">
 <ui:composition>
  <f:view>
      <f:loadBundle basename="landingPage.bundle" var="bundle" /> 

   <ui:decorate template="/WEB-INF/jsf_helpers/htmlShell.xhtml">
    <ui:param name="PageTitleParam" value="#{bundle.pageTitle}" />

    <h:form>
      <h:dataTable var="rowVar" value="#{userListContainer.users}">
       <f:facet name="header"><h:outputText value="Users you are currently managing:" /></f:facet>
       <h:column>
        <f:facet name="header">
         <h:outputText value="Screen Name" />
        </f:facet>
        <h:outputText value="#{rowVar.screenName}" />
    </h:column>
       <h:column>
        <f:facet name="header">
         <h:outputText value="Password" />
        </f:facet>
        <h:outputText value="#{rowVar.password}">
         <f:converter converterId="passwordFieldStringConverter" />
        </h:outputText>
       </h:column>
      </h:dataTable>
    </h:form>
   </ui:decorate>
  </f:view>
 </ui:composition>
</html>

JSF应该扫描我在部署时战争罐子和检测哪些课程对他们的注释(并相应地自动配置应用程序)。我的问题是,JSF显然没有检测到我有运动注解类。

JSF is supposed to scan the jars in my War at deployment-time and detect which classes have annotations on them (and auto-configure the application accordingly). My problem is that JSF is apparently not detecting the classes I have which sport annotations.

战争项目有我所有的.xhtml文件,以及该项目的faces-config.xml中,我的JAR项目有我所有的面孔相关的Java code(动作豆,管理的bean,定制的转换器等。 )

The War project has all of my .xhtml files as well as the project's faces-config.xml, my Jar project has all of my faces related Java code (action beans, managed beans, custom converters, etc.)

推荐答案

是的,我马上回答我的问题,因为我已经花了一个星期撞我的头在桌子上,我只有通过调试后想通了,我的问题JSF 2.0 RI(Mojarra),看看它在做什么。

Yes, I'm immediately answering my own question because I already spent a week banging my head on the table and I only figured out my problem after debugging through the JSF 2.0 RI (Mojarra) to see what it was doing.

基本上,注释扫描仪只能咨询WAR的/ WEB-INF / classes中的.class文件注释(假设你在/ WEB-INF一个faces-config.xml中)。如果你把你的code在单独的JAR文件和想你的.class文件你在/ WEB-INF / lib中的.jar文件进行扫描了注解,您必须在一个faces-config.xml中JAR的META-INF文件夹即可。你扑通到您的罐子是空的faces-config.xml中,只需要在那里,否则注释扫描仪将逾越节的罐子,就像是吃剩的肉饼。

Basically, the annotation scanner only consults the WAR's /WEB-INF/classes .class files for annotations (assuming you have a faces-config.xml in /WEB-INF). If you keep your code in separate Jar files and want your .class files you have in /WEB-INF/lib .jar files to be scanned for annotations, you must have a faces-config.xml in that Jar's META-INF folder. The faces-config.xml you plop into your jar can be empty, it just needs to be there or else the annotation scanner will passover your jar like it was leftover meatloaf.

空faces-config.xml中:

Empty 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 /WEB-INF/schema/web-facesconfig_2_0.xsd" 
              version="2.0" metadata-complete="false">
    <!-- This file must be present with our jar, even if it is empty.  Otherwise, our Java annotations won't get scanned! -->
</faces-config>

我知道扔了很多-INF的那里。所以,只是为了回顾一下。您faces-config.xml中在战争中去WEB-INF。你可能为空,注释扫描仪启用,faces-config.xml中的每个jar文件去在罐的 META -INF。

如果你想知道为什么的行为有如此疯狂的,那是因为JSF CONFIGS可以分散,分散在第1和第三方库这种方式。如果你想知道关于faces-config.xml中的一罐整个presence是一个因素,那么,我的看法是,这是什么标记为有趣的引擎罐 - 和缺乏的faces-config.xml中表示注释扫描仪就可以避免罐子和处理保存在部署的的时间。它只是本来不错,如果这些扫描器语义都有些更清楚解释的地方,&LT;咒骂删除&GT;!

If you're wondering why the behavior has to be so nutty, it's because the JSF configs can be decentralized and scattered across 1st and 3rd party libraries this way. And if you're wondering about the whole presence of the faces-config.xml in a jar being a factor, well, my opinion is that that is what marks a Jar as interesting to the engine - and the absence of a faces-config.xml means the annotation scanner can avoid that jar and save on processing at deployment time. It just would have been nice if those scanner semantics were a little more clearly explained somewhere, <expletives deleted>!

下面的博客文章是我的理解是非常有用的东西code的这样做的:

The following blog post was very useful in my understanding of what the code was doing:

<一个href=\"http://one-size-doesnt-fit-all.blogspot.com/2007/01/using-multiple-faces-configxml-files-in.html\">http://one-size-doesnt-fit-all.blogspot.com/2007/01/using-multiple-faces-configxml-files-in.html

我真的希望这样可以节省从痛苦的一周像我的人。

I really hope this saves someone from a week of pain like I had.

这篇关于为什么不JSF 2.0 RI(Mojarra)扫描我的班“的注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆