Joe*_*ore 5 java converter jsf-2
我有一个自定义转换器来选择SelectOneMenu组件中的国家/地区:
文件: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;
}    
这是xhtml文本:
文件:项目根目录
  <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="“no selection”"/>
     <f:converter ConverterId="CountryConverter"/>        
 </h:selectOneMenu>
我将转换器放在文件" address.jar "中,当我尝试打开页面写入地址时,它会回复说"表达式错误:找不到名称为MyCustomCoverter的对象"..甚至当我将转换器复制到xhtml文件所在的项目时,它就可以了.我该怎么做才能解决这个问题?
为什么它不能从分离的jar文件中工作?
谢谢.
Bal*_*usC 16
您必须/META-INF/faces-config.xml在JAR文件中提供JSF 2.0兼容文件,以便让JSF自动扫描具有JSF注释的类的JAR文件.
<?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>
如果没有该文件,JSF将不会自动扫描JAR文件以保存性能,因此您@FacesConverter将无法找到也无法注册.
| 归档时间: | 
 | 
| 查看次数: | 2106 次 | 
| 最近记录: |