Since commandLink launch an action, you have to add a h:form
<h:form>
<h:commandLink action="#{welcomeBean.logOutSession}" class="subAnchor" value="Log Out"/>
</h:form>
Question
I have Added My commandLink (which is the logout link) into the template File which is not inside a jsf form.
MainTemplate.xhtml
<h:commandLink action="#{welcomeBean.logOutSession}" class="subAnchor" value="Log Out">
</h:commandLink>
on the webpage that uses the template, i have all the elements inside a jsf form:
NewWelcome.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/webpages/templates/MainTemplate.xhtml">
<ui:define name="infomationPartOfBody">
<h:form>
<div> Here i have all the page specific content </div>
</h:form>
</ui:define>
</ui:composition>
I was hoping that the logout would work once i use the template in a page with a jsf form. However, the logout commandLink shows error :
Log Out: This link is disabled as it is not nested within a JSF form.
I know a clear solution is to add this element separately into each of the xhtml pages that would use the template. But i want it to be added into the template itself as it is a common element in all the pages.
Any help would be deeply appreciated. Thank You! :)
Editing: here is the code of my MainTemplate.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view>
<h:head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/cupertino/jquery-ui.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="../JavaScriptPages/JQueryFile.js"></script>
<title>State Transport Department- Work Schedule</title>
<link rel="stylesheet" type="text/css" href="../CSS/CompleteTemplateCSS.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/templateCSS.css"/>
</h:head>
<h:body>
<div class="container">
<div class="header">
<h:graphicImage id="img" value="http://s28.postimg.org/ksnr9zs5p/header.jpg" class="img" ></h:graphicImage>
</div>
<div class="menu">
<h:outputLabel class="welcomeNote" style="font-size: x-large; color: white; float: left;
margin: 8px 0 0 5px; text-shadow:0 0 3px white" value="#{welcomeBean.fullname}" />
<ul class="ulForMyAccount">
<li> <h:outputLabel value="My Account" id="mainAnchor"/>
<ul >
<li> <h:link value="Change my Password" outcome="/webpages/ChangePasswordxhtml.xhtml" rendered="true" class="subAnchor"/> </li>
<li><h:commandLink action="#{welcomeBean.logOutSession}" class="subAnchor" value="Log Out">
</h:commandLink> </li>
</ul>
</li>
</ul>
</div>
<div class="contentBody">
<div class="menuTable">
<table class="templateBody" >
<tr>
<td class="navigationLink" > <ul><li>
<h:link value="Home" outcome="/webpages/NewWelcome.xhtml" rendered="#{welcomeBean.home}" class="mainLinks"/>
</li></ul> </td>
</tr>
<tr>
<td class="navigationLink"> <ul> <li>
<h:link value="My Schedule" outcome="/webpages/MyTask.xhtml" rendered="#{welcomeBean.myTask}" class="mainLinks"/>
</li></ul></td>
</tr>
<tr>
<td class="navigationLink"> <ul> <li>
<h:link value="Employee Work Schedule" outcome="/webpages/EmpDutySched.xhtml" rendered="#{welcomeBean.workSchedule}" class="mainLinks"/>
</li></ul></td>
</tr>
<tr>
<td class="navigationLink"><ul> <li>
<h:link value="Allocate Work" outcome="/webpages/AllocateTask.xhtml" rendered="#{welcomeBean.allocateWork}" class="mainLinks"/>
</li></ul></td>
</tr>
<tr>
<td class="navigationLink"><ul> <li>
<h:outputLabel value="Report" rendered="#{welcomeBean.report}" class="reportMenu"/>
</li></ul>
<ul>
<li><h:link value="Project Wise Report" outcome="/webpages/ProjectWiseReport.xhtml" rendered="true" class="reportItems"/></li>
<li><h:link value="Employee Wise Report" outcome="/webpages/EmployeeWiseReport.xhtml" rendered="true" class="reportItems"/></li>
</ul>
</td>
</tr>
</table>
</div>
<ui:insert name="infomationPartOfBody">
</ui:insert>
</div>
<div class="footer"></div>
</div>
</h:body>
</f:view>
</html>
Solution
Since commandLink launch an action, you have to add a h:form
<h:form>
<h:commandLink action="#{welcomeBean.logOutSession}" class="subAnchor" value="Log Out"/>
</h:form>