2

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.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
ula.uvula
  • 158
  • 13
  • The question is a bit unclear. I think you need to further explain exactly what you want to do. You need to show us a concrete example. – Adam Waldenberg Apr 13 '21 at 17:08

1 Answers1

2

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:params, 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)}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • I like the idea. Sorry not mentioned, but in the text part I also have formatting information like ```
    ```. When I put it to the ```c:set``` and later use it, I still have the ```
    ```, when I use the variable in ```p:outputLabel```. Not yet found to escape this part. But I still investigate your solution.
    – ula.uvula Apr 14 '21 at 07:49
  • Use ``, but read about XSS attacks before doing so. – Jasper de Vries Apr 14 '21 at 07:54