myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mike Kienenberger" <mkien...@gmail.com>
Subject Re: Dynamic Value binding - please help!
Date Fri, 20 Apr 2007 17:33:17 GMT
Probably not because this is likely a request-scoped bean and the
value will be null on the next request.   You definitely don't want to
serialize a component -- I'm pretty sure there are other issues.

I haven't really done much with programmic creation of components in
two years as it's far easier to use facelets templates and other
techniques, so I'm no expert in the subject.  However, every example
I've seen uses the getter.

On 4/20/07, Leyzerzon, Simeon <simeon.leyzerzon@credit-suisse.com> wrote:
> The fact that I create it only if it doesn't yet exist with  "if (this.reportingPeriodsSelectItemsControl
== null){}" doesn't count?
>
>
> -----Original Message-----
> From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> Sent: Friday, April 20, 2007 1:17 PM
> To: MyFaces Discussion
> Subject: Re: Dynamic Value binding - please help!
>
> I think this is because you need to create the component in the
> getter, not the setter.    If you create it in the setter, you'll get
> a new instance of the components created each time you request the page again.
>
> On 4/20/07, Leyzerzon, Simeon <simeon.leyzerzon@credit-suisse.com> wrote:
> > Ok, another issue:
> >
> > <Apr 20, 2007 12:56:18 PM EDT> <Error> <HTTP> <BEA-101017>
<[weblogic.servlet.internal.WebAppServletContext@1cc3245 - appName: 'GISWeb', name: 'GISWeb.war',
context-path: '/GISWeb'] Root cause of ServletException.
> > java.lang.IllegalStateException: Client-id : repPeriods is duplicated in the faces
tree. Component : searchFrm:repPeriods, path: {Component-Path : [Class: org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId:
/jsp/maintenance/search.jsp][Class: javax.faces.component.html.HtmlPanelGrid,Id: _idJsp0][Class:
javax.faces.component.html.HtmlForm,Id: searchFrm][Class: org.apache.myfaces.custom.fieldset.Fieldset,Id:
_idJsp1][Class: javax.faces.component.html.HtmlPanelGrid,Id: root][Class: javax.faces.component.html.HtmlSelectOneMenu,Id:
_idJsp6][Class: org.apache.myfaces.custom.selectitems.UISelectItems,Id: repPeriods]}
> >         at org.apache.myfaces.application.jsp.JspStateManagerImpl.checkForDuplicateIds(JspStateManagerImpl.java:329)
> >         at org.apache.myfaces.application.jsp.JspStateManagerImpl.checkForDuplicateIds(JspStateManagerImpl.java:341)
> >         at org.apache.myfaces.application.jsp.JspStateManagerImpl.checkForDuplicateIds(JspStateManagerImpl.java:341)
> >         at org.apache.myfaces.application.jsp.JspStateManagerImpl.checkForDuplicateIds(JspStateManagerImpl.java:341)
> >         at org.apache.myfaces.application.jsp.JspStateManagerImpl.checkForDuplicateIds(JspStateManagerImpl.java:338)
> >         Truncated. see log file for complete stacktrace
> > >
> >
> >
> >
> > This happens when the form is resubmitted as a reaction on a change event in the
period selectOneMenu:
> >
> >       <h:outputText value="Period" />
> >            <h:selectOneMenu value="#{searchHandler.period}" onchange="document.forms['searchFrm'].submit();"
> >                   valueChangeListener="#{searchHandler.periodChanged}" >
> >                 <f:selectItems value="#{searchHandler.periods}"/>
> >       </h:selectOneMenu>
> >
> >
> >                 <h:outputText value="Reporting Period"  />
> >                      <h:selectOneMenu value="#{searchHandler.reportingPeriod.rawDate}"
> >                                binding="#{searchHandler.reportingPeriodSelectOneMenuControl}">
> >                      </h:selectOneMenu>
> >
> > The behavior I'm looking for is on the value change event in Period, the combo with
reportingPeriods should be redisplayed with different set of values.
> >
> > This is the code in the periodChanged():
> > ======================================================================
> > ==================================================================
> > public void periodChanged(ValueChangeEvent event) {
> >
> >
> >     this.setContext(SwitchContextFactory.getContext(userProfile, this.getPeriod()));
> >     logger.debug("\t on which the period is: " +
> > this.getContext().getPeriod());
> >
> >     this.getSearchService().setContext(this.getContext());
> >     this.getSearchService().getReportingPeriods();
> >
> >
> >
> >
> >     if (this.reportingPeriodsSelectItemsControl == null){
> >
> >         this.reportingPeriodsSelectItemsControl = (UISelectItems) FacesContext
> >                     .getCurrentInstance().getApplication().createComponent(
> >                             UISelectItems.COMPONENT_TYPE);
> >         this.reportingPeriodsSelectItemsControl.setId("repPeriods");
> >     }
> >
> >
> >
> >     reportingPeriodsSelectItemsControl.setValue(reportingPeriods);
> >     reportingPeriodsSelectItemsControl.setVar("rptPeriod");
> >     if (DAILY.equals(this.getPeriod())) {
> >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> >                     FacesContext.getCurrentInstance().getApplication()
> >                             .createValueBinding(
> >                                     "#{rptPeriod.reportingDateDaily}"));
> >     }else if (MONTHLY.equals(this.getPeriod())) {
> >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> >                     FacesContext.getCurrentInstance().getApplication()
> >                             .createValueBinding(
> >                                     "#{rptPeriod.reportingDateMonthly}"));
> >     }
> >
> >     reportingPeriodsSelectItemsControl.setValueBinding("itemValue",
> >
> > FacesContext.getCurrentInstance().getApplication().createValueBinding(
> > "#{rptPeriod.rawDate}"));
> >
> >
> >
> >
> > this.reportingPeriodSelectOneMenuControl.getChildren().add(reportingPe
> > riodsSelectItemsControl);
> >
> >
> >
> > }
> > ======================================================================
> > ==================================================================
> >
> >
> > Here is the code in the setter fro <h:selectOneMenu> for reportingPeriod:
> > ======================================================================
> > ==================================================================
> > public void setReportingPeriodSelectOneMenuControl(
> >         HtmlSelectOneMenu _reportingPeriodSelectOneMenuControl) {
> >
> >
> >     if (reportingPeriods == null){
> >         logger.debug("reportingPeriods == null: " +
> >                 (reportingPeriods==null) + " building reportingPeriods list...");
> >         reportingPeriods = this.getSearchService().getReportingPeriods();
> >     }
> >
> >
> >     //building <t:selectItems>
> >     if (this.reportingPeriodsSelectItemsControl == null){
> >         logger.debug("creating new <t:selectItems>");
> >         this.reportingPeriodsSelectItemsControl = (UISelectItems) FacesContext
> >                     .getCurrentInstance().getApplication().createComponent(
> >                             UISelectItems.COMPONENT_TYPE);
> >         this.reportingPeriodsSelectItemsControl.setId("repPeriods");
> >     }
> >
> >
> >     this.reportingPeriodsSelectItemsControl.setValue(reportingPeriods);
> >     this.reportingPeriodsSelectItemsControl.setVar("rptPeriod");
> >     if (DAILY.equals(this.getPeriod())) {
> >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> >                     FacesContext.getCurrentInstance().getApplication()
> >                             .createValueBinding(
> >                                     "#{rptPeriod.reportingDateDaily}"));
> >     }else if (MONTHLY.equals(this.getPeriod())) {
> >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> >                     FacesContext.getCurrentInstance().getApplication()
> >                             .createValueBinding(
> >                                     "#{rptPeriod.reportingDateMonthly}"));
> >     }
> >
> >     reportingPeriodsSelectItemsControl.setValueBinding("itemValue",
> >
> > FacesContext.getCurrentInstance().getApplication().createValueBinding(
> > "#{rptPeriod.rawDate}"));
> >
> >
> >
> >
> > _reportingPeriodSelectOneMenuControl.getChildren().add(reportingPeriod
> > sSelectItemsControl);
> >
> >
> >     this.reportingPeriodSelectOneMenuControl = _reportingPeriodSelectOneMenuControl;
> >     logger.debug("setReportingPeriodSelectOneMenuControl()" +
> > _reportingPeriodSelectOneMenuControl);
> > }
> > ======================================================================
> > ==================================================================
> >
> >
> >
> > Appreciate if you could give me a direction in fixing this.
> >
> > Thank you in advance.
> > Simeon
> >
> >
> > -----Original Message-----
> > From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> > Sent: Friday, April 20, 2007 12:38 PM
> > To: MyFaces Discussion
> > Subject: Re: Dynamic Value binding - please help!
> >
> > I don't have any more ideas from what you've posted.   If it were me,
> > I'd step through the process with the debugger and see why at this point.  Make
sure the value bindings are set to what you think they are, and if they are, see what's calling
rawDate.
> >
> > On 4/20/07, Leyzerzon, Simeon <simeon.leyzerzon@credit-suisse.com> wrote:
> > > Hah! That works, thanks, but now I'm facing a new complication: #{rptPeriod.reportingDateMonthly}
and #{rptPeriod.reportingDateMonthly} doesn't seem to be mapping to the right field in the
underlying bean.  Let me explain...
> > >
> > > 'rtpPeriod' is a representation of ReportingPeriod bean, instances of which
populate the collection behind <t:selectItems> component.  These ReportingPeriod beans
have 3 properties for display, each formatted differently, and set by Spring-based DAOlike
this:
> > >
> > >
> > > public void setReportingDate(Date reportingDate) {
> > >         //logger.debug("set reporting date to: " + reportingDate);
> > >
> > >         this.reportingDate = reportingDate;
> > >         this.setReportingDateDaily(new SimpleDateFormat("MMM dd yyyy").format(reportingDate));
> > >         this.setReportingDateMonthly(new SimpleDateFormat("MMM yyyy").format(reportingDate));
> > >         this.setRawDate(new SimpleDateFormat("MM/dd/yyyy").format(reportingDate));
> > >     }
> > >
> > > What I see on the page though, is a combo box with the content populated in
the following manner:
> > >
> > >                 03/01/2007
> > >                 02/01/2007
> > >                 01/01/2007, etc.
> > >
> > > That is instead of using #{rptPeriod.reportingDateMonthly} or #{rptPeriod.reportingDateDaily},
#{rtpPeriod.rawDate} seems to be used.
> > >
> > > Here is my current code:
> > >
> > > ====================================================================
> > > == ===============================================================
> > > public void setReportingPeriodSelectOneMenuControl(
> > >         HtmlSelectOneMenu reportingPeriodSelectOneMenuControl) {
> > >
> > >
> > >     if (reportingPeriods == null){
> > >         logger.debug("reportingPeriods == null: " +
> > >                 (reportingPeriods==null) + "building reportingPeriods list...");
> > >         reportingPeriods = this.getSearchService().getReportingPeriods();
> > >     }
> > >
> > >
> > >     //building <t:selectItems>
> > >     if (reportingPeriodsSelectItemsControl == null){
> > >         reportingPeriodsSelectItemsControl = (UISelectItems) FacesContext
> > >                     .getCurrentInstance().getApplication().createComponent(
> > >                             UISelectItems.COMPONENT_TYPE);
> > >
> > >     }
> > >
> > >     reportingPeriodsSelectItemsControl.setValue(reportingPeriods);
> > >     reportingPeriodsSelectItemsControl.setVar("rptPeriod");
> > >     if (DAILY.equals(this.getPeriod())) {
> > >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> > >                     FacesContext.getCurrentInstance().getApplication()
> > >                             .createValueBinding(
> > >                                     "#{rptPeriod.reportingDateDaily}"));
> > >     }else if (MONTHLY.equals(this.getPeriod())) {
> > >         reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> > >                     FacesContext.getCurrentInstance().getApplication()
> > >                             .createValueBinding(
> > >                                     "#{rptPeriod.reportingDateMonthly}"));
> > >     }
> > >
> > >     reportingPeriodsSelectItemsControl.setValueBinding("itemValue",
> > >
> > > FacesContext.getCurrentInstance().getApplication().createValueBindin
> > > g(
> > > "#{rptPeriod.rawDate}"));
> > >
> > >
> > >
> > >
> > > reportingPeriodSelectOneMenuControl.getChildren().add(reportingPerio
> > > ds
> > > SelectItemsControl);
> > >
> > >
> > >     this.reportingPeriodSelectOneMenuControl = reportingPeriodSelectOneMenuControl;
> > >     logger.debug("setReportingPeriodSelectOneMenuControl()" +
> > > reportingPeriodSelectOneMenuControl);
> > > }
> > >
> > > }
> > > ====================================================================
> > > == ===============================================================
> > >
> > > ReportingPeriod bean is also registered as a managed-property of the backing
bean on the faces-config.xml if it makes any difference.
> > >
> > > Thanks again!!!
> > > Simeon
> > >
> > >
> > > -----Original Message-----
> > > From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> > > Sent: Friday, April 20, 2007 10:26 AM
> > > To: MyFaces Discussion
> > > Subject: Re: Dynamic Value binding - please help!
> > >
> > > You need to assign ValueBinding objects, not strings.
> > >
> > > You'd typically do something like this [example borrowed and modified from
another posting]:
> > >
> > >     FacesContext context = FacesContext.getCurrentInstance();
> > >    Application app = context.getApplication();
> > >
> > >    reportingPeriodsSelectItemsControl.setValueBinding("itemLabel",
> > > app.createValueBinding("#{rptPeriod.reportingDateMonthly}"));
> > >
> > >
> > >
> > > On 4/20/07, Leyzerzon, Simeon <simeon.leyzerzon@credit-suisse.com> wrote:
> > > >
> > > >
> > > >
> > > > Hi,
> > > >
> > > > I need to create the following construct dynamically in the backing bean:
> > > >
> > > > ==================================================================
> > > > == == ======================== <h:outputText value="Reporting
> > > > Period"
> > > > />
> > > >      <h:selectOneMenu
> > > > value="#{searchHandler.reportingPeriod.rawDate}"
> > > >
> > > > binding="#{searchHandler.reportingPeriodSelectOneMenuControl}">
> > > >
> > > >          <t:selectItems value="#{searchHandler.reportingPeriods}"
> > > > var="rptPeriod"
> > > >                 itemLabel="#{rptPeriod.reportingDateMonthly}"
> > > > itemValue="#{rptPeriod.rawDate}"
> > > >
> > > > binding="#{searchHandler.reportingPeriodsSelectItemsControl}"
> > > >          />
> > > >      </h:selectOneMenu>
> > > >
> > > > ==================================================================
> > > > ==
> > > > ==
> > > > ========================
> > > >
> > > > So I'm doing the following in my setter:
> > > >
> > > > ==================================================================
> > > > == == ======================== public void
> > > > setReportingPeriodSelectOneMenuControl(
> > > >         HtmlSelectOneMenu
> > > > reportingPeriodSelectOneMenuControl) {
> > > >
> > > >
> > > >     if (reportingPeriods == null){
> > > >         logger.debug("reportingPeriods == null: " +
> > > >                 (reportingPeriods==null) + "building
> > > > reportingPeriods list...");
> > > >         reportingPeriods =
> > > > this.getSearchService().getReportingPeriods();
> > > >     }
> > > >
> > > >
> > > >     //building <t:selectItems>
> > > >     if (reportingPeriodsSelectItemsControl == null){
> > > >         reportingPeriodsSelectItemsControl =
> > > > (UISelectItems) FacesContext
> > > >
> > > > .getCurrentInstance().getApplication().createComponent(
> > > >                             UISelectItems.COMPONENT_TYPE);
> > > >
> > > >     }
> > > >
> > > >
> > > > reportingPeriodsSelectItemsControl.setValue(reportingPeriods);
> > > >     reportingPeriodsSelectItemsControl.setVar("rptPeriod");
> > > >
> > > >     String label = null;
> > > >
> > > >
> > > >
> > > >     if (DAILY.equals(this.getPeriod())) {
> > > >         label = "#{rptPeriod.reportingDateDaily}";
> > > >     }else if (MONTHLY.equals(this.getPeriod())) {
> > > >         label = "#{rptPeriod.reportingDateMonthly}";
> > > >     }
> > > >
> > > >
> > > >     reportingPeriodsSelectItemsControl.setItemLabel(label);
> > > >
> > > > reportingPeriodsSelectItemsControl.setItemValue("#{rptPeriod.rawDate}"
> > > > );
> > > >
> > > >
> > > >
> > > > reportingPeriodSelectOneMenuControl.getChildren().add(reportingPer
> > > > io
> > > > ds
> > > > SelectItemsControl);
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >     this.reportingPeriodSelectOneMenuControl =
> > > > reportingPeriodSelectOneMenuControl;
> > > >     logger.debug("setReportingPeriodSelectOneMenuControl()"
> > > > + reportingPeriodSelectOneMenuControl);
> > > > }
> > > >
> > > > ==================================================================
> > > > ==
> > > > ==
> > > > ==========================
> > > >
> > > > Unfortunately, what I am getting in the page is the combo
> > > > populated with a bunch of strings:
> > > > #{rptPeriod.reportingDateMonthly}
> > > >
> > > > What am I missing here to link this variable 'rtpPeriod' to the
> > > > value binding expression.  Or should there be a different syntax?
> > > >
> > > > Thanks for you help.
> > > > Simeon
> > > >
> > > > ==================================================================
> > > > == == ======== Please access the attached hyperlink for an
> > > > important electronic communications disclaimer:
> > > >
> > > > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> > > > ==================================================================
> > > > ==
> > > > ==
> > > > ========
> > > >
> > > >
> > > >
> > > >
> > >
> > > ====================================================================
> > > == ======== Please access the attached hyperlink for an important
> > > electronic communications disclaimer:
> > >
> > > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> > > ====================================================================
> > > ==
> > > ========
> > >
> > >
> >
> > ======================================================================
> > ======== Please access the attached hyperlink for an important
> > electronic communications disclaimer:
> >
> > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> > ======================================================================
> > ========
> >
> >
>
> ==============================================================================
> Please access the attached hyperlink for an important electronic communications disclaimer:
>
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> ==============================================================================
>
>

Mime
View raw message