I have a web application running Java Tapestry, with a lot of user-inputted content. The only formatting that users may input is linebreaks.
I call a text string from a database, and output it into a template. The string contains line breaks as /r, which I replace with < br >. However, these are filtered on output, so the text looks like b<br>text text b<br> text. I think I can use outputRaw or writeRaw to fix this, but I can't find any info for how to add outputRaw or writeRaw to a Tapestry class or template.
The class is:
public String getText() {
KMedium textmedium = getTextmedium();
return (textmedium == null || textmedium.getTextcontent() == null) ? "" : textmedium.getTextcontent().replaceAll("\r", "<br>");
}
The tml is:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
Where would I add the raw output handling to have my line breaks display properly?
To answer my own question, this is how to output the results of $getText() as raw html:
Change the tml from this:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
To this:
<p class="categorytext" id="${currentCategory.id}">
<t:outputraw value="${getText()}"/>
</p>
Note that this is quite dangerous as you are likely opening your site to an XSS attack. You may need to use jsoup or similar to sanitize the input.
An alternative might be:
<p class="categorytext" id="${currentCategory.id}">
<t:loop source="textLines" value="singleLine">
${singleLine} <br/>
</t:loop>
</p>
This assumes a a getTextLines() method that returns a List or array of Strings; it could use the same logic as your getText() but split the result on CRs. This would do a better job when the text lines contain unsafe characters such as & or <. With a little more work, you could add the <br> only between lines (not after each line) ... and this feels like it might be a nice component as well.
Related
I am trying to convert markdown hyperlinks into html hyperlinks in the Apache Velocity template language (for Marketo). I am nearly there by splitting on ']' and then removing the first character from the remaining '[link text' piece, and the first and last characters from the remaining '(url)' piece.
It will let me remove the first character in each, but doesn't like my code for removing the last character. This is simple code so I don't know why it isn't working.
#set( $refArr = $reference.split(']',2) )
<li>
<a href=$refArr[1].substring(1,$refArr[1].length()-1)>$refArr[0].substring(1)</a>
</li>
It just doesn't like the '-1' part, see error. Velocity is supposed to have full Java method access, but it appears that it may be confusing Java for html.
Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Encountered "-1" near</p>
I've also tried using regex with the replace method as well but that doesn't work either, whether with the '(' character escaped, double escaped, or not escaped.
Apparently you have to use the MathTool class for Velocity:
#set( $refArr = $reference.split(']',2) )
<li>
<a href=$refArr.get(1).substring(1,$math.sub($refArr.get(1).length(),1))>$refArr.get(0).substring(1)</a>
</li>
Your code should work in recent versions, otherwise you can do it in two steps:
#set($len = $refArr.get(1).length() - 1)
<a href=$refArr[1].substring(1,$len)>$refArr[0].substring(1)</a>
I am trying to achieve internationalization and I have the following html markup.
<p th:text= "${your_amount(${it.vm.getAmount})}"></p>
which generates html as
<p>Your Amount is: $</p>
Your Amount is: $ is exactly what my internationalized string is.
Ideally the result should be.
<p>Your Amount is: $123.24</p>
The it.vm.getAmount sort of doesn't get executed. I have checked that there is a value present inside getAmount with the following
<p th:text="${it.vm.getAmount}"></p>
Which gives me a result as
<p>123.24</p>
Does thymeleaf consider "$" to be a special character while rendering and is there a way to work around that?
This expression:
<p th:text= "${your_amount(${it.vm.getAmount})}"></p>
is not valid syntax -- for many reasons.
${} expressions are never nested inside other ${} expressions -- unless you are preprocessing.
If your_amount is a string, you cannot use it as a function your_amount(...).
it.vm.getAmount shouldn't work -- unless you really have named your getter method getGetAmount(). It should either be it.vm.amount or it.vm.getAmount().
When I try your expression, I get this error:
Method your_amount(java.lang.String) cannot be found on org.thymeleaf.spring4.expression.SPELContextMapWrapper
If you just want to append different strings together, you should really be doing something like:
<p th:text= "${your_amount + it.vm.getAmount}"></p>
or
<p>
<span th:text="${your_amount}"><span th:text="${it.vm.getAmount}">
</p>
or
<span th:text="|${your_amount}${it.vm.getAmount}|" />
I have an element like below;
<p> I am lost </p>`
I need to highlight "am" like below;
<p> I <mark style="background-color:#FFFF00;">am</mark> lost </p>
My code is like this.
String newText = "I <mark style=\"background-color:#FFFF00;\">am</mark> lost";
element.text(newText);
But when I print the element it looks like this.
<p>I <mark style="background-color:#FF0000;">am</mark> lost</p>
Are there any ways to force "<" and ">" characters to elements using Jsoup?
Instead of element.text(newText) you should use element.html(newText)
In my HTML I'm using paragraph that gets content by calling method via thymeleaf:
<p data-th-text="${fund.formatDescription()}"></p>
Method:
private String description;
public String formatDescription() {
return description.replace(";", " \n ");
}
I want my description to have end lines in palce of every semicolon. So that's why I added \n. But thymeleaf ingores new lines and returns continuous text. I tried adding <br/> but it ends up not interpreted as html. What should I add in place of semicolon to force new line in the description?
Html ignores newlines (this isn't thymeleaf's fault). You can either:
Put the description into <pre></pre> tags (or use the css white-space property on the <p> element).
Instead of replacing ; with \n, replace it with <br /> and use th:utext instead of data-th-text. (This means that html will be unescaped, so you better make sure users can't put other html into the description field or you open yourself up to html attacks).
I made a Thymeleaf dialect that makes it easy to keep the line breaks, if the css white-space property isn't an option.
It also bring support for BBCode if you want it.
You can either import it as a dependency (it's very light) or just use it as inspiration to make your own.
Check it out here :
https://github.com/oxayotl/meikik-project
In my web application (my first with Java, Spring, OR Roo), I'm building a form that has nothing to do with any JPA objects, it's just a form. I really don't want to use JSTL to build my forms here, because there's no data backing for them at this point. I'm using tiles to assemble the pages, so the guts of this form comes from a view, but apart from that there's nothing JSPish about it; it's just a form.
Inside that form, I have a text area that I've written:
<textarea id="whatever" name="whatever"></textarea>
When that comes to the screen, the </textarea> tag is gone. Different browsers deal with that differently, up to and including swallowing up the whole rest of the body HTML inside the text area field.
So I tried putting some content inside that textarea. Spaces and line breaks don't change its behavior, but it appears that any non-space character does. If I go
<textarea>.</textarea>
... it respects my close textarea tag. But then of course my text area renders on the screen with a dot in it, which isn't what I want.
Is this a known issue? Am I doing something wrong?
EDIT:
#bozho: Here's a pertinent chunk of my jsp:
<div id="notes" class="detailPanel">
<div class="panelLabel">Notes</div>
<table >
<thead><tr><th>Date</th><th>By</th><th>Note</th></tr></thead>
<tbody id="notesBody"></tbody>
</table>
<textarea id="newNote" rows="5" cols="80" >.</textarea>
<button id="addNewNote" onClick="saveNote();">Add New Note</button>
</div>
Absolutely nothing fancy going on here (I populate the tbody with rows on the client, is why that's empty). Without the dot in the third-to-last line, the closing textarea tag does not come out in the resulting HTML.
EDIT2 (Solution):
This URL became googlable after hearing some key words from people responding here:
http://www.jroller.com/komu/entry/textareas_with_jspx
Turns out that when jspx pages are parsed, empty tags are collapsed into a single self-closing tag, which breaks text areas. The solution is to put an empty jsp:text in the middle:
<textarea><jsp:text /></textarea>
(Which is STAGGERINGLY stupid, but there it is.)
You are using jspx files right?
In general jspx remove something (or in your case it shorten it: check this: I expect that it addes a slash to the former opening tag, so it becomes: <textarea id="whatever" name="whatever"/> ) where it belives that is not needed. What exactly depends ona bit on the implementation.
So put a <jsp:text> tag in the text area tag to prevent it from "closing"
<jsp:text>
<textarea id="whatever" name="whatever"></textarea>
</jsp:text>
<textarea id="whatever" name="whatever"><jsp:text /></textarea>
for an more complex example have a look at this answer: websphere 7 (and Spring Roo) incompatible with javax.el.ELException