如何使用scriptlet在HTML页面中显示带有换行符的字符串?

时间:2014-11-06 09:57:37

标签: html jsp scriptlet

<% 
String a="abc";
Srting b="xyz";
String c=a+"\n"+b;
%>

我想在HTML表格中显示String c,如下所示:

<table>
  <tr>
    <td><%= c %></td>
  </tr>
</table>

我想得到这个:

--------
| abc  |
| xyz  |
--------

但我明白了:

------------
| abc xyz  |
------------

有没有什么可以用咒语来实现这个?

3 个答案:

答案 0 :(得分:5)

Html有分页符<br>标记。因此,您可以将其插入Java代码而不是\n

String c=a+"<br>"+b;

答案 1 :(得分:1)

尝试使用<br/>代替"\n"

  

字符串c = a + "<br/>" + b;

答案 2 :(得分:1)

从您的设计中,

您可以简单地将其设为,

<table>
  <tr>
    <td><%= a %></td>
  </tr>
  <tr>
    <td><%= b %></td>
  </tr>
</table>

将其显示在行中。在表格中使用<br/>毫无意义。

如果您打算显示多行td

  1. How to show multiline text in a table cell
  2. Width of a <td> is narrower when I use a <br> in it on a fixed width <table>. Why?
  3. 十年来,不鼓励使用scriptlet。请阅读How to avoid Java code in JSP files?

相关问题