#{...} is not allowed in template text

Sanat Pandey picture Sanat Pandey · Sep 18, 2012 · Viewed 23.6k times · Source
<a4j:ajax event="click" render="tempval" listener="#{question.setParameters}" />

When we are using this code, the server throws an exception with the message

#{...} is not allowed in template text

How is this caused and how can I solve it?

Answer

BalusC picture BalusC · Sep 18, 2012

You will get this error when you're using JSP as view technology and you're using #{...} in template text such as (the <p> is just examplary, it can be any plain HTML element):

<p>#{bean.property}</p>

It's namely not supported in JSP, but it is supported in its successor Facelets. In JSP, you would need to explicitly use <h:outputText>:

<p><h:outputText value="#{bean.property}"></p>

However, in your particular code snippet wherein you're using #{...} in a JSF component already, that can only happen if the a4j tag library is not properly been registered as a JSP tag library by <%@ taglib %>, or if the a4j tag library cannot be found in the classpath. This way the <a4j:ajax> tag is not parsed and thus treated as plain text, including all attributes with EL expressions. So the #{question.setParameters} is treated as EL in template text, which is not supported in JSP.

But your problem is bigger: the RichFaces 4.x component library, which the <a4j:ajax> is part of, does not support JSP. JSP is deprecated since JSF 2.0 and succeeded by Facelets. All JSF component libraries such as RichFaces have decided to drop support for JSP, because it's a hell of a lot of work to develop and support tag libraries and components for the two different view technologies JSP and Facelets. So even if you have RichFaces 4.x already in the classpath and you've properly registered it by <%@ taglib %>, it would never work in JSP, simply because the JSP .tld file does not exist for the a4j namespace.

In order to use JSF 2.0 compatible component libraries, you've to migrate from JSP to Facelets. An alternative is to use the older RichFaces 3.x version instead. Version 3.3.3 supports JSF 2.0 on JSP. It offers the <a4j:support> tag to achieve the same. But keep in mind that you're going backwards in technology this way. You should keep moving forwards. Drop JSP and go for its successor Facelets.

See also: