无法打印自定义 jsp 标记 <% %> 的值

unable to print values of custom jsp tag <% %>

我在 Jsp 自定义标签中打印变量时遇到问题。使用下面的代码时,c:out 不会打印任何内容,而当尝试使用 c:out 中的默认属性时,它会打印默认值,这意味着变量为 null 它不在这里是我的代码。

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <% int x = 1;%>
 <c:out value="${x}" />

我怎样才能使这个工作

如果要在 scriptlet 标签中打印声明的变量,请使用 c:out 标签,那么你可以按照下面提到的方式来做

在变量名下的页面上下文中设置变量并使用 EL 表达式计算值

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
</head>
<body>
<%
   int x = 1;
  //set variable x in the page context under the variable name "var_x"
  pageContext.setAttribute("var_x",x);
%>
<c:out value="${var_x}" />
</body>
</html>

更多详情可以查看本教程The most commonly used JSTL tag which is used to display the result of the expressions