如何将 Spring bean 注入 JSF 转换器 [英] How to inject Spring bean into JSF converter

查看:22
本文介绍了如何将 Spring bean 注入 JSF 转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 Spring bean 注入 JSF(Primefaces)转换器.我尝试使用 EL 解析器注入 bean.然而,bean 在转换器中是 null.

I need to inject Spring beans into a JSF (Primefaces) converter. I tried to inject beans by using EL resolver. However, the beans are null inside the converters.

我的 JSF 转换器:

My JSF converter:

public class DepartmentConverter implements Converter  {
    private DepartmentService departmentService;
    //getter setter for this property

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        //codes
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        //Codes
    }
}

faces-config.xml:

<converter>
    <converter-id>DepartmentConverter</converter-id>
    <converter-class>com.studinfo.jsf.converter.DepartmentConverter</converter-class>
    <property>
        <property-name>departmentService</property-name>
        <property-class>com.studinfo.services.DepartmentService</property-class>
        <default-value>#{DepartmentService}</default-value>
    </property>
</converter>

EL 解析器:

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

当我调试代码时,departmentService 属性为 null.我可以用同样的方式访问托管 JSF bean 中的 Spring bean.

When I debug my code, the departmentService property is null. I can access the Spring beans inside a managed JSF bean the same way.

推荐答案

在 JSF 2.3 之前,转换器不是注入目标.将转换器改为 JSF 或 Spring 管理的 bean.下面的示例使其成为 JSF 托管 bean:

Until JSF 2.3, converters are no injection targets. Make the converter a JSF or Spring managed bean instead. The below example makes it a JSF managed bean:

@ManagedBean
@RequestScoped
public class DepartmentConverter implements Converter  {
    // ...
}

并将其用作 #{departmentConverter} 而不是 DepartmentConverter.

And use it as #{departmentConverter} instead of DepartmentConverter.

例如

<h:inputSome ... converter="#{departmentConverter}" />

<h:someComponent>
    <f:converter binding="#{departmentConverter}" />
</h:someComponent>

不要忘记从 faces-config.xml 中删除 (如果您使用 @FacesConverter 注释,但除此之外).

Don't forget to remove the <converter> from faces-config.xml (which was at its own already unnecessary if you used the @FacesConverter annotation, but that aside).

这篇关于如何将 Spring bean 注入 JSF 转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆