我有以下代码:
@Named
@RequestScoped
public class SearchBean{
private String title;
private String author;
// .... getters and setter s
}
在search.xhtml
我有:
<h:inputText value="#{searchBean.title}" />
<h:commandButton action=#{srchUI.action}"/>
我还有以下ControllerBean:
@Named("srchUI")
@RequestScoped
public class SearchUIController {
public String action(){
// ...
}
}
我想访问SearchBean.title
方法中的action()
...怎么做?如何在我的UI控制器中注入这个bean?
答案 0 :(得分:4)
使用@Inject
。
@Named("srchUI")
@RequestScoped
public class SearchUIController {
@Inject
private SearchBean searchBean;
public String action(){
}
}
答案 1 :(得分:-1)
public class SearchUIController {
@ManagedProperty(value = "#{searchBean}")
private SearchBean searchBean;
// .. setters and getters for the searchBean
}
吸毒者制定者是必要的。
答案 2 :(得分:-2)
使用@Inject并在注入的bean上添加Get和Set方法!
@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{
....
}