自动连接服务在jsf Converter中为空

时间:2014-07-22 09:06:23

标签: spring jsf primefaces

我无法在jsf转换器中使用@Autowired注入服务:

转换器:

@Component("advertiserConverter")
@FacesConverter("advertiserConverter")
public class AdvertiserConverter implements Converter , Serializable {

 @Autowired
 private IAdvertiserService advertiserService;

 public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.length() == 0) {
        return null;
    }
    Long id = Long.parseLong(value);
    return advertiserService.findAdvertiser(id);
 }

 public String getAsString(FacesContext context, UIComponent component, Object value) {
    return value instanceof Advertiser ? ((Advertiser) value).getId().toString() : "";
 }
}

的applicationContext.xml:

<context:annotation-config />
<context:component-scan base-package="com.test.example"/>
<tx:annotation-driven />

服务:

@Service
@Transactional
public class AdvertiserServiceImpl implements IAdvertiserService {

}

4 个答案:

答案 0 :(得分:7)

最后我找到了解决方案:

转换器:

@Service
public class AdvertiserConverter implements Converter{

XHTML:

converter="#{advertiserConverter}"

而不是

converter="advertiserConverter"

答案 1 :(得分:0)

您无需创建自己的converter

如果您想要generic converter,那么Omnifaces SelectItemsConverter就是最好的。

Omni Faces Converter

如果您不想使用Omnifaces,请使用以下通用转换器

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {

    private static Map<Object, String> entities = new WeakHashMap<Object, String>();

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object entity) {
        synchronized (entities) {
            if (!entities.containsKey(entity)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(entity, uuid);
                return uuid;
            } else {
                return entities.get(entity);
            }
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(uuid)) {
                return entry.getKey();
            }
        }
        return null;
    }

}

答案 2 :(得分:0)

JSF没有认识到Spring Beans。因此,您需要弹出spring和JSF bean以获得更好的示例,请参考here

JSF托管bean

@FacesConverter("advertiserConverter")
public class AdvertiserConverter implements Converter , Serializable {


 public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.length() == 0) {
        return null;
    }
    Long id = Long.parseLong(value);
    ApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext((ServletContext) FacesContext
                    .getCurrentInstance().getExternalContext()
                    .getContext());
    IAdvertiserService advertiserService= (AdvertiserServiceImpl) ctx.getBean("advertiserServiceImpl");
    return advertiserService.findAdvertiser(id);
 }

 public String getAsString(FacesContext context, UIComponent component, Object value) {
    return value instanceof Advertiser ? ((Advertiser) value).getId().toString() : "";
 }
}

Spring bean

@Service("advertiserServiceImpl")
@Transactional
public class AdvertiserServiceImpl implements IAdvertiserService {

}

faces-config.xml中

<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_1.xsd"
    version="2.1">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

Web.xml中

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

答案 3 :(得分:-1)

FWIW,我不得不转换我的&#34; @ Autowired&#34;服务是@ ManagedProperty&... ......这样的事情:

-
-
-

@ManagedProperty(value = "#{myService}")
private MyService myService;
public MyService getMyService()
{
    return myService;
}
public void setMyService(MyService myService)
{
    this.myService = myService;
}

-
-
-