2016-08-01 25 views
7

Tôi cần thêm ngày vào thực thể của mình và cho phép người dùng đặt nó ở dạng web. Trường này cần có ngày hôm nay được điền theo mặc định.Loại đầu vào = "ngày" thymeleaf

1. <input type="date" value="2016-08-01"> 

thấy đúng ngày setted cho mặc định

2. <input type="date" th:value="${startDate}"> 

cho thấy chọn ngày mà không cần bất kỳ giá trị (lưu ý: String STARTDATE = "2016/08/01";)

3. <input type="date" th:field="${startDate}"> 

tạo 400 lỗi (Yêu cầu không hợp lệ) (lưu ý: Ngày bắt đầuDate = new Date();)

Vì vậy, câu hỏi là: cách sử dụng thymeleaf cho ngày đầu vào ?

  • Tôi có thể sử dụng Date() kiểu dữ liệu để nhập và lưu trữ dữ liệu đó không?
  • cách tôi cần đặt "th: field" ở dạng?
  • cách tôi cần đặt "th: value" trong cùng một biểu mẫu?

điều khiển của tôi (s):

@RequestMapping("/project_new") 
public String createProject(Model model) { 
    Project project = new Project(); 
    List<Role> roles = mRoleService.findAll(); 

    project.setStart(new Date()); 

    model.addAttribute("page_title", "create project"); 
    model.addAttribute("roles", roles); 
    model.addAttribute("statuses", Status.values()); 
    model.addAttribute("project", project); 
    return "project_new"; 
} 

@RequestMapping(value = "/project_new", method = RequestMethod.POST) 
public String createProject(@ModelAttribute Project project, Model model) { 
    // Fill id field for project.rolesNeeded 
    mRoleService.setRolesId(project.getRolesNeeded()); 
    project.fixCollaboratorsAndRoles(); 

    mProjectService.save(project); 
    return "redirect:/"; 
} 

mẫu của tôi:

<form th:action="@{/project_new}" method="post" th:object="${project}"> 
    <div class="project-list single"> 
    <label for="name">Name:</label> 
    <input type="text" id="name" required="true" th:placeholder="*{name}" th:value="*{name}" th:field="*{name}"/> 
    <label for="description">Description:</label> 
    <textarea rows="5" id="description" type="text" required="true" th:placeholder="*{description}" th:value="*{description}" th:field="*{description}"/> 

    <label for="date-started">Date started:</label> 
    <input type="date" th:value="${project.start}" th:field="${project.start}" id="date-started"/> 

    <div> 
     <label for="project_status">Project Status:</label> 
     <div class="custom-select"> 
     <span class="dropdown-arrow"></span> 
     <select th:field="*{status}" id="project_status"> 
      <option th:each="s : ${statuses}" th:value="${s}" th:text="${s}">Active</option> 
     </select> 
     </div> 
    </div> 

    <div class="roles-collaborators"> 
    <ul class="checkbox-list"> 
     <li th:if="${role.name} ne 'Undefined'" th:each="role : ${roles}"> 
     <input type="checkbox" th:value="${role}" th:field="${project.rolesNeeded}" class="checkbox"/> 
     <span th:text="${role.name}" th:value="${role}" class="checkbox">Developer</span> 
     </li> 
    </ul> 
    </div> 

    <div class="actions"> 
    <button type="submit" class="button">Save</button> 
    <a th:href="@{/}" class="button button-secondary">Cancel</a> 
    </div> 
</div> 
</form> 

tổ chức dự án:

@Entity 
public class Project { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

@NotNull 
@Size (min = 3) 
private String name; 

@Column(columnDefinition="TEXT") 
private String description; 

@Column 
private Status status; 

@Column 
private Date start; 

@ManyToMany 
@LazyCollection(LazyCollectionOption.FALSE) 
private List<Role> rolesNeeded; 

@ManyToMany 
@LazyCollection(LazyCollectionOption.FALSE) 
private List<Collaborator> collaborators; 

public Date getStart() { 
    return start; 
} 

public void setStart(Date start) { 
    this.start = start; 
} 
+0

Bạn có thể chỉ cho chúng tôi các phương pháp điều khiển cho các hành động GET và POST của bạn và cũng là dạng thymeleaf không? – BitExodus

+0

Chắc chắn, hãy thêm nó vào câu hỏi – zzheads

+0

Vui lòng bao gồm phần tử "form" trong mẫu và lớp Project với thuộc tính "start" và phương thức get và set cho nó. – BitExodus

Trả lời

9

Lấy một cái nhìn tại các comment với lỗi đăng nhập nó dường như là sự cố chuyển đổi giữa Chuỗi đến java.util.Date. Sau khi tìm kiếm một lúc trong Thymeleaf GitHub tôi thấy hai vấn đề có thể giải thích cách tiến hành trong trường hợp này:

  • Thảo luận về việc chuyển đổi bao gồm ngày trong this issue.
  • Implementacion của chuyển đổi được giải thích here.

Từ quan điểm cuối cùng, tôi đã thêm một chú thích để ngày bắt đầu của lớp dự án của bạn:

// This is "org.springframework.format.annotation.DateTimeFormat" 
@DateTimeFormat(pattern = "yyyy-MM-dd") 
private Date start; 

Và sau đó, tôi đã có thể nhận được cập nhật trong phương pháp điều khiển POST của bạn.

Đi vào tài khoản bạn cũng cần phải thay đổi "thứ: giá trị" và "thứ: trường" thuộc tính từ mẫu của bạn cho giá trị ngày từ $ {} project.start-* {} bắt đầu như tôi đã viết trong các nhận xét như bạn đã làm cho các trường tênmô tả.

+1

Cảm ơn, nó hoạt động! (i có nghĩa là @DateTimeFormat). Không cần phải thay đổi $ {project.start} thành * {start}, vì nó đơn giản giống nhau, tôi chỉ thích cú pháp "tên đầy đủ" :) – zzheads

+1

Chỉ cần cẩn thận với cú pháp đầy đủ. Ví dụ, nếu bạn đang tái cấu trúc mã của mình và thay đổi tên biến "dự án", bạn cũng cần thay đổi "$ {project.start}" và tất cả các lần xuất hiện cú pháp đầy đủ khác; điều này sẽ không xảy ra nếu bạn sử dụng cách "* {field}". – BitExodus

Các vấn đề liên quan