2016-04-24 125 views
0

我对spring引导,thymeleaf和spring mvc非常陌生,但我无法解决我的问题。 我将我的真实案例移到一个最简单的示例中,以使其更容易理解。Thymeleaf和Spring MVC:PUT请求在窗体中导致null值,而相同的代码与POST请求一起工作

这是我的真简单 “Customer.java ”级:

@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; 
    } 
} 

我有以下的“ MyController.java” 一流:

@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"); 
    } 
} 

我的客户资源/ 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> 

如果我在chrome浏览器中启动我的应用程序(SpringBoot)并使用http://localhost:8080/abwesenheits_microservice/new_customer_form.html url,则会出现用户ui-form。 中键入一个名称(例如克劳迪娅希弗),并点击提交按钮后,出现以下日志:

把客户:0:空空

这是我的问题:为什么形式参数为null如果出现表单请求?

重的是:如果我改变,从把张贴请求方法(更换th:method="put"th:method="post")它的工作原理,并导致以下日志:

邮政客户:0:希弗克劳迪亚

谢谢你阅读我的问题。

编辑 Thymeleaf & Spring MVC的产生与“放”的价值和变化自动的方法来“后”的隐藏字段。但是,仍来自没有数据..

生成的HTML格式的浏览器是:

<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

表单或浏览器不支持仅PUT POST和GET。如果你想放弃或删除东西,你将需要一个JavaScript解决方案。 –

+0

感谢您的评论。该死的..我发现这个给你的评论:http://stackoverflow.com/questions/8054165/using-put-method-in-html-form。但是我仍然感到困惑,因为这个类可以和我的Chrome浏览器一起工作来提出请求:https://github.com/ewolff/microservice/blob/master/microservice-demo/microservice-demo-customer/src/main/资源/模板/ customer.html – flipperweid

+1

只是做了一些检查百里香叶来源,显然'TH:方法'实际上做了一个POST,但增加了一个隐藏的领域来隐藏实际的方法,即PUT。为了这个工作,你需要配置'HiddenHttpMethodFilter'并且为了支持PUT请求,还需要'HttpPutFormContentFilter'。确保'HiddenHttpMethodFilter'出现在'HttpPutFormContentFilter'之前,否则它将不起作用。 –

回答

1

随着我解决我的问题M. Deinum伟大的意见。 问题是我需要注册一个HttpPutFormContentFilter

Spring Boot通常会自动执行该操作 - 但仅限于1.3.0以上版本。

我的问题是,我加载弹簧云(org.springframework.cloud)作为父母在我的pom.xml版本AngelSR6。该版本对版本1.2.8中的spring-boot具有依赖性。所以这是自动注册过滤器的错误版本。

寻找春天 - 云(http://projects.spring.io/spring-cloud/)发布的火车后,我改变了版本从“AngelSR6”到“Brixton.RC2”,这在1.3.3版本依赖于春天启动。

现在我的请求作品:-) 感谢M. Deinum的评论!!