0
votes

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>
1
Forms or browsers don't support PUT only POST and GET. If you want to PUT or DELETE things you will need a JavaScript solution.M. Deinum
Thank you for your comment. Damned.. I found this to your comment: stackoverflow.com/questions/8054165/… . But I'm still confused because that class works with my chrome browser to make a put request: github.com/ewolff/microservice/blob/master/microservice-demo/…phil
Just did some checking in thyme leaf sources and apparently 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 the HiddenHttpMethodFilter and for supporting PUT requests also a HttpPutFormContentFilter. Make sure the HiddenHttpMethodFilter comes before the HttpPutFormContentFilter else it won't work.M. Deinum
Thanks. I edit my question with the resulting html to show that there is really the hidden field. Cause I use Spring Boot (@SpringBootApplication) it should work without manually creating a HttpPutFormContentFilter. I found that information that it is already in Spring Boot here: github.com/spring-projects/spring-boot/issues/3643phil
Then mention you are using Spring Boot as that changes a lot of things. Which Spring Boot version are you using?M. Deinum

1 Answers

1
votes

With the great comments of M. Deinum I solved my problem. The problem was that I need do register a HttpPutFormContentFilter.

Spring Boot do that usually automatically - but only since 1.3.0.

My problem was, that I load spring-cloud (org.springframework.cloud) as parent in my pom.xml in Version AngelSR6. That version has a dependecy to spring-boot in version 1.2.8. So this is the wrong version to registers the filter automatically.

After looking in the release train of spring-cloud (http://projects.spring.io/spring-cloud/) I changed the version from "AngelSR6" to "Brixton.RC2" which has a dependency to spring-boot in version 1.3.3.

Now my put-request works:-) Thank you to M. Deinum for your comments!!