java 将一个页面传递到 JSF-2.0 中的另一个页面的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12833918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parameter passing one page to another page in JSF-2.0?
提问by Zaw Than oo
I would like to pass a parameter from one page to another.
我想将参数从一个页面传递到另一个页面。
Each page will have a ViewScopedJSF Backing Bean.
每个页面都有一个ViewScopedJSF Backing Bean。
Although, I try to use <f:param>
I get the following error:
when I click <h:commandLink>
will navigate to another page.
虽然,我尝试使用时<f:param>
出现以下错误:单击时<h:commandLink>
将导航到另一个页面。
ERROR :
错误 :
] Root cause of ServletException.
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean ReservationActionBean. The following problems were found:
- The scope of the object referenced by expression #{param.resvDataModel}, request, is shorter than the referring managed beans (ReservationActionBean) scope of view
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
.........
page1.xhtml
页面1.xhtml
<p:panelGrid style="margin-top:-1px;" id="dashboard">
<ui:repeat value="#{DashBoard.dayList}" var="day">
<p:row>
<p:column style="background:#C1CDCD;width:100px;">
<h:outputText value="#{day}" style="color:#333333;font-size:13px;">
<f:convertDateTime type="date" pattern="EEE, yyyy-MM-dd"/>
</h:outputText>
</p:column>
<ui:repeat value="#{DashBoard.timeSlot}" var="timeSlot">
<p:column style="background:#C1CDCD;text-align: center;">
<h:outputText value="#{timeSlot}" style="font-size:12px;"/>
</p:column>
</ui:repeat>
</p:row>
<ui:repeat value="#{DashBoard.resourceList}" var="res">
<p:row>
<p:column>
<h:outputText value="#{res.name}" style="font-size:12px;"/>
</p:column>
<ui:repeat value="#{DashBoard.getResvDataModelList(day, res)}" var="model">
<p:column style="background:#{model.colour};" colspan="#{model.section}">
<h:commandLink action="reservation" style="display:block;width:#{model.section * 50}px;height:20px;">
<f:param name="model" value="#{ReservationActionBean.resvDataModel}"/>
<!--h:outputText value="#{model.user}"rendered="#{model.resource.name == res.name ? true : false}"style="font-size:12px;"/-->
</h:commandLink>
</p:column>
</ui:repeat>
</p:row>
</ui:repeat>
</ui:repeat>
</p:panelGrid>
page2.xtml
page2.xml
<h:form id="reservationEntryFrom">
<f:metadata>
<f:viewParam name="resvDataModel" value="#{ReservationActionBean.resvDataModel}"/>
</f:metadata>
<!-- other -->
</h:form>
DashBoard.java
仪表盘.java
@ManagedBean(name = "DashBoard")
@ViewScoped
public class DashBoard extends BaseBean {
public List<ResvDataModel> getResvDataModelList(
Date date, MeetingRoom meetingRoom) {
// do operation
}
}
ReservationActionBean.java
预订ActionBean.java
@ManagedBean(name="ReservationActionBean")
@ViewScoped
public class ReservationActionBean extends BaseBean {
@ManagedProperty("#{param.resvDataModel}")
private ResvDataModel resvDataModel;
//other operations
}
ResvDataModel.java
ResvDataModel.java
public class ResvDataModel {
// attribute, getter and sertter
@Override
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
回答by BalusC
The @ManagedProperty
is invoked only once during bean's construction. Imagine that the bean is in session scope and the managed property references a request scoped variable (e.g. a request parameter), then only the parameter of the very first request would be set and it would never be updated with changed request parameter values in subsequent requests afterthe session bean construction. This is considered undesired behaviour. Hence @ManagedProperty
cannot reference something which has a narrower scope than the @ManagedBean
itself.
该@ManagedProperty
bean的施工过程中被调用一次。假设 bean 在会话范围内并且托管属性引用请求范围变量(例如请求参数),那么只会设置第一个请求的参数,并且在后续请求中永远不会使用更改的请求参数值更新它在会话 bean 构建之后。这被认为是不受欢迎的行为。因此@ManagedProperty
不能引用范围比它@ManagedBean
本身更窄的东西。
In this particular case, you need <f:viewParam>
instead. Put the following in page2.xhtml
:
在这种特殊情况下,您需要<f:viewParam>
改为。将以下内容放入page2.xhtml
:
<f:metadata>
<f:viewParam name="resvDataModel" value="#{ReservationActionBean.resvDataModel}" />
</f:metadata>
See also:
也可以看看:
However, there's another problem with this approach. You're passing non-String
object along as a request parameter. The value would be only com.example.ResvDataModel@hashcode
(or whatever the toString()
method of your ResvDataModel
class returns). This information is insufficient to reconstruct exactly the desired ResvDataModel
instance.
但是,这种方法还有另一个问题。您将非String
对象作为请求参数传递。该值将仅为com.example.ResvDataModel@hashcode
(或toString()
您的ResvDataModel
类返回的任何方法)。此信息不足以准确地重建所需的ResvDataModel
实例。
You need to pass some unique identifier or action parameter value along instead of a whole complex Java object which can't be uniquely represented as a String
.
您需要传递一些唯一标识符或操作参数值,而不是传递不能唯一表示为String
.