【问题标题】:How to render content as both plain text and URL encoded如何将内容呈现为纯文本和 URL 编码
【发布时间】:2021-07-08 11:58:41
【问题描述】:

JSF 2.x/Primefaces 7.x

假设我有一个

<p:outputLabel id="myLabel">
  Here some static text written from a colleage without java background,
  but mixed with #{myBean.name} information from java object
<p:outputLabel>

&lt;h:outputLabel&gt;

我想在同一页面上提供一个电子邮件链接 &lt;a href="mailto:"&gt; 并将标签字段 (id=myLabel) 中的内容放入电子邮件中。

我还发现了类似的例子 &lt;a href="mailto:?subject=mySubject&amp;body=myBody"&gt; 预先填写电子邮件。所以 myBody 应该是 id=myLabel 字段的内容。

我正在寻找一种解决方案来从标签中提取呈现的内容作为正文。 我想,内容也必须是 url 编码的。

有什么提示吗? 提前致谢。

【问题讨论】:

  • 这个问题有点不清楚。我认为您需要进一步解释您想要做什么。你需要给我们看一个具体的例子。

标签: jsf primefaces jsf-2


【解决方案1】:

您可以使用c:varp:outputLabel 的内容添加到变量中。例如:

<c:set var="myVar">Some text at #{now}</c:set>
<p:outputLabel>#{myVar}</p:outputLabel>

这允许您在链接中重复使用myVar。在您的情况下,您希望将其用作 URL 参数。所以它确实需要进行 URL 编码。为此,您只需将h:outputLinkf:params 一起使用,它将自动进行URL 编码:

<h:outputLink value="mailto:">
  <f:param name="subject" value="#{myVar}" />
  <f:param name="body" value="#{myOtherVar}" />
  Send mail
</h:outputLink>

您也可以创建 a custom EL function 来执行此操作,或者,如果您使用的是 OmniFaces,您可以使用 of:encodeURL

<c:set var="myVar">Some text at #{now}</c:set>
<p:outputLabel>#{myVar}</p:outputLabel>
'myVar' URL encoded: #{of:encodeURL(myVar)}

【讨论】:

  • 我喜欢这个主意。抱歉没有提到,但在文本部分我也有格式信息,如&lt;br/&gt;。当我把它放到c:set 之后使用它时,我仍然有&lt;br/&gt;,当我使用p:outputLabel 中的变量时。尚未发现逃脱这部分。但我仍在调查您的解决方案。
  • 使用&lt;h:outputText escape="false" value="#{myVar}"/&gt;,但在使用之前请阅读有关 XSS 攻击的信息。
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
相关资源
最近更新 更多