Thymeleaf construct URL with variable

As user482745 suggests in the comments (now deleted), the string concatenation I previously suggested <form th:action=”@{/mycontroller/} + ${type}”> will fail in some web contexts. Thymeleaf uses LinkExpression that resolves the @{..} expression. Internally, this uses HttpServletResponse#encodeURL(String). Its javadoc states For robust session tracking, all URLs emitted by a servlet should be run through this method. … Read more

How do I populate a drop down with a list using thymeleaf and spring

This is how I populate the drop down list.I think it may help to you get some idea about it. Controller List<Operator> operators = operatorService.getAllOperaors() model.addAttribute(“operators”, operators); Model @Entity @Table(name = “operator”) public class Operator { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = “id”) @JsonIgnore private Long id; @NotBlank(message=”operator Name cannot be empty”) @Column(name = “operator_name”, … Read more

Send datas from html to controller in Thymeleaf?

You can find an example in http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form. As the tutorial suggests, you need to use th:object, th:action and th:field to create a form in Thymeleaf. It looks like this: Controller: @RequestMapping(value = “/showForm”, method=RequestMethod.GET) public String showForm(Model model) { Foo foo = new Foo(); foo.setBar(“bar”); model.addAttribute(“foo”, foo); … } @RequestMapping(value = “/processForm”, method=RequestMethod.POST) public String … Read more

How to do if-else in Thymeleaf?

Thymeleaf has an equivalent to <c:choose> and <c:when>: the th:switch and th:case attributes introduced in Thymeleaf 2.0. They work as you’d expect, using * for the default case: <div th:switch=”${user.role}”> <p th:case=”‘admin'”>User is an administrator</p> <p th:case=”#{roles.manager}”>User is a manager</p> <p th:case=”*”>User is some other thing</p> </div> See this for a quick explanation of syntax … Read more