2017-03-03 78 views
0

這是我的控制器類如何將列表數據thymeleaf html綁定到spring控制器?

package com.myblog.controller; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PostMapping; 

import com.myblog.model.User; 

@Controller 
public class UserController { 

    private static final Logger log = LoggerFactory.getLogger(UserController.class); 

    @GetMapping(value="/user") 
    public String getUser(Model model){ 
     model.addAttribute("user", new User()); 
     return "user"; 
    } 
    @PostMapping(value="/user") 
    public String postUser(@ModelAttribute("user") User user){ 
     log.info("user :"+user); 
     return "user"; 
    } 
} 

這是我的模型類 user.java

package com.myblog.model; 

import java.util.ArrayList; 
import java.util.List; 

public class User { 

    private int id; 
    private String name; 
    private List<Address> address=new ArrayList<Address>(); 

    @Override 
    public String toString() { 
     return "User [name=" + name + ", address=" + address + "]"; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<Address> getAddress() { 
     return address; 
    } 
    public void setAddress(List<Address> address) { 
     this.address = address; 
    } 


} 

address.java

package com.myblog.model; 


public class Address { 

    private int id; 
    private String street; 
    private String city; 

    public int getAddid() { 
     return addid; 
    } 
    public void setAddid(int addid) { 
     this.addid = addid; 
    } 
    public String getStreet() { 
     return street; 
    } 
    public void setStreet(String street) { 
     this.street = street; 
    } 
    public String getCity() { 
     return city; 
    } 
    public void setCity(String city) { 
     this.city = city; 
    } 

} 

這是我thymeleaf HTML頁面

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
<title>Page Title</title> 
<link href="/static/css/bootstrap.min.css" rel="stylesheet" media="screen" th:href="@{css/bootstrap.min.css}"/> 
<script src="/static/js/bootstrap.min.js" th:src="@{js/bootstrap.min.js}"></script> 
      </head> 
    <body> 
    <div class="container"> 
    <h1> User Account</h1> 
    <form class="form-horizontal" th:action="@{/user}" method="POST" th:object="${user}"> 
     <div class="form-group"> 
     <label for="inputDescription" class="col-sm-2 control-label">Full Name</label> 
     <div class="col-sm-5"> 
      <input type="text" class="form-control" name="name" placeholder="Full name"/> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="inputDescription" class="col-sm-2 control-label">Address</label> 
     <div class="col-sm-5"> 
      <input type="text" class="form-control" name="address.city" placeholder="City"/> 
     </div> 
     </div> 

    <div class="form-group"> 
      <label for="inputDescription" class="col-sm-2 control-label">Street</label> 
     <div class="col-sm-5"> 
        <input type="text" class="form-control" name="address.street" placeholder="street" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label class="col-sm-2 control-label"></label> 
      <div class="col-sm-10"> 
       <input type="submit" class="btn btn-primary" value="Add"/> 
      </div> 
     </div> 
    </form> 
    </div> 
    </body> 
    </html> 

控制器用戶對象不會將數據綁定到地址模型對象中。 輸出爲user:user [name =「name」,address []]

回答

1

您的表單似乎只管理一個地址,但您的域模型指定了一個地址列表。在這種情況下,您需要爲輸入字段命名,如地址[0] .street爲第一個地址,等等。

所以我會考慮使用thymleaf迭代器(th:each)使所有地址在您的窗體中可管理。然後使用th:field而不是定義名稱屬性。這應該可以解決你的問題。

+0

我嘗試 '日:場= 「* {} user.address.city」' 這是不工作 –

+0

謝謝ANSGAR日:場= 「地址[0] .street」 工作 –

+0

我不t完全知道thymleaf的語法,但你應該可以像'th:each =「anAddress:* {address}'做一些事情,然後在循環中使用'th:field =」$ {anAddress.street}「'例如,但我不完全確定。 –

0
<form class="form-horizontal" th:action="@{/user}" method="POST" th:object="${user}"> 
    <div class="form-group"> 
    <label for="inputDescription" class="col-sm-2 control-label">Full Name</label> 
    <div class="col-sm-5"> 
     <input type="text" class="form-control" th:field="*{name}" placeholder="Full name"/> 
    </div> 
    </div> 
    <div class="form-group"> 
    <label for="inputDescription" class="col-sm-2 control-label">Address</label> 
    <div class="col-sm-5"> 
     <input type="text" class="form-control" th:field="*{address[0].city}" placeholder="City"/> 
    </div> 
    </div> 

<div class="form-group"> 
    <label for="inputDescription" class="col-sm-2 control-label">Street</label> 
    <div class="col-sm-5"> 
     <input type="text" class="form-control" th:field="*{address[0].street}" placeholder="street" /> 
    </div> 
    </div> 
    <div class="form-group"> 
     <label class="col-sm-2 control-label"></label> 
     <div class="col-sm-10"> 
      <input type="submit" class="btn btn-primary" value="Add"/> 
     </div> 
    </div> 
</form> 
相關問題