Thymeleaf would not resolve html entities - thymeleaf

How do I get thymeleaf to resolve my html entities?
I have the following:
<input th:name="title" th:value="Wayne’s World" />
will simply produce an input element with "Wayne’s World" instead of "Wayne's world".
Any way to make thymeleaf resolve the html entities?

You have two choices.
First. Do not use html entities at all. Just escape special symbols using \. Note, that you have specify string value in single quotes:
<input th:name="title" th:value="'Wayne\'s World'" />
Second. Use Thymeleaf's string utility for escape xml enitites:
<input th:name="title" th:value="${#strings.escapeXml('Wayne’s World')}" />
When your string value is coming from controller, don't use __${}__ preprocess expression. It doesn't needed. Just use Thymeleaf's standard variable expression ${}. And don't enclose this expression in single quotes. Look at ${title} variable in next example:
<th:block th:include="row::row(attrs='value=${title}, minLength=\'.{1, 16}\', required=true, ... />
In this case you can add string value in controller as is, without any escaping:
public String method(ModelMap model){
...
mode.addAttribute("title", "Wayne's world");
...
}

Related

How to use a razor variable mixed with html ID text?

I am getting an error because the razor and html is getting confused by the compiler I imagine.
<div id="#Model.MyLabelCars" ...>
My model variable is:
Model.MyLabel
The "Cars" is just raw text that should be in the HTML.
So say Model.MyLabel's value is "123" the ID should be:
id="123Car"
How can I seperate the model's variable name and HTML?
You could use regular string add operator
<div id="#(Model.MyLabel + "Car")"></div>
Or C# 6's string interpolation.
<div id="#($"{Model.MyLabel}Car")"></div>
What you want is to use the <text></text> pseudo tags
<div id="#Model.MyLabel<text>Cars</text>" ...>

How to html escape variables in Thymeleaf?

I would like to display several variables in a web page using Thymeleaf.
I have the code set as follows...
<span th:text="${foo.bar}" />
The problem is that when property foo.bar contains multiple spaces in a row, they are displayed as one (expected behaviour for HTML).
e.g. "hello world" => "hello world"
Is there a "Thymeleaf" way to HTML escape the value of the variable so that the value visually appears exactly as it should be?
I think the best way is to use HTML's way of accomplishing this: either using <pre> tags (<pre th:text="${foo.bar}" />), or else using the css white-space property and changing the style of your <span> tags that contain variables.
I guess you could also replace ' ' with (like this: <span th:text="${#strings.replace(foo.bar, ' ', ' ')}" />, but that would be my last option.

Replacing string with HTML control MVC View

<td style="font-size:1em;font-family:Verdana;color:darkkhaki">
#item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>")
</td>
I have seen a lot of similar questions, but I guess this is not a duplicate, because all the questions are how to add a control on serve-side. I want to replace a string that I get in my object.property in my cshtml View. I want to inject a textbox (or any html control) in place of some special characters.
The above code snippet simply replaces <input type='Text'></input> as text.
For e.g.
Acronym of CSS is ##
Here ## should be replaced by a textbox.
MVC will by default HTML encode all your strings in your view. You have tell it to output it as a raw HTML string instead by using Html.Raw:
#Html.Raw(item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>"))
Another option without having to use Html.Raw (security concern) is to split the string by the replacement word then combine them together with the HTML input:
#
{
string[] splits = item.ObjQuestions.QuestionText.Split(new string[] {"____"}, StringSplitOption.None);
}
#splits[0]
<input type='Text'></input>
#if (splits.Length > 1)
{
#: #splits[1]
}
This is for string with one placeholder only, if otherwise use a loop instead.

struts2 s:select value ognl expression

In Struts2 tutorial for s:select tag I've seen:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/> ^ ^
and my question: why value="%{petDao.pets.{id}}"? why not simply value="%{petDao.pets.id}"? what do those trailing curly braces mean?
This is an OGNL list projection to get all the id values as a list from petDao.pets, meaning all values in this <s:select> will be pre-selected.
It isn't necessary; I suspect it was the result of an error in the tag's source file.
It works with it, but isn't needed, will fail IDE validation (if the IDE supports S2 and/or OGNL, e.g., IntelliJ), and I've made a note to update.
The principal reason is because %{} syntax is used to force OGNL evaluation where Struts would otherwise be treating the value as a String literal.
For example,
<s:property value="name" />
will look for a name property in the value stack i.e. is a value retrieved by calling getName().
If you wanted to force it to use literal value "name", you will need to use the %{} syntax -
<s:property value="%{'name'}" />
Source:
http://www.coderanch.com/t/420711/Struts/Struts

Difference between '#','%' and '$'

I'm new to struts2 and confused by the '#','%' and '$' element. There are some usages like:
${user.name}
%{user.name}
<s:radio list="#{key1:value1,key2:value2}" />
Could any give me an explanation and examples?
To put it simply
If ${user.name} in jsp page, it is an EL expression.
If ${user.name} in struts.xml, it is an OGNL expression.
If %{user.name} in jsp page, it is an OGNL expression.
Final, #{key1:value1,key2:value2} is an OGNL expression, it means creates a map that maps the key1 to the value1 and key2 to the value2.
BTW: #{key1:value1,key2:value2} should be wrap in %{}, like %{#{key1:value1,key2:value2}}, however, some attributes in struts2 tags will assume that is OGNL expression, that means without %{} is OK.

Resources