问题描述
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:outputLabel
将 c:var
的内容添加到变量中。例如:
<c:set var="myVar">Some text at #{now}</c:set>
<p:outputLabel>#{myVar}</p:outputLabel>
这允许您在链接中重复使用 myVar
。在您的情况下,您想将其用作 URL 参数。所以它确实需要进行 URL 编码。为此,您可以简单地使用带有 h:outputLink
的 f: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)}