I'm new for thymeleaf.
The question is easy when it's in jsp. But it beats me in thymeleaf.
I want to complete this function as below:
if (page.number<=1)
html like this:
<li class="q-pagination-disabled">Pre</li>
else
<li>Pre</li>
But I can't finish it with th:if,th:href,th:remove and so on.
Does anyone know how to do it?
Try the following and let me know if it worked for you
<li th:if="${page.number le 1}" class="q-pagination-disabled">Pre</li>
<li th:if="${page.number gt 1}"><a th:href="#{/pages/{page}(page=${page.number-1})}">Pre</a></li>
Related
I have my messages.properties like this:
tablas.menu.paises=Pa\u00EDses
tablas.menu.regiones=Regiones
tablas.menu.anhos=A\u00F1os
tablas.menu.universidades=Universidades
tablas.menu.usuarios=Usuarios
In the .html I have a menu showing each item with th:each:
<li class="nav-item" th:each="menu : ${menus}">
<a th:text="#{tablas.menu.${menu.nombre}}"></a>
</li>
For each "menu in ${menus}", menu.nombre has the values paises, regiones, anhos...
But thymeleaf doesn't recognise the model variable inside the #{}, and this it is giving me in the view the error when it doesn't find the message:
??tablas.menu.${menu.nombre}_es_ES??
Configuration is ok, if I change the call for #{tablas.menu.regiones} I get 'regiones' and so.
Is there any way to call a message from messages.properties dynamically using a model variable like this?
Thanks.
You can use the #messages utility object for this:
<a th:text="${#messages.msg('tablas.menu.' + menu.nombre)}" />
Or you can create the string using literal substitution:
<a th:text="#{|tablas.menu.${menu.nombre}|}" />
I would recommend using preprocessing only as a last resort, as bad values can cause runtime errors. (Although they do work for this.)
Using Thymeleaf 3.0.9, suppose I have this translation:
hello=Hello {0}
In my template, I have:
<span th:text="#{hello(${user.getName()})}"></span>
Now suppose user.getName() can be null, so I want to display something nice in that case:
<span th:text="#{hello(${user.getName()?:'Unknown'})}"></span>
This works fine.
Now I also want to tranlate Unknown, how can I do this?
I tried:
<span th:text="#{hello(${user.getName()?:#{unknown})}"></span>
But that does not seem to work.
I think you can use the #messages utility object:
<span th:text="#{hello(${user.getName()?:#messages.msg('unknown')})}"></span>
And add property in the messages file(s):
unknown=Unknown
I am working on a Spring MVC application that uses Thymeleaf as template engine and I am trying to capitalize some string showed into my page. On my page I have something like this:
<li class="com__nav-item" th:each="menuItem : ${#authentication.principal.listaFunzioniUtente}">
<a href="" class="com__nav-link centered">
<span class="blue-line animate scaleIn delay-3" style="font-size: 1.4em; text-align: center;" th:text="${#strings.capitalize(menuItem.desFnz)}"></span>
<span class="white-circle animate scaleIn delay-5"></span>
</a>
</li>
As you can see in the previous code, in the first <span> tag, I show a string inside the desFnz property of the menuItem object.
It works fine, my problem is that I want capitalize all the characters, so I tried to do:
th:text="${#strings.capitalize(menuItem.desFnz)}"
using the #strings.capitalize() but it can't work, in fact in my page I still obtain the text but not capitalized. Why? What am I missing? How can I fix this issue?
#strings.capitalize(menuItem.desFnz) will only capitalize the 1st character, where as #strings.toUpperCase(menuItem.desFnz) will convert the entire string to uppercase. Here is the documentation for the Strings class.
you can do it by
$string.toLowerCase() or $string.toUpperCase()
Just adding to Pradeep Pati's point.
In case you are using it in the spring boot project where some of your values are coming from messages.properties
like In messages.properties file, You have something like:
email.dailyAlert.greeting.newTemplate = Dear {0},
Then to substitute the value in place of {0} (in the Title case), you need to write like the below line.
<p th:text="#{email.dailyAlert.greeting.newTemplate(${#strings.capitalize(orgSlug)})}"></p>
The final output will be:
Dear Organisation,
I am evaluating spring boot + MVC + bootstrap . One problem I am facing is bootstrap's navbar highlighting problem in thymeleaf.
I hope thymeleaf can judge the tab which should be highlighted.
I searched and found this solution : Bootstrap Navbar Highlighting in Thymeleaf
In the containing(outer) page , it uses
<div th:replace="header::header('home')">
to be replaced header
</div>
to designate the home tab should be highlighted.
And in the contained (inner) page , it uses
<nav class="..." th:fragment="header(activeTab)">
<ul class="nav navbar-nav">
<li th:class="${activeTab == 'home'} ? 'active' : null ">Home</li>
</ul>
to judge this tab should be highlighted or not.
It works for every single page well , but not for layout.
In a layout page , the containing (outer) page is a decorator , which decorates other pages (home / about / contact ...) . The tab value is pending here .
for example
<div th:replace="header::header('home')">
to be replaced header
</div>
<div layout:fragment="content">
layout
</div>
<div th:replace="footer::footer">
to be replaced footer
</div>
I cannot pre-assign home tab in the layout file.
Is there any way to solve it ?
Can it judge by controller or even controller's method ?
environment :
springboot.version 1.3.0.M5
spring.version :4.2.1.RELEASE
Thanks a lot !
It is not a exact solution for your question but maybe you will like the idea. You may use request context path to recognize which tab is actually selected, so you can use ${#httpServletRequest.getContextPath()} and maybe then something like that:
<ul class="nav navbar-nav" th:with="view=${#httpServletRequest.getServletPath()}">
<li th:classappend="${#strings.startsWith(view,'/home')? 'active' : ''}">Home</li>
</ul>
Or you can use ControllerAdvice:
#ControllerAdvice(assignableTypes = { MyController.class })
public class MyControllerAdvice {
#ModelAttribute
public void addAttributes(#RequestParam Map<String, String> params, Model model, HttpServletRequest request) {
String activeTab = ... whatever
model.addAttribute("active_tab", activeTab);
}
}
There is one big disadvantage of using #ControllerAdvice. If you are planning to use Spring WebFlow to create multi-page forms (wizards) then it will not work because WebFlow does not use model attributes.
change this line <nav class="..." th:fragment="header(activeTab)">
to this
<nav class="..." th:fragment="header(activeTab='activeTab')">
it worked for me
I am using joomala 2.5 and developed my own component for showing table data in front end and added pagination. I'm getting pagination links, after clicking on the links 'next', 'prev' nothing happens.
What may be the problem?
In view.html.php I've added
$this -> pagination = $this->get('Pagination');
In default.php I've added
<div class="pagination">
<?php echo $this->pagination->getListFooter(); ?>
</div>
You haven't mentioned what you have done in you components model file. My advoice to you is just read this document carefully http://docs.joomla.org/J1.5:Using_JPagination_in_your_component & you will be easily apply pagination. The doc is perfect & its very simple to use pagination in Joomla.
Thank you.
Put your pagination buttons inside a tag and make sure the action url points to the same view (e.g. action = 'index.php?option=com_component&view=listview')
<form action=""...>
<div class="pagination">
<?php echo $this->pagination->getListFooter(); ?>
</div>
</form>