【问题标题】:unable to print values of custom jsp tag <% %>无法打印自定义 jsp 标签 <% %> 的值
【发布时间】:2015-08-25 17:28:20
【问题描述】:

在 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}" />

我怎样才能做到这一点

【问题讨论】:

  • 你得到什么输出?你收到${x} 了吗?
  • 无(空白页)
  • 在使用我在下面发布的解决方案后,如果您仍然遇到同样的问题,请告诉我
  • x 是一个脚本变量。为了将其用作作用域变量,您必须将其放入作用域中。
  • 这是一种方法。

标签: jsp tags xhtml


【解决方案1】:

如果你想在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

【讨论】:

  • 感谢它为我工作
猜你喜欢
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 2011-03-09
相关资源
最近更新 更多