3

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?

brandizzi
  • 26,083
  • 8
  • 103
  • 158
Sunil
  • 127
  • 4
  • 11

3 Answers3

4

Use @Inject.

@Named("srchUI")
@RequestScoped
public class SearchUIController {

    @Inject
    private SearchBean searchBean;

    public String action(){

    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-1
public class SearchUIController {

    @ManagedProperty(value = "#{searchBean}")
    private SearchBean searchBean;

    // .. setters and getters for the searchBean
}

Getters-setters are necessary.

palacsint
  • 28,416
  • 10
  • 82
  • 109
-2

Use @Inject and add Get and Set methods on your injected bean!

@Named(value = "postMB")
@SessionScoped
public class PostMB{
   // inject comments on your posts
   @Inject
   private CommentMB commentMB;


   /* ADD GET and SET Methods to commentMB*/
   public CommentBM getCommentMB(){return this.commentMB;}
   public void setCommentMB(CommentMB newMB){this.commentMB = newMB;}
}


@Named(value="commentMB")
@RequestScoped
public class CommentMB{
  ....
}
  • Getters/setters are unnecessary for `@Inject`. Perhaps you're confusing with `@ManagedProperty`. – BalusC Sep 04 '16 at 18:56
  • in my case, work with get and set! but I don't tryed without get and Set.. I will do it now! ... NOTE: I'm using eclipse+Wildfly+JAP(Hibernate). so.. thank you for your feedback! – Miguel Vieira Sep 09 '16 at 02:54