【问题标题】:how to get values from jsp in ${clue.text}如何从 ${clue.text} 中的 jsp 获取值
【发布时间】:2014-11-26 08:29:46
【问题描述】:

我定义了一个 Clue 类:

Class Clue{
  ...
  String text;
  ..
  getter/setter method
}

我想从 jsp 中获取 ${clue.text} 以在 "" 范围内使用以仅显示 subSequence(0, 10),如下所示:

<c:forEach items="${clues}" var = "clue">
    <tr>
    <td>${clue.weibo.user.name}</td>
    <td>
    <%
    String str = ${clue.weibo.text};
    %>
    <%=str.subSequence(0, 10) %>
    </td>
    </tr>
</c:forEach>

我该怎么办?

【问题讨论】:

  • 您上面的代码缺少我担心的问题陈述 - 如果您添加您想要的结果,我们可以提供更好的帮助。
  • 也可以是旧的 Servlet 规范进入 web.xml 并且没有 jsp 解析器。
  • @Smutje 我只想在 ${} 中为 创造价值
  • 这不是问题陈述——问题陈述类似于“我想在方法参数中使用线索.weibo.text 的值”或“我想使用线索的值。 weibo.text 要打印出来”。
  • @Smutje 谢谢,我已经编辑了问题。

标签: java spring jsp


【解决方案1】:

要访问脚本中的 JSTL 变量:

<%
String str = (String)pageContext.getAttribute("clue.weibo.text", PageContext.REQUEST_SCOPE);
%>

不过,更好的方法是使用 JSTL 函数来操作字符串。

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:forEach items="${clues}" var = "clue">
    <tr>
    <td>${clue.weibo.user.name}</td>
    <td>
    ${fn:substring(clue.weibo.text, 0, 10)}
    </td>
    </tr>
</c:forEach>

【讨论】:

  • 感谢您的回答,对我很有帮助
猜你喜欢
  • 1970-01-01
  • 2014-06-14
  • 2016-05-05
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多