13
<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?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Sanat Pandey
  • 3,861
  • 15
  • 65
  • 122

4 Answers4

11

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

I faced the same problem, for me the cause of the error was a commented line in javascript that use #{...} to assign value to a field in my page. once I removed it worked fine, sounds weird but this is what happened.

user1512999
  • 227
  • 2
  • 5
  • 19
  • this was my case too - although I didn't want to delete the HTML commented out line - so e.g. instead of deleting – hello_earth Aug 12 '20 at 14:53
0

if you are using jsp as a view technology you need to import the below two libraries.

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

if you using xhtml add the below in html tag like

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
...
</html>
Dmitriy
  • 5,347
  • 12
  • 23
  • 37
Tulu
  • 11
  • 1
    Sorry, this is not a correct answer. The namespaces you import have no relation whatsoever with the error of the OP. – Kukeltje Sep 12 '18 at 11:03
0

just remove the Xmlns from jsp page

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

and now add tag lib for jsp page..

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

It will definitely solve your problem.