JSTL 异常expressions

  • A+
所属分类:Java
摘要

在开发JSTL的过程中遇到According to TLD or attribute directive in tag file, attribute value does not accept any expressions怎么样解决,下面提供三种方式来处理不同的情况。

今天在开发JSTL自定义标签的时候出现According to TLD or attribute directive in tag file, attribute value does not accept any expressions,具体的错误和解决办法看下面:

1、具体的错误信息

  1. 九月 062017 6:36:22 下午 org..catalina.core.ApplicationDispatcher invoke
  2. 严重: Servlet.service() for servlet jsp threw exception
  3. org.apache.jasper.JasperException: /WEB-INF/jsp/sys/dict/index.jsp (line: 60, column: 13) According to TLD or attribute directive in tag file, attribute value does not accept any expressions
  4.     at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.:42)
  5.     at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
  6.     at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
  7.     at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1241)
  8.     at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:876)
  9.     at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1553)
  10.     at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392)
  11.     at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2444)
  12.     at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2450)
  13.     at org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
  14.     at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392)
  15.     at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1845)
  16.     at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:218)
  17.     at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
  18.     at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
  19.     at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
  20.     at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
  21.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
  22.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
  23.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
  24.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  25.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  26.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  27.     at org.apache..websocket.server.WsFilter.doFilter(WsFilter.java:52)
  28.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  29.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  30.     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:747)
  31.     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:485)
  32.     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:410)
  33.     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:337)
  34.     at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
  35.     at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
  36.     at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
  37.     at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
  38.     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
  39.     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
  40.     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
  41.     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
  42.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
  43.     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
  44.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  45.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  46.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  47.     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  48.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  49.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  50.     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
  51.     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  52.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  53.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  54.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
  55.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
  56.     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
  57.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
  58.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
  59.     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
  60.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  61.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
  62.     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
  63.     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
  64.     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
  65.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  66.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  67.     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  68.     at java.lang.Thread.run(Thread.java:745)

解决办法共3种

1)最简单的方法主要针对自定义标签,在自定义的TLD文件中添加下面的代码,意思是开启EL表达式,看下面的例子

  1. <tag>
  2.         <!-- 定义标签名 -->
  3.         <name>dict</name>
  4.         <!-- 定义标签处理类 -->
  5.         <tag-class>com.yp.webapp.jsptag.DictTag</tag-class>
  6.         <!-- 定义标签体为空 -->
  7.         <body-content>empty</body-content>
  8.         <attribute>
  9.             <name>type</name>
  10.             <required>true</required>
  11.         </attribute>
  12.         <attribute>
  13.             <name>value</name>
  14.             <required>false</required>
  15.  <!--看这里,表示该value值可以支持EL表达式-->
  16.             <rtexprvalue>true</rtexprvalue>
  17.         </attribute>
  18.         <attribute>
  19.             <name>code</name>
  20.             <required>true</required>
  21.         </attribute>
  22.     </tag>

2)查看源文件,jstl包规定解析的地方主要是在下面的图片中找配置文件,查看该文件会找到标红的地方,我们就从这里为入口,大概就明白了其实就是select该标签开启EL表达式支持,所以我们只要把该表达式修改了就可以。

修改步骤

1)找到导入项目的JSTL包中的META-INF拷贝X.TLD到自己的项目中,建议放在根目录编译后在WEB-INF目录。

2)把下面图中的红色标记中的fase修改为true

3)在web.xml中声明

  1. <jsp-config>
  2.         <taglib>
  3.             <taglib-uri>/test</taglib-uri>
  4.             <taglib-location>/WEB-INF/x.tld</taglib-location>
  5.         </taglib>
  6.     </jsp-config>

4)具体的界面引用例子

  1. <%@ taglib prefix="x" uri="/test" %>

JSTL 异常expressions

3、 如果是核心标签库出现该异常,可以使用下面的方法解决,修改导入的标签库,主要导致的原因是因为使用了JSP2.0版本, 同时又没有使用JSTL core库的备用版本(RT库),所以该方法就是切换为RT库,这里存在2中解决方式

1)在代码中找到该项

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

修改为

  1. <%@  taglib prefix="c"  uri="http://java.sun.com/jstl/core_rt " %>

2)修改web.xml中servlet版本,主要是在2.4版本会存在该问题,所以我们只要把版本修改为2.3就可以解决。

在web.xml中找到该代码

  1. <web-app version="2.4" xmlns="http://java.sun.com//ns/j2ee"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  4. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

修改为

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  2. <web-app>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: