我正在这样做:
<a href="http%3A%2F%2Flocalhost%3A8080%2Fnews.xhtml%3Fid%3D32%26lang%3Den" target="_blank" />
渲染后,链接指向:http://localhost:8080/news.xhtml?id=32&lang=en
我需要此链接指向编码后的网址,而不是解码后的网址。
有人知道如何逃脱吗?
更新(根据评论):我需要它来实现在 Facebook 上分享此功能。方法是调用以下链接:http://facebook.com/sharer.php?u=<encoded url to share>
最佳答案
为什么需要这个?然后,该链接将在技术上被破坏。
无论如何,您基本上只需要将百分号 %
替换为其 URL 编码表示 %25
(换句话说:只需对 URL 进行两次编码)。
因此,您的特定情况的结果将是:
http%253A%252F%252Flocalhost%253A8080%252Fnews.xhtml%253Fid%253D32%2526lang%253Den
Update: as you're asking this in JSF context, here's the way how you would normally create links in JSF (which are thus by default already URL-encoded):
<h:outputLink value="#{bean.url}" />
如果你想对其进行两次编码,则必须获取 JSTL 的 c:url
:
<c:url value="#{bean.url}" var="url" />
<h:outputLink value="#{url}" />
更新 2:根据评论,实际要求现在完全清楚了,正常的 JSF 方式就可以了(请注意,您不一定需要对其进行两次编码在这里!):
<h:outputLink value="http://facesbook.com/sharer.php">
<f:param name="u" value="#{bean.url}" />
</h:outputLink>
关于html - html 标签上的转义 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158498/