简体   繁体   English

JSF 2.0转换器无法从单独的jar文件中运行

[英]JSF 2.0 Converter not working from separate jar-file

I have a custom converter to select a Country in a SelectOneMenu component: 我有一个自定义转换器来选择SelectOneMenu组件中的国家/地区:

File: address.jar 文件: address.jar

@FacesConverter(value="CountryConverter", forClass=Country.class)
public class CountryConverter implements Converter {

private CountryBean countryBean = CountryBean.getCountryService();

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return countryBean.find(value);
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null)
        return ((Country)value).getcc_fips();
    else
        return null;
}    

And this is the xhtml text: 这是xhtml文本:

File: Project root 文件: 项目根目录

  <h:selectOneMenu id="country" value="#{cc.attrs.addrEntity.country}">
     <f:selectItem itemLabel="Please select one..." 
             noSelectionOption="true" />
     <f:selectItems value="#{cc.attrs.addrBean.countries}"
                    var="model"
                    itemLabel="#{model.name}"
                    itemValue="#{model}"
                    noSelectionValue="&#8220;no selection&#8221;"/>
     <f:converter ConverterId="CountryConverter"/>        
 </h:selectOneMenu>

I have the converter in a file " address.jar " and when I try to open the page to write the address, then it responds saying "Expression Error: Object with name MyCustomCoverter not found." 我将转换器放在文件“ address.jar ”中,当我尝试打开页面写入地址时,它会回复说“表达式错误:找不到名称为MyCustomCoverter的对象”。 . Even thought when I copy the converter to the project where the xhtml files are, then it works ok. 甚至当我将转换器复制到xhtml文件所在的项目时,它就可以了。 What can I do to solve this? 我该怎么做才能解决这个问题?

Why it doesn't work from a separated jar file? 为什么它不能从分离的jar文件中工作?

Thanks. 谢谢。

You have to supply a JSF 2.0 compatible /META-INF/faces-config.xml file in the JAR file in order to get JSF to auto-scan the JAR file for classes with JSF annotations. 您必须在JAR文件中提供兼容JSF 2.0的/META-INF/faces-config.xml文件,以便让JSF自动扫描JAR文件以获取具有JSF注释的类。

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

Without that file, JSF won't auto-scan the JAR file to save performance and thus your @FacesConverter won't be found nor registered. 如果没有该文件,JSF将不会自动扫描JAR文件以保存性能,因此将无法找到或注册您的@FacesConverter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM