gpt4 book ai didi

java - 将反斜杠替换为斜杠时出现异常

转载 作者:行者123 更新时间:2023-12-02 07:45:19 25 4
gpt4 key购买 nike

在我的应用程序中,我需要为 JSP 页面内的 XML 文件准备一个路径。我正在做这样的事情:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<!DOCTYPE html>
<c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />

但是有一个问题,我收到以下异常:

org.apache.jasper.JasperException:无法编译 JSP 类:

An error occurred at line: 6 in the jsp file: /users.jsp
String literal is not properly closed by a double-quote
3: <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4: <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
5: <!DOCTYPE html>
6: <c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />

显然是关于这部分的:.replace("\\", "/")当我删除它时,我没有收到此异常。

那是关于什么的?我将非常感谢任何线索。

编辑:

我按以下方式使用此变量:

                <c:import url="file:/${abs_path}/MyProject/xml/users.xml" var="inputDoc" charEncoding="UTF-8" />

<c:import url="xsl/users_list.xsl"
var="stylesheet" charEncoding="UTF-8" />

<x:transform
xml = "${inputDoc}"
xslt = "${stylesheet}">
</x:transform>

最佳答案

这不是关于"\\"是正则表达式...因为这不会导致 JSP 编译错误。 (此外,String.replace(String) 的参数不会被解释为正则表达式。)

但是,编译错误似乎确实表明您需要双重转义,我认为原因是 JSP 语法正在“消耗”一级转义本身......在此背景下。

这来自 JSP 2.1 规范:

JSP.1.6 引用和转义约定

...

在属性中引用

Quotation is done consistently regardless of whether the attribute value is a literal or a request-time attribute expression. Quoting can be used in attribute values regardless of whether they are delimited using single or double quotes. It is only required as described below.

  • A is quoted as \’. This is required within a single quote-delimited attribute value.
  • A is quoted as \”. This is required within a double quote-delimited attribute value.
  • A \ is quoted as \\
  • Only when the EL is enabled for a page (see Section JSP.3.3.2, “Deactivating EL Evaluation”), a literal $ can be quoted by \$. Similarly, a literal # can be quoted by \#. This is not required but is useful for quoting EL expressions.
  • A %> is quoted as %\>
  • A <% is quoted as <\%
  • The entities &apos; and &quot; are available to describe single and double quotes.
<小时/>

无论如何,尝试将有问题的代码片段编写为 replace("\\\\", "/") .

关于java - 将反斜杠替换为斜杠时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10965236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com