0

We create command links on a page. The same command link is sometimes used on a different page of the web site. When first created because Page A does not have a form.

Scenario 1

Page A (no form on page)

  • Some text
  • Need a link. So, create command link surrounded by a form.

Scenario 2

Page B (reuse commandLink from Page A on page B)

<h:form>
  Some text
  Need a link. So, create command link surrounded by a form.

  Reuse commandlink component from PageA
</h:form>

Throws an error, inner form exception.

  1. Cannot use h:link because of url issues.
  2. Can we use h:outputLink instead of commandLink to avoid form issues?
  3. Can we create a custom jsf component to create commandLink. Check if form exists on page. If yes, do not add form. If no, add form around commandLink.
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
user679526
  • 845
  • 4
  • 16
  • 39

1 Answers1

3

As a rule, do not put h:form tags in your JSF components. The h:form tag should wrap the component in the page you're using it, not in the component itself. That way you'll be able to mix them with other components in the same form providing you much more flexibility, at the little price of having two more lines of code. That's how is done in all the public component libraries, take a look to Primefaces as an example.

Apart from that, bear in mind that h:commandLink fires a POST request to the server, while you seem to be talking about plain links. In general, h:commandLink is used when you don't know the destination url at the time the view renders. I would suggest to avoid its use for any other case, because still you've got h:link for JSF destinations and h:outputLink for external destinations, you could even use a plain HTML anchor element. And, definitely, firing a GET request doesn't require a h:form tag.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217