@ManagedProperty

编程入门 行业动态 更新时间:2024-10-11 09:24:29
本文介绍了@ManagedProperty-将一个请求范围内的bean注入另一个请求范围内的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个 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中.例如:

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; } }

和Facelet(search.xhtml):

And the Facelet (search.xhtml):

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

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

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>

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

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

推荐答案

我假定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()); }

但是,假设已设置一个值,则在调用此方法时该值不会为空:

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

public String Submit() { return null; }

Richard Hightower的面向非信徒的JSF的图像:JSF应用程序生命周期.

原因归结于JSF生命周期的工作原理:

The reason is due to how the JSF lifecycle works:

  • 第一次解析#{searchBean...}时,发现它不存在:
    • bean被实例化
    • 执行任何依赖项注入(在这种情况下没有任何依赖项)
    • @PostConstruct方法被调用
    • 将bean放入范围内
    • 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
      • 此过程在 JSF规范中定义.

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

        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;

        但是,这没有任何真正的优势-您跳过了任何输入验证,并且不能将SearchBean.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.

        应该使用SearchBean.Submit()方法执行搜索的应用程序逻辑.

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

  • 更多推荐

    @ManagedProperty

    本文发布于:2023-11-07 19:22:57,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1567206.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:ManagedProperty

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!