如何将内容呈现为纯文本和 URL 编码

问题描述

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>

<h:outputLabel>

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

我还发现了这样的例子 <a href="mailto:?subject=mySubject&body=myBody"> 预填电子邮件。所以 myBody 应该是来自 id=myLabel 字段的内容

我正在寻找一种解决方案,将渲染的内容标签提取为正文。 我假设,内容也必须是 url 编码的。

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

解决方法

您可以做的是使用 p:outputLabelc:var 的内容添加到变量中。例如:

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

这允许您在链接中重复使用 myVar。在您的情况下,您想将其用作 URL 参数。所以它确实需要进行 URL 编码。为此,您可以简单地使用带有 h:outputLinkf:param,它会自动进行 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)}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...