[英]How to render content as both plain text and URL encoded
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>
。
我想在同一页面上提供指向 email <a href="mailto:">
的链接,并将 label 字段 (id=myLabel) 中的内容放入 email。
我还找到了像<a href="mailto:?subject=mySubject&body=myBody">
这样的示例来预填充 email。所以 myBody 应该是 id=myLabel 字段中的内容。
我正在寻找一种解决方案,将渲染内容从 label 中提取为正文。 我想,内容也必须是 url 编码的。
有什么提示吗? 提前致谢。
您可以做的是使用c:var
将p:outputLabel
outputLabel 的内容添加到一个变量中。 例如:
<c:set var="myVar">Some text at #{now}</c:set>
<p:outputLabel>#{myVar}</p:outputLabel>
这允许您在链接中重用myVar
。 在您的情况下,您想将其用作 URL 参数。 所以它确实需要 URL 编码。 为此,您可以简单地使用带有f:param
的h:outputLink
,它将自动编码为 URL:
<h:outputLink value="mailto:">
<f:param name="subject" value="#{myVar}" />
<f:param name="body" value="#{myOtherVar}" />
Send mail
</h:outputLink>
您还可以创建自定义 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)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.