jstl <c:set> with target属性

时间:2018-04-28 21:01:44

标签: jsp jstl

我从“jstl”开始,但我已经对某些基本的东西感到震惊,比如&lt; c:set&gt;,所以我想知道我是否缺少一些基本概念或者其他。< / p>

我已阅读规范

Iterate

因此我做了以下.jsp

<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><body>

<%
    Map<String,Integer> mymap = new HashMap<String,Integer>();
    mymap.put("name",new Integer(1));
    out.println(mymap.get("name"));
%>

<c:set value="new Integer(3)" target="mymap" property="name"/>

<% 
    out.println(mymap.get("name"));
%>


</body></html>

我期待这个输出

1
3

相反,我得到一个例外

HTTP Status 500 – Internal Server Error

Type Exception Report

Message An exception occurred processing [/page1.jsp] at line [12]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/page1.jsp] at line [12]

9: %>
10: 
11: <c:set value="new Integer(3)" target="mymap" property="name"/>
12: 
13: <% 
14:     out.println(mymap.get("name"));
15: %>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:467)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  "name"
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:667)
    org.apache.jsp.page1_jsp._jspService(page1_jsp.java:161)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  "name"
    org.apache.taglibs.standard.tag.common.core.SetSupport.exportToBeanProperty(SetSupport.java:244)
    org.apache.taglibs.standard.tag.common.core.SetSupport.doEndTag(SetSupport.java:96)
    org.apache.jsp.page1_jsp._jspx_meth_c_005fset_005f0(page1_jsp.java:186)
    org.apache.jsp.page1_jsp._jspService(page1_jsp.java:139)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.7
我在做错了什么? 地图存在,我定位它的引用变量,它有一个键/值对与键“名称”... 为什么它在&lt; set&gt;:“name”中说无效的属性?

1 个答案:

答案 0 :(得分:0)

没有什么要观察的。

首先,如this another question所示,scriplet(<% ... %>之间的Java代码)不与表达式语言(EL,即应位于${ .. }之间的代码)共享上下文JSTL标记的参数),因此您需要在请求,页面或其他相关上下文中保持共享变量。

此外,内插是使用字符串文字进行的,然后为了有效使用数字,我建议使用formatNumber tag

请记住,混合使用这些方法是一种不好的做法,下面的解决方案几乎是最小的更改,以使代码“有效”。为了继续学习,我希望您将Java逻辑移植到servlet内的业务层中,并希望保持这种体系结构。

<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>

    <%
        Map<String, Integer> mymap = new HashMap<String, Integer>();
        mymap.put("name", new Integer(1));
        out.println(mymap.get("name"));
        pageContext.setAttribute("mymap", mymap);
    %>

    <c:set value="3" target="${mymap}" property="name" />

    <%
        out.println(mymap.get("name"));
    %>

</body>
</html>