Is it possible to reuse a form in two different views in JSF? I know about the ui:include
but how would you do it with the action on the submit button for example? Or do I need to duplicate the forms?
Asked
Active
Viewed 1,986 times
2
-
Sure, I use the same form in a bunch of different views. But I dont really understand your question. Can you expand on your concern about the action of the submit button? The submit button is part of the form itself, so how would that be effected by the view it is contained in (other than possibly a different id due to naming container)? – Lucas Nov 20 '11 at 18:45
2 Answers
5
When using <ui:include>
you can parameterize the bean and/or the method by <ui:param>
.
<ui:include src="...">
<ui:param name="bean" value="#{someBeanName}" />
<ui:param name="action" value="someMethodName" />
</ui:include>
with
<h:commandButton ... action="#{someBeanName[someMethodName]}" />
You can also make it a Facelets tag file instead so that you end up with something like
<my:someForm bean="#{someBeanName}" action="someMethodName" />
A composite component is also doable, but IMO not really the right approach for this purpose.
See also:
-
I am reading on how to make composite components now. What would you do? Used the ui:include with parameters as you described? – LuckyLuke Nov 21 '11 at 11:52
-
Depends really on the functional purpose of that form and what you would like to do on those 2 cases. For example, is it one for data input and other for data editing? If so, `ui:include` would make more sense. Or is it kind of a confirmation form which you *can* basically reuse on *every* other page/form? If so, a tag file would make more sense. If it had to represent for example a single input field, a composite component would make more sense, which clearly isn't the case if it's already a whole form. – BalusC Nov 21 '11 at 11:59
-
This form will be used for both editing and adding questions on the admin side and also for adding new questions on the public/client side. – LuckyLuke Nov 21 '11 at 12:03
-
And may I ask what the tag file is (I did try to search, did not find any correct sources for further reading)? I have not heard or read anything about that yet, I think. – LuckyLuke Nov 21 '11 at 12:25
-
I'd just start off with an include. As to tag files, check http://stackoverflow.com/questions/5713718/how-to-make-a-grid-of-jsf-composite-component/5716633#5716633 – BalusC Nov 21 '11 at 12:28
-
Thank you, your help is much appriciated:) I forgot one thing though, if the form will have different backing beans (since the form will be used for both admin and the public side) then how would I do it? Should I make a base class that both beans inherit from so the property I use in the form is available in the form? – LuckyLuke Nov 21 '11 at 12:43
-
A base class is only useful if the beans share common properties which is I think indeed true in this case. – BalusC Nov 22 '11 at 13:37
1
You want to use composite components. Those components can be parameterized so that you could exchange the backing bean, etc..
IBM has some nice guides on that topic.

Udo Held
- 12,314
- 11
- 67
- 93