加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何缩短自定义JSP标签生成的输出?

发布时间:2020-12-14 16:40:35 所属栏目:Java 来源:网络整理
导读:可以使我自己的JSP标签生成的输出更短吗?例如,如下定义的标签生成5行而不是1.可以避免(没有连接所有5行到标签源中的1行)? %@ tag description="link" pageEncoding="UTF-8"%%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %%@ attribute n
可以使我自己的JSP标签生成的输出更短吗?例如,如下定义的标签生成5行而不是1.可以避免(没有连接所有5行到标签源中的1行)?
<%@ tag description="link" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="href" required="true" type="java.lang.String" %>
<%@ attribute name="label" required="false" type="java.lang.String" %>
<a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>

不是解决办法:

<%@ tag description="standard input" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ attribute name="href" required="true" type="java.lang.String" description="address relative to web-app context" %><%@ attribute name="label" required="false" type="java.lang.String" description="link label" %><a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>

解决方法

是的,您可以全局配置JSP解析器来修剪脚本表达式和标记留下的空格.

将其添加到您的webapp的web.xml(它必须是Servlet 2.5兼容!):

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
</jsp-config>

如果您定位到一个Servlet 2.4容器或更低版本,那么您必须编辑容器自己的web.xml,才能在全局应用.例如,在Tomcat中,它是/conf/web.xml文件.搜索< servlet>声明JspServlet,并在< servlet>中添加以下servlet init参数.宣言.

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读