0

I would like to retrieve the user input from a component within my Java code. Something akin to textbox.text in aspx/.NET. I am finding the documentation very confusing and my attempts don't compile. Is there a way?

    <tr:inputDate id="date" required="true"
          inlineStyle="color:rgb(0,58,117); font-weight:bold;"
          value="#{processScope.benefit.serviceDate}"
          immediate="false"
          onchange="submit();"
          label="#{mb_ResourceBean.res['claim.serviceDate.label']}">

          <tr:convertDateTime pattern="yyyy/MM/dd" secondaryPattern="yyyyMMdd"
                type="date"/>

    <tr:validateDateTimeRange minimum="#{bk_ClaimBean.minDate}"
                        maximum="#{bk_ClaimBean.maxDate}"/>

    </tr:inputDate>

Poor half-attempt to grab input:

     UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
     UIXComponent component = viewRoot.findComponent("date"); //does not compile
cmac
  • 906
  • 10
  • 19

1 Answers1

0

I'm not sure what you are trying to achieve, but since you already have a value binding (#{processScope.benefit.serviceDate}) and you have onchange="submit();" in your <tr:inputDate> it looks like you want to use a valueChangeListener.

You need a method to handle the value change event in your bean, for example:

public void dateChanged(ValueChangeEvent event)
{
  System.out.println("New value: "+ event.getNewValue());
  System.out.println("instanceof Date: "+ (event.getNewValue() instanceof Date));
}

In your jspx you have to add the listener. Also you might want to use autoSubmit="true" instead of onchange="submit();", for example:

<tr:inputDate value="#{myBean.myDate}"
              valueChangeListener="#{myBean.dateChanged}"
              immediate="true" autoSubmit="true"/>

The code in your question does not compile since viewRoot.findComponent() will return a UIComponent. You need to cast it to UIXComponent.

Also, you need to take the naming containers into account. You will need to use something like: viewRoot.findComponent("formId:date");. In this case formId is the id of your <tr:form>.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • I need to pull the date out to insert it into an error message. I inherited some smelly code. As for binding the value to processScope, I have no idea why this was done and fear the unknown implications of changing it. – cmac Feb 28 '13 at 19:15
  • Did you take a look at adding a message bundle? (http://stackoverflow.com/a/15122278/880619) – Jasper de Vries Feb 28 '13 at 19:36
  • You could also check out `RequestContext.getCurrentInstance().getPageFlowScope()`, that'll probably hold the `benefit` object. See also http://myfaces.apache.org/trinidad/trinidad-api/apidocs/org/apache/myfaces/trinidad/context/PageFlowScopeProvider.html – Jasper de Vries Feb 28 '13 at 19:51
  • I am about to add a comment about the message bundle in that post. – cmac Feb 28 '13 at 19:54
  • RequestContext.getCurrentInstance().getPageFlowScope() certainly works, but alas, the value I need is null. – cmac Mar 01 '13 at 17:15