@ManagedProperty-将一个请求范围内的bean注入另一个请求范围内的bean [英] @ManagedProperty - Inject one request scoped bean into another request scoped 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
将绑定到类似这样的东西:
<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-将一个请求范围内的bean注入另一个请求范围内的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!