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

查看:30
本文介绍了为什么 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 项目拥有我所有与面部相关的 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.

空的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.所以,只是回顾一下.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天全站免登陆