1

I am working on a web-application in jsf. My question is regarding the working of <h:commandLink>.

I tried using <h:commandLink> for doing the stuff which I can do with <h:outputLink>, just for testing.(I know it is a bad idea to use <h:commandLink> for static navigation).

My <h:commandLink> is present in a file called CustomerRight.jsp.

So in my file CustomerRight.jsp, I have the code as shown below:

<f:view>
<h:commandLink action="/pages/accountSettings/ToggleMessageService.jsp" value="Click here"></h:commandLink>
</f:view>

Now this file is included in another jsp called Home.jsp as shown below:

<f:view>
   <h:form>
      <jsp:include page="CustomerRight.jsp"></jsp:include>
   </h:form>
</f:view>

Now as we can see when the page is included in Home.jsp, it will act as a nested <f:view> tag. Because of this, the <h:commandLink> is not working. It is re-deirecting me to the same page, whereas if I use a <h:outputLink> instead of it, that is working fine.

Can anyone explain me why the <h:commandLink> is not working but the <h:outputLink> is working in case of nested <f:view> tag?

If I remove the <f:view> tag from the CustomerRight.jsp, the commandLink works fine (since there is no nested <f:view> tag now)

Also what is the significance of <f:view> tag? If I don't use it at all in any of the jsps, the page is not rendered. I get an exception saying "java.lang.IllegalStateException: Component javax.faces.component.UIViewRoot@1226eca not expected type. Expected: javax.faces.component.UIOutput. Perhaps you're missing a tag?"

Mr Lister
  • 42,557
  • 14
  • 95
  • 136
A_J
  • 907
  • 2
  • 11
  • 37

1 Answers1

1

First of all, you're using legacy JSP view technology which was deprecated and succeeded by Facelets (XHTML) since JSF 2.0 in 2009. If you're just starting with JSF, make sure that you don't look at age-old learning resources. Namely, the <f:view> behaves differently in Facelets, so if you're researching for answers, any answer targeted at Facelets doesn't apply to JSP and may end up to confuse you.

As to the concrete problem, in JSP there can be only one <f:view> and it must be the top level JSF component. It represents the UIViewRoot instance. You can't nest multiple <f:view> in JSP. In JSP's successor Facelets it is optional, and doesn't need to be top level one, and there can be multiple, basically extending each other.

You shouldn't have a <f:view> in the include file. You can at most have a <f:subview> in the include file if you intend to reuse the same include file multiple times, otherwise duplicate component ID errors will occur for the included content.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thanks for the answer and the suggestions! But my question is still not answered completely. Why is outputLink working in case of nested – A_J Dec 15 '15 at 09:38
  • 1
    Because it generates a plain GET link and doesn't require being nested in a h:form. With the inner f:view you're basically destroying anything outside it (because in JSP there can be only one), so h:commandLink can't find any parent h:form in order to perform the POST request. – BalusC Dec 15 '15 at 09:40
  • Thanks for the clarification! – A_J Dec 15 '15 at 09:43