JSTL的错误“attribute test does not accept any expressions”解决方法

本文解决了使用JSTL时遇到的attributetestdoesnotacceptanyexpressions错误,提供了两种解决方案:一是更改taglib指令中的URI,二是使用JSTL的备用库。
前几天用JSTL碰到这个很郁闷的问题: attribute test does not accept any expressions

找了点文章明白了是JSP版本与JSTL的兼容问题

解决方法有2个

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

2、使用JSTL的备用库,
       将<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
       更改为
        <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
   
当 `<c:set var="inx_price_${statusPage}"` 报错,提示属性 `var` 不接受任何表达式时,可采用以下方法解决: #### 1. 使用脚本表达式进行拼接 可以在 JSP 脚本中拼接变量名,然后将其赋值给 `var` 属性。示例如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String statusPage = (String) request.getAttribute("statusPage"); String varName = "inx_price_" + statusPage; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSTL c:set Example</title> </head> <body> <c:set var="<%=varName%>" value="100" /> <p>Value of <%=varName%>: <c:out value="${<%=varName%>}" /></p> </body> </html> ``` #### 2. 通过 Java 代码动态设置 在 Servlet 或 JavaBean 中动态生成变量名,然后将其存于请求属性,再在 JSP 中使用。示例如下: ```java // 在 Servlet 中 import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/example") public class ExampleServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String statusPage = "1"; String varName = "inx_price_" + statusPage; request.setAttribute("varName", varName); request.getRequestDispatcher("/example.jsp").forward(request, response); } } ``` ```jsp <!-- example.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSTL c:set Example</title> </head> <body> <c:set var="${varName}" value="100" /> <p>Value of ${varName}: <c:out value="${varName}" /></p> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值