我對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>
表單或瀏覽器不支持僅PUT POST和GET。如果你想放棄或刪除東西,你將需要一個JavaScript解決方案。 –
感謝您的評論。該死的..我發現這個給你的評論: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
只是做了一些檢查百里香葉來源,顯然'TH:方法'實際上做了一個POST,但增加了一個隱藏的領域來隱藏實際的方法,即PUT。爲了這個工作,你需要配置'HiddenHttpMethodFilter'並且爲了支持PUT請求,還需要'HttpPutFormContentFilter'。確保'HiddenHttpMethodFilter'出現在'HttpPutFormContentFilter'之前,否則它將不起作用。 –