I'm really new to spring boot, thymeleaf and spring mvc and I can't solve my problem. I move my real case to a minimal example to make it easier understand for you.
This is my really simple "Customer.java"-class:
@Entity
public class Customer {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
public Customer() {
super();
id = 0l;
}
public Customer(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
/*GETTER AND SETTER FOR ALL ATTRIBUTES*/
@Override
public String toString() {
return id + ": " + firstName + " " + lastName;
}
}
I have the following "MyController.java"-Class:
@Controller
class MyController{
//....
@RequestMapping(value = "/new_customer_form.html", method = RequestMethod.GET)
public ModelAndView getNewCustomerForm() {
logger.info("NEW Customer Form");
return new ModelAndView("customer", "customer", new Customer());
}
@RequestMapping(value = "/customer_save/{id}.html", method = RequestMethod.PUT)
public ModelAndView putCustomer(@PathVariable("id") long id, Customer customer,
HttpServletRequest httpRequest) {
logger.info("PUT Customer: " + customer.toString());
return new ModelAndView("success");
}
@RequestMapping(value = "/customer_save/{id}.html", method = RequestMethod.POST)
public ModelAndView postCustomer(@PathVariable("id") long id, Customer customer,
HttpServletRequest httpRequest) {
logger.info("POST Customer: " + customer.toString());
return new ModelAndView("success");
}
}
My customer resources/templates/customer.html file is that:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<title>Customer : Edit</title>
</head>
<body>
<h1 >Customer : Edit</h1>
<div>
<form th:action="'customer_save/'+${customer.id}+'.html'" th:object="${customer}"
th:method="put" role="form">
<div class="form-group">
<label >Lastname</label> <input type="text"
th:field="*{lastName}" class="form-control" placeholder="lastName" />
</div>
<div class="form-group">
<label >Firstname</label> <input type="text"
th:field="*{firstName}" class="form-control"
placeholder="firstName" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</body>
</html>
If I start my application (SpringBoot) and use the http://localhost:8080/abwesenheits_microservice/new_customer_form.html url in chrome browser the user ui-form appears. After typing a name (e.g. Claudia Schiffer) in and click the submit Button the following LOG appears:
PUT Customer: 0: null null
That is my question: Why the form parameters are null in case of a put form request?
The heavy thing is: If I change the request method from put to post (replace the th:method="put"
with th:method="post"
) it works and lead to the following log:
POST Customer: 0: Schiffer Claudia
Thank you for reading my question.
EDIT Thymeleaf & Spring MVC generates a hidden-field with "put" value and changes automatically the method to "post". But there still comes no data..
The resulting html-form in browser is:
<form role="form" method="post" action="customer_save/0.html"><input type="hidden" name="_method" value="put">
<div class="form-group">
<label>Lastname</label> <input type="text" class="form-control" placeholder="lastName" id="lastName" name="lastName" value="">
</div>
<div class="form-group">
<label>Firstname</label> <input type="text" class="form-control" placeholder="firstName" id="firstName" name="firstName" value="">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
th:method
actually does a POST but adds a hidden field to hide the actual method i.e. PUT. For this to work you would need to have configured theHiddenHttpMethodFilter
and for supporting PUT requests also aHttpPutFormContentFilter
. Make sure theHiddenHttpMethodFilter
comes before theHttpPutFormContentFilter
else it won't work. – M. DeinumHttpPutFormContentFilter
. I found that information that it is already in Spring Boot here: github.com/spring-projects/spring-boot/issues/3643 – phil