ThymeLeaf Select标签异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThymeLeaf Select标签异常相关的知识,希望对你有一定的参考价值。

我有一个控制器类,我已经调试过它。我在控制器类中添加了如下所示的2行。

model.addAttribute("employees",employees);
model.addAttribute("visitorForm",visitorForm);

我已经看到两个对象都存在并正确填充。

当涉及到页面时,它如下所示。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Enter Visitor Details</title>
</head>
<body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container"></div>
<form action="#" th:action="@{/enterVisitor}" th:object="${visitorForm}" method="post">
        <p>Name: <input type="text" th:field="*{name}" required="required"/></p>
        <p>Address: <input type="text" th:field="*{address}" required="required"/></p>
        <p>Mobile: <input type="text" th:field="*{mobileNumber}" size="10" required="required" maxlength="10"/></p>
        <p>Email: <input type="text" th:field="*{email}" /></p>
        <p>Purpose: <input type="text" th:field="*{purposeOfvisit}" size="20" required="required"/></p>
        <p>In Time:<input type="number" id="inTimee" name="inTimee" contenteditable="false" readonly="readonly"/></p>
        <script type="text/javascript">document.getElementById("inTimee").value=new Date().getTime()</script>
        <p>Employee to Meet:  <select th:field="${visitorForm.id}">
                         <!-- <option text=""><option> -->
                         <option th:each="emp:${employees}" 
                            th:value="${emp.id}"
                            th:text="${emp.name}"/>
                  </select>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
    </div>
    <script type="text/javascript">
    function getDateTime(){
        var x = document.getElementById("inTimee").value=new Date().getTime();
    }
    </script>
</body>
</html>

我得到一个例外说法。

org.thymeleaf.exceptions.TemplateProcessingException: Attribute "value" is required in "option" tags (template: "visitor/enterVisitor" - line 20, col 26)
    at org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor.doProcess(SpringOptionFieldTagProcessor.java:63) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:187) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]

这是访客表单类。

package com.ahmed.visitorapp.forms;

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

import com.ahmed.visitorapp.model.Employee;

public class VisitorForm {
String id;
String name;
String address;
String mobileNumber;
String email;
String purposeOfvisit;
List<Employee> employees;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getMobileNumber() {
    return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getPurposeOfvisit() {
    return purposeOfvisit;
}
public void setPurposeOfvisit(String purposeOfvisit) {
    this.purposeOfvisit = purposeOfvisit;
}
public List<Employee> getEmployees() {
    return employees;
}
public void setEmployees(List<Employee> employees) {
    this.employees = employees;
}
public void setEmployees(Iterable<Employee> employees) {
    if(this.employees==null) {
        this.employees =  new ArrayList<>();
    }
    for(Employee emp:employees) {
        this.employees.add(emp);
    }
}
}

Employee类具有成员名称和Id。请指导thanks.Select标签给出例外。

答案

在您的选项中添加“value”属性,如下所示:

<option th:each="emp:${employees}" th:value="${emp.id}" th:text="${emp.name}" value=""/>

有关更多详细信息,请参阅this

以上是关于ThymeLeaf Select标签异常的主要内容,如果未能解决你的问题,请参考以下文章

Thymeleaf <select> 标签生成选项(数字)

thymeleaf-extras-db 0.0.1发布,select标签加载数据的新姿势

thymeleaf引入公共页面的某个片段

用thymeleaf用标签给页面select下拉框赋值怎么实现

springboot 学习 —— springboot 2.x ,thymeleaf 自定义 select 标签

springboot 学习 —— springboot 2.x ,thymeleaf 自定义 select 标签