我有一个控制器类,并且已经调试了它。我在控制器类中添加了以下两行。

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类具有字段nameId<select>标记导致异常。如何解决?

最佳答案

将“值”属性添加到您的选项中,如下所示:

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


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

07-24 20:24