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

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

问题描述

我的基于 Eclipse 的 JSF 项目中有一个 War and 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);  
 }

}

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

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 应该在部署时扫描我的 War 中的 jar,并检测哪些类上有注释(并相应地自动配置应用程序).我的问题是 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.

War 项目有我所有的 .xhtml 文件以及项目的 faces-config.xml,我的 Jar 项目有我所有的 faces 相关 Java 代码(动作 bean、托管 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).如果您将代码保存在单独的 Jar 文件中,并希望扫描/WEB-INF/lib .jar 文件中的 .class 文件以查找注释,您必须在该 Jar 的 META 中有 faces-config.xml -INF 文件夹.您放入 jar 中的 faces-config.xml 可以是空的,它只需要存在即可,否则注释扫描器将跳过您的 jar,就像它是剩下的肉饼一样.

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.

空面-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.所以,只是回顾一下.您的 War 中的 faces-config.xml 进入 WEB-INF.您的每个 Jar 文件中的可能为空的注释扫描器启用 faces-config.xml 进入该 Jar 的 META-INF.

I know a threw out a lot of -INF's there. So, just to recap. Your faces-config.xml in your War goes in WEB-INF. Your potentially empty, annotation scanner enabling, faces-config.xml in each of your Jar files goes in that Jar's META-INF.

如果您想知道为什么行为必须如此疯狂,那是因为 JSF 配置可以通过这种方式分散在第一方和第三方库中.如果你想知道 jar 中 faces-config.xml 的全部存在是一个因素,那么我的观点是,这就是 Jar 对引擎感兴趣的标志 - 并且没有 faces-config.xml 意味着注释扫描器可以避免该 jar 并在部署时节省处理时间.如果在某处能更清楚地解释这些扫描仪语义就好了,<脏话已删除>!

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

以下博客文章对我理解代码的作用非常有用:

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

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天全站免登陆