@ManagedProperty - 将一个请求作用域 bean 注入另一个请求作用域 bean

发布于 2024-11-01 17:46:05 字数 1563 浏览 0 评论 0原文

我有这个 SearchBean

@ManagedBean(name = "searchBean")
@RequestScoped
public class SearchBean implements Serializable
{
    private String input = null;

    // getter methods
    public String getInput() {
        return input;
    }

    // setter method
    public void setInput(String input) {
        this.input = input;
    }

    public String Submit() {
        return null;
    }    
}

我可以使用 @ManagedProperty 将它注入到另一个 bean 中吗?例如:

@ManagedBean(name = "bookBean")
@RequestScoped
public class BookBean implements Serializable
{
    @ManagedProperty(value = "#{searchBean}")
    private SearchBean searchBean;  

    @PostConstruct
    public void init()
    {   
       System.out.println("Value: " + searchBean.getInput());
    }

    public SearchBean getSearchBean() {
       return searchBean;
    }

    public void setSearchBean(SearchBean searchBean) {
       this.searchBean = searchBean;
    }   
}

Facelet (search.xhtml):

<h:form id="formSearch">
   <h:commandButton value="Search" action="#{searchBean.Submit}" />
</h:form>

更新:我已通过 search.xhtml 插入到 book.xhtml 中ui:insert 组件如下:

<h:form id="formBooks">
   <ui:insert name="search">
      <ui:include src="/templates/common/search.xhtml"/>
   </ui:insert> 
</h:form>

上面的 searchBean.getInput() 方法应返回一个值作为表单提交的结果。上述注射方法可以吗?

I have this SearchBean:

@ManagedBean(name = "searchBean")
@RequestScoped
public class SearchBean implements Serializable
{
    private String input = null;

    // getter methods
    public String getInput() {
        return input;
    }

    // setter method
    public void setInput(String input) {
        this.input = input;
    }

    public String Submit() {
        return null;
    }    
}

Can I inject it into another bean using @ManagedProperty. For example:

@ManagedBean(name = "bookBean")
@RequestScoped
public class BookBean implements Serializable
{
    @ManagedProperty(value = "#{searchBean}")
    private SearchBean searchBean;  

    @PostConstruct
    public void init()
    {   
       System.out.println("Value: " + searchBean.getInput());
    }

    public SearchBean getSearchBean() {
       return searchBean;
    }

    public void setSearchBean(SearchBean searchBean) {
       this.searchBean = searchBean;
    }   
}

And the Facelet (search.xhtml):

<h:form id="formSearch">
   <h:commandButton value="Search" action="#{searchBean.Submit}" />
</h:form>

UPDATE: I have search.xhtml inserted into book.xhtml via a ui:insert component as follow:

<h:form id="formBooks">
   <ui:insert name="search">
      <ui:include src="/templates/common/search.xhtml"/>
   </ui:insert> 
</h:form>

The searchBean.getInput() method above should return a value as a result of a form's submission. Is the above method of injection possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2024-11-08 17:46:05

我假设 SearchBean.input 将绑定到 输入字段

public class SearchBean implements Serializable {
    private String input = null;

类似这样:

<h:inputText value="#{searchBean.input}" />

如果是这样,那么这将为空:

@PostConstruct
public void init()
{   
   System.out.println("Value: " + searchBean.getInput());
}

但是,假设有一个值已设置,调用此方法时它不会为 null:

public String Submit() {
    return null;
}

来自 Richard Hightower 的 JSF 的非信徒的图片:JSF 应用程序生命周期

来自 Richard Hightower 的 针对非信徒的 JSF 的图片: JSF 应用程序生命周期

原因在于 JSF 生命周期的工作方式:

  1. #{searchBean...} 首次解析并发现不存在时:
    • bean 已实例化
    • 执行任何依赖项注入(本例中没有任何依赖项注入)
    • 调用@PostConstruct方法
    • bean 被放入作用域
  2. 假设应用请求值和验证阶段成功,SearchBean.setInput (String) 在更新模型值阶段调用
  3. SearchBean.Submit() 在调用应用程序阶段调用

此过程在 JSF 规范


现在,如果直接从参数映射注入 SearchBean.input,则在 @PostConstruct 期间它不会为 null:

@ManagedProperty(value = "#{param.someParamName}")
private String input;

但是,这样做并没有任何真正的优势 - 您正在跳过任何输入验证,并且您不能使用 SearchBean.input 作为字段绑定,因为它将在更新模型值阶段被覆盖。

SearchBean.Submit() 方法是执行搜索的应用程序逻辑所在的位置。

I assume that SearchBean.input will be bound to an input field:

public class SearchBean implements Serializable {
    private String input = null;

Something like this:

<h:inputText value="#{searchBean.input}" />

If so, then this will be null:

@PostConstruct
public void init()
{   
   System.out.println("Value: " + searchBean.getInput());
}

But, assuming a value has been set, it will not be null when this method is invoked:

public String Submit() {
    return null;
}

Image from Richard Hightower's JSF for nonbelievers: The JSF application lifecycle

Image from Richard Hightower's JSF for nonbelievers: The JSF application lifecycle.

The reason is due to how the JSF lifecycle works:

  1. When #{searchBean...} is first resolved and found not to exist:
    • The bean is instantiated
    • Any dependency injections are performed (there aren't any in this case)
    • @PostConstruct method is invoked
    • The bean is placed into scope
  2. Assuming the Apply Request Values and Validations phases succeed, SearchBean.setInput(String) is invoked in the Update Model Values phase
  3. SearchBean.Submit() is invoked in the Invoke Application phase

This process is defined in the JSF specification.


Now, if SearchBean.input were injected directly from the parameter map, it would not be null during @PostConstruct:

@ManagedProperty(value = "#{param.someParamName}")
private String input;

However, there aren't any real advantages to this - you're skipping any input validation and you can't use SearchBean.input as a field binding because it will be overwritten in the Update Model Values phase.

The SearchBean.Submit() method is where your application logic for performing the search should go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文