HTML 未通过验证

发布于 2024-12-23 10:45:44 字数 386 浏览 0 评论 0原文

下面的行没有在我的应用程序中通过验证。 Netbeans 中的错误是...

元素“a”上的属性 href 的错误值“/content/edit”:PATH 中的 WHITESPACE

    <a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

运行时错误是:

org.apache.jasper。 JasperException: /base.jsp(9,25) PWC6213: 需要引号

我正在传递该值的属性。为什么我在传递值时会收到此错误?

The below line is not passing validation in my application. The error is in Netbeans is...

Bad value " /content/edit" for attribute href on element "a": WHITESPACE in PATH

    <a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

The runtime error is:

org.apache.jasper.JasperException: /base.jsp(9,25) PWC6213: quote symbol expected

I am passing an attribute for this value. Why am I getting this error when I pass a value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-12-30 10:45:44

不要在 JSP 中使用 scriptlet。使用 JSP EL:

<a href="${urlPrefix}/content/edit">Add Content</a>

Don't use scriptlets in JSP. Use the JSP EL:

<a href="${urlPrefix}/content/edit">Add Content</a>
遗心遗梦遗幸福 2024-12-30 10:45:44
<a href="<%=request.getAttribute('urlPrefix')%>/content/edit">Add Content</a>

urlPrefix 中使用单引号。它应该有效。

<a href="<%=request.getAttribute('urlPrefix')%>/content/edit">Add Content</a>

Use single quotes with urlPrefix. It should work.

她说她爱他 2024-12-30 10:45:44

试试这个:

<% String urlPrefix = (String)request.getAttribute("urlPrefix"); %>
<a href="<%=urlPrefix%>/content/edit">Add Content</a>

或者更好这个:

<%
String urlPrefix = (String)request.getAttribute("urlPrefix");
String url = urlPrefix + "/content/edit";
%>
<a href="<%=url%>">Add Content</a>

或者更好地使用 EL:

<a href="${urlPrefix}/content/edit">Add Content</a>

值得一提的是针对 XSS 攻击 作为 Asaph 在他的评论中指出:

<a href="${fn:escapeXml(urlPrefix)}/content/edit">Add Content</a>

可能会成功。

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

如果您将其包含在 JSP 的顶部,

Try this:

<% String urlPrefix = (String)request.getAttribute("urlPrefix"); %>
<a href="<%=urlPrefix%>/content/edit">Add Content</a>

or better this:

<%
String urlPrefix = (String)request.getAttribute("urlPrefix");
String url = urlPrefix + "/content/edit";
%>
<a href="<%=url%>">Add Content</a>

or even better use EL:

<a href="${urlPrefix}/content/edit">Add Content</a>

It's worth mentioning the protection against XSS attacks as Asaph pointed out in his comment:

<a href="${fn:escapeXml(urlPrefix)}/content/edit">Add Content</a>

might do the trick if you include

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

at the top of your JSP.

吝吻 2024-12-30 10:45:44

我刚刚做了一个简单的测试,下面的行没有语法错误,并且无论是否设置了 urlPrefix 属性,运行时都不会抛出异常:

<a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

根本没有语法错误。在没有设置 urlPrefix 属性的情况下,生成的 html 为:

<a href="null/content/edit">Add Content</a>

如果 urlPrefix 等于 http://example.com,生成的 html 是:

<a href="http://example.com/content/edit">Add Content</a>

这是一个快速的小型独立 test.jsp 文件来演示:

<% request.setAttribute("urlPrefix", "http://example.com"); %>
<a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

您可以删除第一行来测试 null 情况。

因此,我们已经证明您发布的涉嫌违规行实际上并没有问题。一些可能性:

  1. 您确定您正在查看正确的线路吗?
  2. 您确定您正在查看正确的文件吗?
  3. 您确定已经部署了您的应用程序吗?
  4. 您确定正在查看正确的网址/环境吗?

I've just done a simple test and the following line has no syntax error and runs without throwing an exception whether the urlPrefix attribute is set or not:

<a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

There is no syntax error at all. In the case of there being no urlPrefix attribute set, the resulting html is:

<a href="null/content/edit">Add Content</a>

In the case of urlPrefix being equal to http://example.com, the resulting html is:

<a href="http://example.com/content/edit">Add Content</a>

Here is a quick little standalone test.jsp file to demonstrate:

<% request.setAttribute("urlPrefix", "http://example.com"); %>
<a href="<%=request.getAttribute("urlPrefix")%>/content/edit">Add Content</a>

You can remove the first line to test the null case.

So we've demonstrated that the line you posted as the alleged offending line is not actually problematic. Some possibilities:

  1. Are you sure you're looking at the correct line?
  2. Are you sure you're looking at the correct file?
  3. Are you sure you've deployed your application?
  4. Are you sure you're looking at the correct url/environment?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文