0

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

Each page will have a ViewScoped JSF Backing Bean.

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

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

<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);
    }
}
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • I guess its cause of combination of c:foreach and viewscope... Try to change your `ReservationActionBean` scope into Session or replace the c:foreach with ui:repeat or datatable , look at this http://stackoverflow.com/questions/2842401/jstl-cforeach-causes-viewscoped-bean-to-invoke-postconstruct-on-every-request – Daniel Oct 11 '12 at 07:11
  • @Daniel, If I use `ui:repeat`, the program cannot render the output. I don't know why? I don't want to use `SessionScope` for `ReservationActionBean`. – Zaw Than oo Oct 11 '12 at 07:31
  • 2
    Its a bad idea to use c:foreach with view scope... – Daniel Oct 11 '12 at 07:33
  • @Daniel `ui:repeat` does now work in `p:panelGrid` of `Primefaces`? Any way, I will try use `ui:repeat`. thanks for your supporting. – Zaw Than oo Oct 11 '12 at 07:37
  • Are you using a component library? – jahroy Oct 11 '12 at 07:39
  • @jahroy Primefaces 3 + JSF 2.0 used – Zaw Than oo Oct 11 '12 at 07:41
  • You might need to place your `ui:repeat` inside a `p:column` inside your `p:panelGrid` – Daniel Oct 11 '12 at 07:41
  • @Daniel I update full page code. Please, could you revised again? – Zaw Than oo Oct 11 '12 at 07:47

1 Answers1

5

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.

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>

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.

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555