我是春季启动广告的新手,目前正在做一个时间表生成支持项目。添加时间表在我的系统中可以正常工作,现在我想更新时间表。但是,当我尝试加载upadteTimeTableView
时,显示错误
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/updateTimeTable.html]") Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "updateTimeTable" - line 18, col 46)
updateTimeTable
胸腺视图 <!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<link rel="stylesheet" type="text/css" href="static/css/timeTableMapping.css" th:href="@{/css/timeTableMapping.css}">
<meta charset="UTF-8">
<title>Update Time Table</title>
</head>
<body>
</form>
<div class="container2">
<form action="#" th:action="@{/timeTableMapping/saveAll}" th:object="${timeTableMapping}" method="post">
<table border="0" cell[adding="10">
<tr>
<td><h1>Time Table:</h1></td>
<td>
<select th:field="*{time_table_code}">
<option value="">Choose..</option>
<option th:each="timeTable: ${timeTables}" th:value="${timeTable.name}" th:text="${timeTable.name}"/>
</select>
</td>
</tr>
</table>
<table border="1" >
<thead>
<tr>
</tr>
<br>
<th></th>
<th>Monday</th>
<td style="background-color: cornflowerblue;"></td >
<td style="background-color: cornflowerblue;"></td>
<td style="background-color: cornflowerblue;"></td>
<th>Tuesday</th>
<td style="background-color: cornflowerblue;"></td >
<td style="background-color: cornflowerblue;"></td>
<td style="background-color: cornflowerblue;"></td>
<th>Wednesday</th>
<td style="background-color: cornflowerblue;"></td >
<td style="background-color: cornflowerblue;"></td>
<td style="background-color: cornflowerblue;"></td>
<th>Thursday</th>
<td style="background-color: cornflowerblue;"></td >
<td style="background-color: cornflowerblue;"></td>
<td style="background-color: cornflowerblue;"></td>
<th>Friday</th>
<td style="background-color: cornflowerblue;"></td >
<td style="background-color: cornflowerblue;"></td>
<td style="background-color: cornflowerblue;"></td>
</thead>
<tbody>
<th:block th:each="time : ${times}">
<tr>
<th th:text="${ time.start }+':00 - ' + ${ time.end }+':00'"></th>
<th:block th:each="day : ${days}">
<td>
<select th:field="*{subject_code}">
<option value=""></option>
<option th:each="subject: ${subjects}" th:value="${subject.subject_code}" th:text="${subject.name}"/>
<td><input type="text" th:value="${day.name}" name="day" style="display:none;width:0px" /></td>
<td><input type="text" th:value="${time.start}" name="start" style="display:none;width:0px" /></td>
<td><input type="text" th:value="${time.end}" name="end" style="display:none;width:0px" /></td>
</select>
</td>
</th:block>
</tr>
</th:block>
<tr>
<td colspan="2">
<button type="submit">Save</button>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
timeTableMapping
型号@Entity
@Table(name="timetablemappings")
@EntityListeners(AuditingEntityListener.class)
public class TimeTableMapping {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String day;
private String start;
private String end;
private String subject_code;
private String time_table_code;
public TimeTableMapping () {
}
public TimeTableMapping(String day, String start, String end, String subject_code, String time_table_code) {
this.day = day;
this.start = start;
this.end = end;
this.subject_code = subject_code;
this.time_table_code = time_table_code;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getSubject_code() {
return subject_code;
}
public void setSubject_code(String subject_code) {
this.subject_code = subject_code;
}
public String getTime_table_code() {
return time_table_code;
}
public void setTime_table_code(String time_table_code) {
this.time_table_code = time_table_code;
}
}
timeTableMapping
控制器@Controller
public class TimeTableMappingController {
@Autowired
private TimeTableMappingDAO timeTableMappingDAO;
@Autowired
private TimeTableDAO timeTableDAO;
@Autowired
private SubjectDAO subjectDAO;
@Autowired
private StudentGroupDAO studentGroupDAO;
@Autowired
private DayDAO dayDAO;
@Autowired
private TimeDAO timeDAO;
@RequestMapping("/timeTableMapping")
public String viewHomePage(Model model){
List<TimeTableMapping> timeTableMappingDetails= timeTableMappingDAO.findAll();
model.addAttribute("timeTableMappingDetails",timeTableMappingDetails);
return "timeTableMapping";
}
@RequestMapping(value="/timeTableMapping/saveAll",method= RequestMethod.POST)
public String saveTimeTable(@ModelAttribute("timeTableMapping") TimeTableMapping timeTableMapping){
String[] dayArray = timeTableMapping.getDay().split(",");
String[] subArray = timeTableMapping.getSubject_code().split(",");
String[] startArray = timeTableMapping.getStart().split(",");
String[] endArray = timeTableMapping.getEnd().split(",");
String[] uniqueDay = Arrays.stream(dayArray).distinct().toArray(String[]::new);
String[] uniqueStart = Arrays.stream(startArray).distinct().toArray(String[]::new);
String[] uniqueEnd = Arrays.stream(endArray).distinct().toArray(String[]::new);
List<TimeTableMapping> tempList = new ArrayList<>();
int count=0;
for(int j=0;j<uniqueStart.length;j++) {
for(int i = 0 ; i < uniqueDay.length; i++) {
TimeTableMapping tempTimeTable = new TimeTableMapping();
tempTimeTable.setStart(uniqueStart[j]);
System.out.println(uniqueStart[j]);
tempTimeTable.setEnd(uniqueEnd[j]);
tempTimeTable.setTime_table_code(timeTableMapping.getTime_table_code());
tempTimeTable.setDay(uniqueDay[i]);
tempTimeTable.setSubject_code(subArray[count]);
tempList.add(tempTimeTable);
count++;
}
}
timeTableMappingDAO.saveAll(tempList);
return "redirect:/timeTableMapping";
}
@RequestMapping("/timeTableMapping/new")
public String addTimeTableMapping(Model model){
System.out.println("hey");
System.out.println("hey");
TimeTableMapping timeTableMapping =new TimeTableMapping();
model.addAttribute("timeTableMapping",timeTableMapping);
List<TimeTable> timeTableDetail = timeTableDAO.findAll();
model.addAttribute("timeTables", timeTableDetail);
List<Subject> subjectDetail = subjectDAO.findAll();
model.addAttribute("subjects", subjectDetail);
List<Day> dayDetails = dayDAO.findAll();
model.addAttribute("days",dayDetails);
List<Time> timeDetails = timeDAO.findAll();
model.addAttribute("times",timeDetails);
return "addTimeTableMapping";
}
@RequestMapping("/timeTableMapping/edit/{id}")
public ModelAndView updateTimeTable(@PathVariable(name="id")Long id){
ModelAndView mav=new ModelAndView(("updateTimeTableMapping"));
//mav.addObject("timeTableMapping", new TimeTableMapping());
TimeTableMapping timeTableMapping = timeTableMappingDAO.findById(id);
mav.addObject("timeTableMapping",timeTableMapping);
List<TimeTable> timeTableDetails = timeTableDAO.findAll();
mav.addObject("timeTables", timeTableDetails);
List<Subject> subjectDetails = subjectDAO.findAll();
mav.addObject("subjects", subjectDetails);
List<StudentGroup> studentGroupDetails = studentGroupDAO.findAll();
mav.addObject("studentGroups",studentGroupDetails);
List<Day> dayDetails = dayDAO.findAll();
mav.addObject("days",dayDetails);
List<Time> timeDetails = timeDAO.findAll();
mav.addObject("times",timeDetails);
return mav;
}
@RequestMapping("/timeTableMapping/delete/{id}")
public String deleteProduct(@PathVariable(name="id") Long id){
timeTableDAO.delete(id);
return "redirect:/timeTable";
}
}
timeTableMappingDAO
@Service
public class TimeTableMappingDAO {
@Autowired
TimeTableRepository timeTableRepository;
@Autowired
TimeTableMappingRepository timeTableMappingRepository;
//to save a time table
public TimeTableMapping save(TimeTableMapping timeTableMapping){
return timeTableMappingRepository.save(timeTableMapping);
}
public void saveAll(List<TimeTableMapping> timeTableMapping){
timeTableMappingRepository.saveAll(timeTableMapping);
}
//to search all
public List<TimeTableMapping> findAll(){
return timeTableMappingRepository.findAll();
}
//get a machine by id
public TimeTableMapping findById(Long id){
return timeTableMappingRepository.findById(id).orElse(null);
}
//delete
public void delete(Long id){
timeTableMappingRepository.deleteById(id);
}
}
timeTableMapping
存储库@Repository
public interface TimeTableMappingRepository extends JpaRepository<TimeTableMapping, Long> {
}
这是要求
http://localhost:8080/timeTable/edit/919
更新1
错误是语法错误,已正确更新了文件。感谢所有尝试提供帮助的人
最佳答案
我已经看到了您的堆栈跟踪并找到了该行
省略了89个公共框架造成原因:java.lang.IllegalStateException:BeanResult'timeTableMapping'的BindingResult和普通目标对象都不能作为org.springframework.web.servlet.support.BindStatus。(BindStatus.java:153)上的请求属性使用[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
这里的问题是,您将对象绑定到表单上,即您没有通过控制器或没有传递空对象。也许指定的id在数据库中不可用,所以无论您从哪里调用此页面,都可以在其中添加一个空对象,如下所示
1)如果您使用的是ModelAndView类,则只需在modelAndView中添加一个空对象
modelAndView.addObject(“ timeTableMapping”,new TimeTableMapping());
2)如果您使用的是Model类,则只需在模型中添加一个空对象
model.addAttribute(“ timeTableMapping”,new TimeTableMapping());