0

我想将参数从一个页面传递到另一个页面。

每个页面都有一个ViewScoped JSF 支持 Bean。

虽然,我尝试使用<f:param>我得到以下错误:当我点击时<h:commandLink>将导航到另一个页面。

错误 :

] 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

<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.xml

<h:form id="reservationEntryFrom">
    <f:metadata>
        <f:viewParam name="resvDataModel" value="#{ReservationActionBean.resvDataModel}"/>
    </f:metadata>

    <!-- other -->

</h:form>

仪表板.java

@ManagedBean(name = "DashBoard")
@ViewScoped
public class DashBoard extends BaseBean {

    public List<ResvDataModel> getResvDataModelList(
            Date date, MeetingRoom meetingRoom) {

        // do operation
    }
}

ReservationActionBean.java

@ManagedBean(name="ReservationActionBean")
@ViewScoped
public class ReservationActionBean extends BaseBean  {

    @ManagedProperty("#{param.resvDataModel}")
    private ResvDataModel resvDataModel;

    //other operations
}

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

1 回答 1

5

在 bean的@ManagedProperty构造过程中只调用一次。假设 bean 在会话范围内,并且托管属性引用了请求范围的变量(例如请求参数),那么只会设置第一个请求的参数,并且在后续请求中永远不会使用更改的请求参数值来更新它会话 bean 构建之后。这被认为是不良行为。因此@ManagedProperty不能引用比@ManagedBean自身范围更窄的东西。

在这种特殊情况下,您需要<f:viewParam>。将以下内容放入page2.xhtml

<f:metadata>
    <f:viewParam name="resvDataModel" value="#{ReservationActionBean.resvDataModel}" />
</f:metadata>

也可以看看:

但是,这种方法还有另一个问题。您将非String对象作为请求参数传递。该值将是 only com.example.ResvDataModel@hashcode(或toString()您的ResvDataModel类的方法返回的任何值)。此信息不足以准确重建所需的ResvDataModel实例。

您需要传递一些唯一标识符或操作参数值,而不是传递一个无法唯一表示为String.

于 2012-10-11T11:47:15.507 回答