@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 中吗?例如:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设
SearchBean.input
将绑定到 输入字段:类似这样:
如果是这样,那么这将为空:
但是,假设有一个值已设置,调用此方法时它不会为 null:
来自 Richard Hightower 的 针对非信徒的 JSF 的图片: JSF 应用程序生命周期。
原因在于 JSF 生命周期的工作方式:
#{searchBean...}
首次解析并发现不存在时:SearchBean.setInput (String)
在更新模型值阶段调用SearchBean.Submit()
在调用应用程序阶段调用此过程在 JSF 规范。
现在,如果直接从参数映射注入
SearchBean.input
,则在@PostConstruct
期间它不会为 null:但是,这样做并没有任何真正的优势 - 您正在跳过任何输入验证,并且您不能使用
SearchBean.input
作为字段绑定,因为它将在更新模型值阶段被覆盖。SearchBean.Submit()
方法是执行搜索的应用程序逻辑所在的位置。I assume that
SearchBean.input
will be bound to an input field:Something like this:
If so, then this will be null:
But, assuming a value has been set, it will not be null when this method is invoked:
Image from Richard Hightower's JSF for nonbelievers: The JSF application lifecycle.
The reason is due to how the JSF lifecycle works:
#{searchBean...}
is first resolved and found not to exist:SearchBean.setInput(String)
is invoked in the Update Model Values phaseSearchBean.Submit()
is invoked in the Invoke Application phaseThis 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
: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.