0

I have seen this question has been asked in this forum but following the solutions provided on those posts, I am not able to inject the spring bean in my converter.

Below is the code snippet:

UserConverter.java class:

@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());  
        }  
    }

}

I am invoking the converter from xhtml like:

<p:autoComplete id="users" value="#{userSearchBean.selecteSearchedUser}"     completeMethod="#   {userSearchBean.searchFriends}"                            var="user" itemLabel="#{user.firstName}" itemValue="#{user}" converter="#{userConverter}" forceSelection="true">
</p:autoComplete>

faces-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 is the spring class that needs to be injected in the UserConverter.java class.

But unable to get any reference of SearchServiceImpl in the UserConverter.java

Thank you in advance for your help.

  • Just make the converter a Spring managed bean instead of a JSF managed bean so that you can use Spring's `@Autowired`. The whole `` entry is totally unnecessary. It's only used if you use `converter="converterId"`, not if you use `converter="#{managedBeanName}"`. – BalusC Jun 28 '13 at 20:06
  • Duplicate/related: http://stackoverflow.com/questions/7531449/cdi-injection-into-a-facesconverter/7531487#7531487, http://stackoverflow.com/questions/10229396/how-to-inject-spring-bean-into-jsf-converter/10229586#10229586 and http://stackoverflow.com/questions/13156671/how-can-i-inject-in-facesconverter/13156834#13156834 – BalusC Jun 28 '13 at 20:08
  • @BalusC you are genius Sir. Hats off to you. – Bubai Misra Jun 28 '13 at 21:02

0 Answers0