简体   繁体   English

JSTL标记在Javascript调用和HTML元素中返回null / empty

[英]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="&lt;${field['POLL_FIELD_ENTER_QUESTION']}&gt;"
                styleWidth="280px">
            </main:uiInputBox>

Where the tag ${field['POLL_FIELD_ENTER_QUESTION']} should return the string "enter question". 标签${field['POLL_FIELD_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. 我不明白的是,标记通常在JSP文件中返回字符串,但是现在在HTML描述符中时才返回。

Another error is that in javascript if I have a function like this: 另一个错误是在javascript中,如果我有这样的功能:

It prints out the literal string "${field['POLL_FIELD_CHOICE']}" , and not the element that it's representing. 它打印出文字字符串"${field['POLL_FIELD_CHOICE']}" ,而不是它所表示的元素。 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). 至于EL不能在自定义标签中解决的第一个问题,那不是JSTL (是那些<c:xxx>标签)。 That's EL (those ${} things). 那就是EL(那些${}东西)。

You seem to be using EL in a custom tag. 您似乎在自定义标签中使用EL。 The <main:xxx> does not belong to any JSP standard tag library (look, that's what JSTL stands for). <main:xxx>不属于任何JSP标准标记库(看起来,这就是JSTL所代表的)。 To get EL in custom tags to work as well, you need to ensure of the following: 为了使EL在自定义标签中也能正常工作,您需要确保以下几点:

  • The web.xml must be declared as at least Servlet 2.4, which implies JSP 2.0 where this feature is supported. 必须将web.xml 至少声明为Servlet 2.4,这意味着支持此功能的JSP 2.0。

     <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. <main:xxx> .tld库的.tld文件必须至少声明为JSP 2.0,其中支持<rtexprvalue>属性。

     <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). <main:uiInputBox>.tld文件中的defaultValue属性必须标记为<rtexprvalue>true</rtexprvalue>才能启用支持运行时表达式(EL,那些${}东西)。

     <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. 关于EL文件中没有解决EL的第二个问题,解释很简单:模板文本中的EL仅在扩展名为.jsp JSP(2.0或更高版本)文件中运行。 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): .js重命名为.jsp并将以下行添加到页面顶部(最佳解决方案):

     <%@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). 请将那段JS放在JSP页面的内联<script>中(不建议这样做,因为通常认为这是一种不好的做法)。

  • Map *.js on the JSP servlet in web.xml (not recommended, it tight-couples your webapp to the servletcontainer's specific JspServlet which may not necessarily be mapped on the servlet name of jsp ). *.js映射到web.xml的JSP servlet(不建议这样做,它会将您的webapp与servlet容器的特定JspServlet紧密耦合,不一定将其映射到jsp的servlet名称上)。

     <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM