0

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>
phil
  • 1,289
  • 1
  • 15
  • 24
  • 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 Apr 24 '16 at 11:08
  • Thank you for your comment. Damned.. I found this to your comment: http://stackoverflow.com/questions/8054165/using-put-method-in-html-form . But I'm still confused because that class works with my chrome browser to make a put request: https://github.com/ewolff/microservice/blob/master/microservice-demo/microservice-demo-customer/src/main/resources/templates/customer.html – phil Apr 24 '16 at 11:16
  • 2
    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 Apr 24 '16 at 11:18
  • 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: https://github.com/spring-projects/spring-boot/issues/3643 – phil Apr 24 '16 at 12:25
  • But there is still my question because there comes no Data to my PUT method while the POST method works... – phil Apr 25 '16 at 06:51
  • 1
    Then mention you are using Spring Boot as that changes a lot of things. Which Spring Boot version are you using? – M. Deinum Apr 25 '16 at 07:09
  • I load my dependecies via maven and not specify the version. So it should be the newest in the mvn repo. A look into the jar shows lib\spring-boot-1.2.4.RELEASE.jar – phil Apr 25 '16 at 07:26
  • Aaaahhh... I'm new to maven too.. I guessed that it will load the newest spring boot lib. But the newest in mvn repo is 1.3.3. http://mvnrepository.com/artifact/org.springframework.boot/spring-boot – phil Apr 25 '16 at 07:28
  • Sorry for not mention clear that it is a spring boot application. It stands in the middle of my text, but I edited my question and put it to the beginning. Thanks for editing my question tags. So my problem ist, that I load "spring-cloud-starter-parent" (http://projects.spring.io/spring-cloud) as parent pom. This is in version "Angel.SR6" and only supports spring-boot 1.2.8. I don't have an idea how I could load Spring Boot 1.3.3 to solve my "put" problem, but I will do some investigation. – phil Apr 25 '16 at 12:06
  • Or simply register the filter yourself... – M. Deinum Apr 25 '16 at 12:15
  • So, I solved the version problem with use an other spring-cloud version which supports spring-boot 1.3.3:-) I think if spring-oot supports this I should use it. Thank you for your helping comment! – phil Apr 25 '16 at 13:25

1 Answers1

1

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!!

phil
  • 1,289
  • 1
  • 15
  • 24