JSTL tags return null/empty in Javascript calls and HTML elements
This is a really confusing error, as it crops up in some of the webpages that I'm creating, but not in others, although syntactically the elements are identical.
For example, this doesn't show up:
<main:uiInputBox
onDarkBG="${hasDarkBG}"
name="questionTitle1"
onblur="pollUpdateQuestion(false, false, true, this);"
defaultValue="<${field['POLL_FIELD_ENTER_QUESTION']}>"
styleWidth="280px">
</main:uiInputBox>
Where the tag ${field['POLL_FIELD_ENTER_QUESTION']}
should return the string "enter question". What I don't understand is that the tag returns the string normally in the JSP file, but now when it's in the HTML descriptor.
Another error is that in javascript if I have a function like this:
It prints out the literal string "${field['POLL_FIELD开发者_开发知识库_CHOICE']}"
, and not the element that it's representing. Ex:
template.find('h2').text('${field["POLL_FIELD_CHOICE"]} ');
What am I doing wrong here and how can I fix it?
As to the first problem of EL not being resolved in a custom tag, that's not JSTL (it are those <c:xxx>
tags). That's EL (those ${}
things).
You seem to be using EL in a custom tag. The <main:xxx>
does not belong to any JSP standard tag library (look, that's what JSTL stands for). To get EL in custom tags to work as well, you need to ensure of the following:
The
web.xml
must be declared as at least Servlet 2.4, which implies JSP 2.0 where this feature is supported.<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
The
.tld
file of the<main:xxx>
taglib must be declared as at least JSP 2.0, where the<rtexprvalue>
attribute is supported.<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
The
defaultValue
attribute in the.tld
file of the<main:uiInputBox>
must be marked with<rtexprvalue>true</rtexprvalue>
to enable the support runtime expressions (the EL, those${}
things).<attribute> <name>defaultValue</name> <rtexprvalue>true</rtexprvalue> </attribute>
As to the second problem of EL not being resolved in a JavaScript file, well, the explanation is pretty simple: EL in template text like that runs in JSP (2.0 or newer) files with .jsp
extension only. There are several ways to get it to work anyway:
Rename
.js
to.jsp
and add the following line to top of the page (best solution):<%@page contentType="text/javascript" %>
Put that piece of JS in an inline
<script>
of the JSP page instead (not recommend since that's generally seen as a poor practice).Map
*.js
on the JSP servlet inweb.xml
(not recommended, it tight-couples your webapp to the servletcontainer's specific JspServlet which may not necessarily be mapped on the servlet name ofjsp
).<servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>
精彩评论