避免 JSTL c:set 语句中出现空格
我有一个 JSP 文件,它生成一个 LI 列表,其中列表中的第一个和最后一个项目获得分配给它们的特殊类。我目前使用以下位:
<c:set var="liclass">
<c:if test="${rowStatus.first}">first</c:if>
<c:if test="${rowStatus.last}"> last</c:if>
</c:set>
<%-- not very pretty --%>
<li<c:if test="${not empty liclass}"> class="${liclass}"</c:if>>
这种情况下的问题是,在只有一个结果的情况下,该类应该成为“第一个最后一个”(有效),但它变成 first [... ] 最后
,其中 [...] 表示一堆被过滤掉的空格。
似乎
也采用了由使用的缩进引起的空格。我可以通过不带空格地键入它来解决它:
<c:set var="liclass"><c:if test="${rowStatus.first}">first</c:if><c:if test="${rowStatus.last}"> last</c:if></c:set>
但我更喜欢可读的变体。另一种选择是通过删除多余空格的函数来提取结果。
问题:是否有一种方法或技术可以避免在
标签中设置这样的空格?
I have a JSP file that generates a list of LIs, where the first and the last item in the list get a special class assigned to them. I currently use the following bit for that:
<c:set var="liclass">
<c:if test="${rowStatus.first}">first</c:if>
<c:if test="${rowStatus.last}"> last</c:if>
</c:set>
<%-- not very pretty --%>
<li<c:if test="${not empty liclass}"> class="${liclass}"</c:if>>
The problem in this case is that, in the case when there's only one result, the class should become 'first last' (which works), but it becomes first [...] last
, where [...] represents a bunch of whitespace that SO filters away.
It seems that <c:set>
also takes the whitespace caused by the indentation used. I could just solve it by typing it without whitespace:
<c:set var="liclass"><c:if test="${rowStatus.first}">first</c:if><c:if test="${rowStatus.last}"> last</c:if></c:set>
But I'd prefer the readable variant. Another alternative is to pull the result through a function that removes excess whitespace.
Question: Is there an approach or technique to avoid setting whitespace like this in a <c:set>
-tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在条件运算符
?:
的帮助下直接在value
属性中完成此操作。对于不漂亮的
部分,我只需添加
并执行
True,它为非第一个/最后一个元素添加了一个看似毫无价值的
class="none"
但谁在乎呢?至于具体问题,您可以通过将
@page
的trimDirectiveWhitespaces
属性设置为true
来修剪标签库留下的空白。(仅适用于 Servlet 2.5/JSP 2.1 容器)
您还可以在 servlet 容器级别配置它。由于不清楚您使用的是哪一个,这里只是一个 Apache Tomcat 示例:在
Tomcat/conf/web.xml
的 JSP servlet 条目中添加/编辑以下初始化参数:无论哪种方式,我都可以不能从头顶看出,也不能保证它会达到以
first last
结束的预期效果。你必须亲自尝试一下。I'd do it straight in
value
attribute with help of the conditional operator?:
.For the not pretty
<li>
part, I'd just addand do
True, it adds a seemingly worthless
class="none"
for non-first/last elements, but who cares?As to the concrete question, you can trim whitespace left by taglibs by setting the
trimDirectiveWhitespaces
attribute of the@page
totrue
.(works in Servlet 2.5/JSP 2.1 containers only)
You can also configure it at servletcontainer level. Since it's unclear which one you're using, here's just an Apache Tomcat example: in the JSP servlet entry in
Tomcat/conf/web.xml
add/edit the following initialization parameter:Either way, I can't tell from top of head nor guarantee that it would achieve the desired effect of ending up with
first last
. You would have to give it a try yourself.