簡體   English   中英

如何將內容呈現為純文本和 URL 編碼

[英]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:varp:outputLabel outputLabel 的內容添加到一個變量中。 例如:

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

這允許您在鏈接中重用myVar 在您的情況下,您想將其用作 URL 參數。 所以它確實需要 URL 編碼。 為此,您可以簡單地使用帶有f:paramh: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM