参数在JSF-2.0中将一个页面传递给另一个页面?

时间:2012-10-11 06:59:12

标签: java jsf

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

每个页面都有一个 ViewScoped JSF Backing 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.xtml

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

    <!-- other -->

</h:form>

DashBoard.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);
    }
}

1 个答案:

答案 0 :(得分:5)

在bean构造期间仅调用@ManagedProperty一次。假设bean在会话范围内,托管属性引用请求范围的变量(例如请求参数),那么只会设置第一个请求的参数,并且永远不会在后续请求中使用更改的请求参数值更新它会话bean构造之后。这被认为是不希望的行为。因此@ManagedProperty无法引用范围比@ManagedBean本身更窄的内容。

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

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

另见:

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

您需要传递一些唯一标识符或操作参数值,而不是整个复杂的Java对象,它不能唯一地表示为String