如何在 JSP 标签后删除换行符?

Art*_*hur 5 java jsp spring-mvc

如何在 JSP 标签后删除换行符?

例如,如果在 JSP 页面中有这样的标记代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${pageTitle}</title>
    </head>
Run Code Online (Sandbox Code Playgroud)

比输出结果将在第一行换行。

!HERE_LINE_BREAK!
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
Run Code Online (Sandbox Code Playgroud)

如何在第一行删除不需要的换行符?

使用 Spring Web MVC 处理的 JSP。

谢谢。

Art*_*hur 4

从从 jsp 输出中去除空格复制 有一个 trimWhiteSpaces 指令可以完成此操作,

在您的 JSP 中:

<%@ page trimDirectiveWhitespaces="true" %>
Run Code Online (Sandbox Code Playgroud)

或者在 web.xml 的 jsp-config 部分中(请注意,这从 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>
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果您有所需的空间,则可能还需要删除该空间,因此您可能需要在某些位置提供不间断的空间。