java - 输入类型 ="date"thymeleaf

标签 java hibernate date input thymeleaf

我需要向我的实体添加日期,并让用户在 Web 表单中设置它。 默认情况下,此字段需要填写今天的日期。

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

显示为默认设置的正确日期

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

显示没有任何值的日期选择器(注意:String startDate = "2016-08-01";)

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

生成 400 错误(Bad request)(注意:Date startDate = new Date();)

那么,问题是:如何使用 thymeleaf 输入日期?

  • 我可以使用 Date() 数据类型来输入和存储此类数据吗?
  • 我需要如何在表单中设置“th:field”?
  • 如何以相同的形式设置“th:value”?

我的 Controller :

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

我的模板:

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

项目实体:

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

最佳答案

查看带有错误日志的注释,它似乎是 Stringjava.util.Date 之间的转换问题。在 Thymeleaf GitHub 中搜索了一段时间后,我看到了两个可以解释在这种情况下如何进行的问题:

  • 讨论转换,包括 this issue 中的日期.
  • 解释了转换的实现 here .

从最后一点开始,我给你的项目类的开始日期添加了一个注释:

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

在那之后,我能够在您的 Controller POST 方法中接收到日期。

考虑到您还需要更改模板中的 th:valueth:field 属性,以获取 ${project.start 中的日期值}*{start},就像我在评论中写的那样,正如您对 namedescription 字段所做的那样。

关于java - 输入类型 ="date"thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693971/

相关文章:

javascript - javascript 中的日期格式格式错误

sql - 将日期格式化为 yyyy-mm-dd hh :mm:ss. 000

mysql - 日历重复/重复事件 -> 查找日期范围内的事件

java - 从 server.xml 按名称读取变量

java - 字节数据类型的默认格式

Java计时器在您单击空格时启动,在再次单击空格后结束

java - 如何在 Hibernate 中设置 Sqlite 相对路径?

java - SQLGrammarexception,执行 native 查询时未找到列 'Id'

java - 在 Java 8 中,使用 wsimport 生成 java 类时缺少 MyServicePort.java 类

java - JPA - 为 SINGLE_TABLE 层次结构抽象类的两个子类分配不同的唯一约束