35

Have JSF 1.2 two pages(one.xhtml and other.xhtml),
that are included to the current page by following rule:

...
    <c:if test="#{flowScope.Bean.param1}">
        <ui:include src="one.xhtml"/>
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}">
        <ui:include src="other.xhtml"/>
    </c:if> 
...

As far one.xhtml differs from other.xhtml only by action parameters:

one.xhtml:<h:commandLink action="actionOne">
other.xhtml:<h:commandLink action="actionTwo">

Is it possible to use some general xhtml?
Instead of one.xhtml and other.xhtml,something like this:

...
    <c:if test="#{flowScope.Bean.param1}">
        <ui:include src="general.xhtml" param="actionOne"/>
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}">
        <ui:include src="general.xhtml" param="actionTwo"/>
    </c:if> 
...

thank you for help.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
sergionni
  • 13,290
  • 42
  • 132
  • 189

2 Answers2

67

You need to nest <ui:param> inside <ui:include> to pass parameters to the included file.

<ui:include src="general.xhtml">
    <ui:param name="action" value="actionOne" />
</ui:include>

and in the include:

<h:commandButton action="#{action}" />

Note that this only supports strings, not action methods. For the latter you would need to upgrade to JSF 2.0 and use composite components.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks, i khow about cc, they are great, but in current jsf 1.2 project can't use them.I will try your solution and write back about results. – sergionni Mar 13 '11 at 17:53
  • It will work based on the way as you presented in the question. But if you were using `#{bean.doSomething}` instead of `actionOne`, you would really need to grab JSF 2.0 composite components. – BalusC Mar 13 '11 at 18:04
  • strange, it throws: action="#{action}": Identity 'action' was null and was unable to invoke – sergionni Mar 13 '11 at 18:06
  • 2
    As a note, param works with whole objects in addition to Strings. – Adam Aug 08 '11 at 22:37
  • 2
    @Adam: when using ``, yes. This is no secret. But this does not apply on `` which will always be converted to `String`. Perhaps your initial confusion was based on this. – BalusC Aug 08 '11 at 22:39
  • 2
    @BalusC: I was just looking to elaborate for future reference. Thanks for the note on `f:param` and its comparison to `ui:param`. I'd never used the `f` library version and will keep the difference in mind. – Adam Aug 10 '11 at 17:40
22

In addition to BalusC's answer:

Note that this only supports strings, not action methods. For the latter you would need to upgrade to JSF 2.0 and use composite components.

There is a way to do this with JSF 1.2, though it's somewhat ugly:

<ui:include src="general.xhtml">
    <ui:param name="actionBean" value="#{myBackingBean}" />
    <ui:param name="actionMethod" value="edit" />
</ui:include>

and

<h:commandButton action="#{actionBean[actionMethod]}" />
meriton
  • 68,356
  • 14
  • 108
  • 175