尝试在 Spring Boot 中更新时间表时出错
Posted
技术标签:
【中文标题】尝试在 Spring Boot 中更新时间表时出错【英文标题】:Error trying to update timeTable in spring boot 【发布时间】:2020-02-05 11:08:55 【问题描述】:我是 Spring Boot 广告的新手,目前正在从事时间表生成支持项目。添加时间表在我的系统中正常工作,现在我想更新时间表。但是,当我尝试加载 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
错误是语法错误并正确更新了文件。感谢所有试图提供帮助的人
【问题讨论】:
请将实际的完整堆栈跟踪添加到消息中,而不是 sn-p。这样做时,请将其包装在适当的代码标签中。 谢谢很多,但是这里太长了,但是可以看到它github.com/Ayesh17/attendance/blob/master/README.md 【参考方案1】:我看到了你的堆栈跟踪并找到了这条线
省略了 89 个常见框架 原因:java.lang.IllegalStateException:在 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.addAttribute("timeTableMapping", new TimeTableMapping());
【讨论】:
嘿,谢谢,但指定的 id 在数据库中,甚至在我添加了 modelAndView.addObject("timeTableMapping", new TimeTableMapping());它显示相同的错误 非常感谢,但问题是语法错误。但是,当我尝试加载upadteTimeTableView
时,它会在所有单元格中显示相同的主题。(因为我已将每个主题保存在唯一的行中,并且当我选择该行的 id 时,它会加载具有相同主题的时间表视图单元格。)你能告诉我如何为每个单元格加载正确的主题吗?所有行的 time_table_code 都相同
请参考此链接了解baeldung.com/thymeleaf-select-option
事情是当我选择更新时它传递一行的 id 并且它只加载该行的数据。所有字段都有一个公共列(time_table_code),我如何使用该公共列加载所有数据并将其传递给更新对象?以上是关于尝试在 Spring Boot 中更新时间表时出错的主要内容,如果未能解决你的问题,请参考以下文章
尝试将 Spring Boot 应用程序与 mongoDB 连接时出错
在 Spring Boot App 中启动 ApplicationContext 时出错
尝试运行基于 Maven 的简单 Spring Boot 项目时出错
在Spring Boot App中启动ApplicationContext时出错