我正在通过 request.setAttribute
将 html 字符串从 servlet 发送到 jsp 页面,但是当我尝试在 jsp
上获取它时,它没有正确显示在 View 中。
服务小程序:
String sampleHtml = ""
+ "<table class=\"table table-bordered\">"
+ "<thead>"
+ "<tr>"
+ "<td>Data</td>"
+ "<td>More Data</td>"
+ "</tr>"
+ "</thead>"
+ "<tbody>"
+ "<tr>"
+ "<td>Macdonal</td>"
+ "<td>David</td>"
+ "</tr>"
+ "</tbody>"
+ "</table>";
}
request.setAttribute("userRoles", sampleHtml);
jsp:
<p>
<label>Set Roles</label>
<span class="field">
<%=request.getAttribute("userRoles") %>
</span>
</p>
最佳答案
span 默认是一个内联元素。它不能包含 block 元素。因此,您不能向其中添加表格。考虑将 span 更改为 div。
来自 span docs
The HTML element is a generic inline container for phrasing content, which does not inherently represent anything.
请不要在 servlet 中编写 HTML。使用 JSTL 或 JS。
关于java - request.getAttribute() 在 jsp 中没有显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33733770/