Thymeleaf code in Spring Boot attribute model - spring

is it possible to sent thymeleaf code via attributeModel to View, to be treated like standard code?
I would like to load a piece of code(thymeleaf fragments) in different places only when it is needed.
But when I try this:
Spring Boot Controller:
model.addAttribute("fragment", "<th:block th:include=\"fragments/header :: body\"></th:block>");
View:
<div th:text="${fragment}"></div>
In WebBrowser as a TEXT I have:
<th:block th:include="fragments/header :: body"></th:block>
Can I force re-render? And how can I do that? But if it is not possible, what can I do in replace?

Every thing that th:text parse, set to innerHTML and not rerendered.
You can set parameter in your controller and then check in Thymeleaf if that set, then include or replace your fragment. like this:
Controller
model.addAttribute("isFragmentBodyShow", true);
View
<th:block th:if="${isFragmentBodyShow}" th:include="fragments/header :: body"></th:block>
You can also send fragment name from contoller to Thymeleaf and use it in th:include for dynamic template include.

Related

How Can I show data on Thymeleaf

Hello all brother this is my controller
model.addAttribute("getDataHead",cartHeadStockinService.getDatahead());
System.out.println(model);
and after that I syso to review i get like this
getDataHead=[Cart_HeadStockin [id=101, invoice=fsdfsdf, po=, remark=, supplyId=1, date=null]]
and I want to show this on my Thymeleaf
th:object="${getDataHead}"
th:text="${invoice}"
Hello friend thats great you are able put the context variables in your Model
The way to print the text of your in your Thymeleaf is like this
<span th:text="${getDataHead.invoice}"></span>
Here is a reference link to Thymeleaf documentation

Passing a list generated with Javascript to a Spring controller

My app is being built with Spring Boot using a MVC pattern, and as template viewer I use Thymeleaf.
I'm generating a dynamic list with Javascript in a form, which I need to collect as a List with the controller.
I have tried to solve it with a #RequestParam, but generating the list with Javascript, as far as I'm concerned, I can't set the Thymeleaf tags.
This is the list:
<ul id="addItemList">
<li class="list-group-item" id="group" name="group" value="Outdoors">Outdoors</li>
<li class="list-group-item" id="group" name="group" value="Entertainment">Entertainment</li>
</ul>
Any indication on which approach I should take, would be much appreciated.
Thanks in advance.
Create a Model having a List as property, and pass it as #ModelAttribute in your controller.
In the end I solved this issue with ajax. I had a button to add an element to the list, which was being made with Javascript. I added a jQuery $.post function to save the item, each time that a new one was added to the list by selecting that button. I didn't find the way to move the whole list from javascript, to the Spring Controller.
Follow the process:
Get the list from controller using ajax
Parse the data then generate li with the retrieved values and update the html of id addItemList

Spring + Thymleaf Header dynamic data

I'm using Spring Boot with Thymleaf. My Layout is composed from 3 zones, header, content and footer.
In header I want to display "Hello !", if user is not logged in I will display "Hello Guest!".
How can I get send the "username" to header.html file ?
It's important to write this code once and to be called everywhere where header.html is included.
If you are saving the username in the session,you can access session object in Thymeleaf template with a session variable. If the session contains username attribute it will display the value , anyway null check is taken care by Thymeleaf if the attribute username is not in session object.
<span>Hello</span>
<span th:text="${session.username}" />
You can use Thymleaf's th:with attribute to pass on variables to your fragments.
This is how I am including my nav fragment by passing some data to the fragment.
<div th:include="fragments/nav :: nav" th:with="userLogin='true', view='business'"></div>
Now inside my nav fragment I can simply use ${userLogin} or ${view} to access the variables.
Hope this helps.

Is it possible to add Html tags into Spring MVC3 I18N messages.properties file

Given the following MVC3 i18n use:
at a jsp file:
<s:message code="clickHere">
at message.properties file:
clickHere=Please click Here
a user's browser will display(and the word Here will be a link to http://abc.com):
Please click Here
Thanks
Did you try
<s:message code="clickHere" htmlEscape="false" />
In any case you use Thymeleaf, instead of th:text, you should use the th:utext.
Example below:
# Instead of this
<div th:text="#{message}">text to be replaced</div>
# Use this
<div th:utext="#{message}">text to be replaced</div>
You should always keep in mind that using the th:utext can cause XSS Attacks.
In the properties file, you will obviously have the following,
# In properties
message=Text to be displayed

How to create hyperlink in Spring + JSP

What's the proper way to create a hyperlink in Spring+JSP? There must be a better way than just coding in the <a href="..."> tag. Take for example a page that displays people. The URL is people.htm. The corresponding controller gets people from the database and performs optional column sorting. The JSP might look like:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Address</td>
</tr>
...
This seems bad as the URL people.htm is hardcoded in the JSP. There should be a way to have Spring automatically build the <a> tag using the URL defined in servlet.xml.
Edit: Maybe I should be using a Spring form.
The only thing that comes to mind is the JSTL standard tag <c:url>. For example:
<c:url var="thisURL" value="homer.jsp">
<c:param name="iq" value="${homer.iq}"/>
<c:param name="checkAgainst" value="marge simpson"/>
</c:url>
Next
Now this won't get you servlet mapping or the like but nothing will. It's not something you could really do programmatically (after all, a servlet can and usually does map to a range of URLs). But this will take care of escaping for you.
I haven't seen this kind of functionality in pure spring (although grails offers things like that).
For your specific case you might consider removing the file part and only using the query string as the href attribute:
<td>Name</td>
<td>Age</td>
<td>Address</td>
These links append the query string to the path component of the current url.
In Spring MVC in jsp:
You can use:
General Hyperlink:
Click Here
If passing from controller:
Click Here
Jsp tags
<c:url var="URL" value="login">
<c:param name="param" value="${parameter}"/>
</c:url>
Click Here
Hope it Helps.. :)
Better way to create link is:
Name
<%=request.getContextPath() %> makes sure that correct URI will be taken into account.
"sort" parameter you can get over with hidden field and change a value with a little bit of javascript:
<input type="hidden" name="sort" id="sort" value="name">
And controller method should look like this:
#RequestMapping("/people")
public String createUser(String sort) {
...
}
Import this package in your jsp file
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
when you want to redirect new page or url then use for eg.
<a href='<c:url value="url of next page" />'>Home</a>

Resources