我无法在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() : "";
}
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<context:annotation-config />
<context:component-scan base-package="com.test.example"/>
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
服务:
@Service
@Transactional
public class AdvertiserServiceImpl implements IAdvertiserService { …Run Code Online (Sandbox Code Playgroud) 请帮我找到解决方案.
这是xhtml代码:
<p:selectOneMenu value="#{activteBean.act.activiteFamille}"
converter="familleAct"
var="f" required="Une famille est obligatoire" >
<f:selectItems value="#{activteBean.actFamList}" var="famille" itemLabel="# {famille.dsgFam}" itemValue="#{famille}"/>
<p:column>#{f.refFam}</p:column>
<p:column>#{f.dsgFam}</p:column>
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
这是我的转换器:
@FacesConverter(forClass=ActiviteFamille.class,value="familleAct" )
public class ActiviteFamilleConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String code) {
if (code.trim().equals("")) {
return null;
} else {
ActiviteFamilleDao actFamDao = new ActiviteFamilleDao();
List<ActiviteFamille> actFamList = actFamDao.findAll();
for (ActiviteFamille af : actFamList) {
if (af.getRefFam().equals(code)) {
return af;
}
}
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, …Run Code Online (Sandbox Code Playgroud)