1

I'm at the end of my rope with this one. I'm new to JSF so this is probably my misunderstanding of a lot of stuff.

<ui:composition>
    <f:view>
         <tr:form>
             <ui:fragment rendered="#{param['type'] eq 'myType'}">
                <ui:include src="/home/myPage.jspx" />
            </ui:fragment>
        ......

I pass the page a certain type, it display's certain fields/criteria for a form and a bean backs it all because there is a single search.

Within myPage.jspx I have:

action="#{MyBean.submitForm}"

does not work, although a onsubmit="alert('hi');" does work as an attribute of the form element.

I guess what's most confusing is that

valueChangeListener="#{MyBean.stateChanged}"

does work on a field in the myPage.jspx

Why does the action (attribute of a button) not work?

Justin
  • 859
  • 4
  • 15
  • 30

1 Answers1

1

During processing of the form submit, if the button or one of its parent components is not rendered, then the button's action won't be invoked. You need to make sure that the rendered attribute evaluates the same during processing of the form submit as it did when the form was displayed. In your case, you're depending on the value of a request parameter with the name type.

In this particular case, you could solve the problem by retaining the request parameter type by a <f:param> nested in the command button:

<h:commandButton ...>
    <f:param name="type" value="#{param.type}" />
</h:commandButton>

Or, if you're using JSF 2.0, placing the bean in the view scope and setting the type parameter in the managed bean by <f:viewParam> can also solve it.

<f:metadata>
    <f:viewParam name="type" value="#{bean.type}" />
</f:metadata>

...

<ui:fragment rendered="#{bean.type eq 'myType'}">

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You were certainly spot on with the reason it was failing. The parameter is being lost. I tried doing the f:param option but that didn't work, said the Servlet couldn't set the param (can't remember exact error). What I eventually did was set the param on the backing bean in the beans constructor: 'HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); type = req.getParameter("myType");' then I just edited the #{} to check the property. Thanks also for the url I wish my searching had turned that up a day ago! – Justin Feb 08 '12 at 15:09
  • 1
    You're welcome. By the way, `ExternalContext` has a `getRequestParameterMap()` method which gives you the parameter map back without the need to have any single line of `javax.servlet` import in your JSF backing bean (which is a sign of code smell). – BalusC Feb 08 '12 at 15:10