本文介绍了在JSF-2.0中将一页传递到另一页的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数从一页传递到另一页.

I would like to pass a parameter from one page to another.

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

Each page will have a ViewScoped JSF Backing Bean.

尽管,我尝试使用<f:param>,但出现以下错误:当我单击<h:commandLink>时,将导航到另一页.

Although, I try to use <f:param> I get the following error:when I click <h:commandLink> will navigate to another page.

错误:

] 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

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

page2.xtml

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

    <!-- other -->

</h:form>

DashBoard.java

DashBoard.java

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

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

        // do operation
    }
}

ReservationActionBean.java

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

推荐答案

@ManagedProperty在bean的构造过程中仅被调用一次.想象一下,该bean在会话范围内,并且托管属性引用了一个请求范围变量(例如,一个请求参数),那么将只设置第一个请求的参数,并且在后续请求中永远不会使用更改后的请求参数值对其进行更新.在会话Bean构建之后 .这被认为是不希望的行为.因此,@ManagedProperty不能引用范围比@ManagedBean本身更窄的内容.

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 after the session bean construction. This is considered undesired behaviour. Hence @ManagedProperty cannot reference something which has a narrower scope than the @ManagedBean itself.

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

In this particular case, you need <f:viewParam> instead. Put the following in page2.xhtml:

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

另请参见:

  • ViewParam与@ManagedProperty(值=#{param.id} ")
  • See also:

    • ViewParam vs @ManagedProperty(value = "#{param.id}")
    • 但是,这种方法还有另一个问题.您正在将非String对象作为请求参数传递.该值只能是com.example.ResvDataModel@hashcode(或ResvDataModel类的toString()方法返回的任何值).此信息不足以准确地重建所需的ResvDataModel实例.

      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的整个复杂Java对象.

      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.

      这篇关于在JSF-2.0中将一页传递到另一页的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:11