java - 为什么我不能在自定义标记中使用 JSP EL?

标签 java jsp tags expression

当我在标记之外使用 ${} 表示表达式语言时,它可以处理数字或运算等。但是每当我将它作为值放入自定义标记中时,就会出现错误。我在互联网上检查了类似的问题并尝试了他们所有的解决方案,但没有一个起作用。顺便说一句,我只是在遵循tutorialspoint.com 上的教程,我相信我做的一切都是正确的。

这是显示的错误:

type Exception report

message /WEB-INF/view/itr.jsp (line: 72, column: 7) According to TLD or attribute directive in tag file, attribute value does not accept any expressions

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /WEB-INF/view/itr.jsp (line: 72, column: 7) According to TLD or attribute directive in tag file, attribute value does not accept any expressions
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:275)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:107)
    org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1241)
    org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:879)
    org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1536)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:898)
    org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1536)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:464)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1853)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.openmrs.web.filter.JspClassLoaderFilter.doFilter(JspClassLoaderFilter.java:47)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:60)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:72)
    org.openmrs.module.owa.filter.OwaFilter.doFilter(OwaFilter.java:57)
    org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)
    org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:54)
    org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:108)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
    org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
    org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

这些是我的代码:

HelloTag.java

package org.openmrs.web.taglib;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

    private double value;

    public void setValue(double val) {
        this.value = val;
    }

    StringWriter sw = new StringWriter();

    public void doTag() throws JspException, IOException {
        if (value != 0) {
            /* Use message from attribute */
            JspWriter out = getJspContext().getOut();
            out.println( value );
        }
        else {
            /* use message from the body */
            getJspBody().invoke(sw);
            getJspContext().getOut().println(sw.toString());
        }
    }
}

自定义.tld

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>Example TLD</short-name>
  <uri>http://java.sun.com/jsp/jstl/core_rt</uri>
  <tag>
    <name>Hello</name>
    <tag-class>org.openmrs.web.taglib.HelloTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
       <name>value</name>
    </attribute>
  </tag>
</taglib>

itr.jsp

<%@ include file="/WEB-INF/view/module/chitscore/template/include.jsp" %>
<%@ page isELIgnored="false" %>

<h1>Individual Treatment Record</h1>

<h3>Anthropometric Data</h3>
<c:set var="OPENMRS_VIEWING_PATIENT_ID" scope="request" value="${patient.patientId}"/>
<form:form action="anthropometricdata.form" method="POST" modelAttribute="anthropometricdata">
    <table>
        <form:input path="patientId" type="hidden" value="${patient.patientId}"/>
        <tr>
            <td>Height</td>
            <td><form:input path="height" /></td>
        </tr>
        <tr>
            <td>Weight</td>
            <td><form:input path="weight" /></td>
        </tr>
        <tr>
            <td>Body Mass Index</td>
            <td></td>
        </tr>
        <tr>
            <td>Waist Circumference</td>
            <td><form:input path="waistCircumference" /></td>
        </tr>
        <tr>
            <td>Hip Circumference</td>
            <td><form:input path="hipCircumference" /></td>
        </tr>
        <tr>
            <td>Waist-Hip Ratio</td>
            <td></td>
        </tr>
        <tr>
            <td>Head Circumference</td>
            <td><form:input path="headCircumference" /></td>
        </tr>
        <tr>
            <td>Chest Circumference</td>
            <td><form:input path="chestCircumference" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="action" value="Save" />
                <input type="submit" name="action" value="Cancel" />
            </td>
        </tr>
    </table>
</form:form>
<br>
<table border="1">
    <th>Height</th>
    <th>Weight</th>
    <th>Body Mass Index</th>
    <th>Waist Circumference</th>
    <th>Hip Circumference</th>
    <th>Waist-Hip Ratio</th>
    <th>Head Circumference</th>
    <th>Chest Circumference</th>
    <th>test</th>
    <c:forEach items="${anthropometricdataList}" var="anthropometricdata">
        <tr>
            <td>${anthropometricdata.height}</td>
            <td>${anthropometricdata.weight}</td>
            <td>${anthropometricdata.bmi}</td>
            <td>${anthropometricdata.waistCircumference}</td>
            <td>${anthropometricdata.hipCircumference}</td>
            <td>${anthropometricdata.waistHipRatio}</td>
            <td>${anthropometricdata.headCircumference}</td>
            <td>${anthropometricdata.chestCircumference}</td>
            <td><ex:Hello value="${ anthropometricdata.weight }"/>${1 + 2 }</td>
        </tr>
    </c:forEach>
</table>

<%@ include file="/WEB-INF/view/module/chitscore/template/footer.jsp" %>

最佳答案

根据Java EE Tutorial ,您必须将标记的 rtexprvalue 设置为 true。即

<attribute>
   <name>value</name>
   <rtexprvalue>true</rtexprvalue>
</attribute>

关于java - 为什么我不能在自定义标记中使用 JSP EL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261809/

相关文章:

javascript - 在 HTML 区域标记中使用图像

java - HTTP 状态 404 : The requested resource is not available

Java Socket 数据包读取重叠

java - 如何使用 Or 和 Not java 正则表达式运算符

java - 除了 Apache Math Mean 之外,深层含义是什么?请参阅代码

java - 在 JSP 中编辑数据表

java - 将属性从 session 复制到请求范围

javascript - 未捕获错误 : Syntax error, 无法识别的表达式:不支持的伪:标签

vim EasyTags,每个文件类型标签命令?

php - PhpQuery 是如何工作的?试图获取 &lt;title&gt; 标签的值