我已经看到在这个论坛中提出了这个问题,但是按照这些帖子中提供的解决方案,我无法在我的转换器中注入 spring bean。
下面是代码片段:
UserConverter.java 类:
@ManagedBean
public class UserConverter implements Converter {
private SearchServiceImpl searchService;
public SearchServiceImpl getSearchService() {
return searchService;
}
public void setSearchService(SearchServiceImpl searchService) {
this.searchService = searchService;
}
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String submittedValue) {
List<User> users = getSearchService().getAllUsers();
if (submittedValue.trim().equals("")) {
return null;
} else {
try {
int number = Integer.parseInt(submittedValue);
for (User user : users) {
if (user.getId() == number) {
return user;
}
}
} catch(NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
}
}
return null;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((User) value).getFirstName());
}
}
}
我正在从 xhtml 调用转换器,例如:
<p:autoComplete id="users" value="#{userSearchBean.selecteSearchedUser}" completeMethod="# {userSearchBean.searchFriends}" var="user" itemLabel="#{user.firstName}" itemValue="#{user}" converter="#{userConverter}" forceSelection="true">
</p:autoComplete>
面孔-config.xml:
<converter>
<converter-id>userConverter</converter-id>
<converter-class>com.mbeans.UserConverter</converter-class>
<property>
<property-name>searchService</property-name>
<property-class>com.services.SearchServiceImpl</property-class>
<default-value>#{searchService}</default-value>
</property>
</converter>
enter code here
SearchServiceImpl 是 Spring 类,需要在 UserConverter.java 类中注入。
但无法在 UserConverter.java 中获取 SearchServiceImpl 的任何引用
预先感谢您的帮助。