Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf Modify and Post Current Object

I have a form and post data into controller via Thymeleaf:

<form action="lia.html" th:action="@{/lia}" th:object="${myRequest}" method="post">

At another place of my html page, if a user click a particular button, I want to modify that object and send it to same controller.

I have already that object which has been initialised. Button is not a part of any form. How can I send that object into a controller with Thymeleaf.

PS: I know that I can send it via Javascript or put such buttons into a form but I want to learn the Thymeleaf way.

like image 932
kamaci Avatar asked Oct 13 '16 11:10

kamaci


People also ask

How to map a form to a thymeleaf model?

Easier way to map the form handling to a thymeleaf model would be to use th:object attribute. The important things to note here is that the th:object attribute in <form> element and th:field attribute of input elements. These attributes let thymeleaf and spring know how to weave the parameters.

How to format and extract date components in thymeleaf?

The #dates object in thymeleaf deals with date objects in thymeleaf model. With its help, we can format and extract date components. Formatting Dates in Thymeleaf The typical way to format a date object would be through SimpleDateFormat.

How to generate dynamic content using thymeleaf?

In index.html page the key (allemplist) is identified as a java object and Thymeleaf iterate over the list and generate dynamic content as per the user provided template. /addNew – when user clicks on Add Employee button than request goes to addNewEmployee () method.

How thymeleaf works with Spring MVC?

It can directly access the java object and spring beans and bind them with UI. And it is mostly used with spring MVC when we create any web application. So let’s start with an example to understand how Thymeleaf works with the Spring framework. Here we are going to perform crud operation on Employee dataset.


1 Answers

I think the only similar approach to what you're looking for is using bind with Spring EL expressions.

Thanks to the advanced form-field binding capabilities in Spring MVC, we can use complex Spring EL expressions to bind dynamic form fields to our form-backing bean. This will allow us to create new Row objects in our SeedStarter bean, and to add those rows’ fields to our form at user request.

Take a look the next link, where is good example:

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

  • The button

    <button type="submit" name="removeRow" 
                th:value="${rowStat.index}" th:text="#{seedstarter.row.remove}">Remove row</button>
    
  • Request Mapping

    @RequestMapping(value="/seedstartermng", params={"removeRow"})
    public String removeRow(
        final SeedStarter seedStarter, final BindingResult bindingResult, 
        final HttpServletRequest req) {
    
        final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
    seedStarter.getRows().remove(rowId.intValue());
        return "seedstartermng";
    }
    
like image 109
Pau Avatar answered Sep 17 '22 13:09

Pau