Search code examples
jsf-2cdi

How to inject one @Named bean in other @Named bean in JSF2?


I have the following code:

@Named
@RequestScoped
public class SearchBean{
    private String title;
    private String author;
    // .... getters and setter s
}

In search.xhtml I have:

<h:inputText value="#{searchBean.title}" />
<h:commandButton action=#{srchUI.action}"/>

And I have also the following ControllerBean:

@Named("srchUI")
@RequestScoped
public class SearchUIController {
    public String action(){
        // ...
    }
}

I want to access the SearchBean.title in action() method... how to do it? How to inject this bean in my UI Controller?


Solution

  • Use @Inject.

    @Named("srchUI")
    @RequestScoped
    public class SearchUIController {
    
        @Inject
        private SearchBean searchBean;
    
        public String action(){
    
        }
    
    }