简体   繁体   中英

How to render content as both plain text and URL encoded

JSF 2.x/Primefaces 7.x

Assuming I have a

<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>

or <h:outputLabel> .

I want offer on same page a link to email <a href="mailto:"> and put the content from the label field (id=myLabel) into the email.

I also found examples like <a href="mailto:?subject=mySubject&body=myBody"> to pre-fill the email. So myBody should be the content from id=myLabel field.

I'm looking for a solution to pull the rendered content from label as body. I assume, the content must also be url-encoded.

Any hints? Thanks in advance.

What you could do is add the contents of your p:outputLabel to a variable, using c:var . For example:

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

This allows you to reuse myVar in a link. In your case you want to use it as a URL parameter. So it needs to be URL encoded indeed. To do so you can simply use a h:outputLink with f:param s, which will automatically be URL encoded:

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

You could also create a custom EL function to do so, or, if you are using OmniFaces you can use of:encodeURL :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM