<td th:text="${task.id}"></td><td th:text="${task.name}"></td><td th:text="${task.description}"></td><td th:text="${task.dateCreated}"></td><td th:text="${task.finished}"></td><td><a th:href="@{'delete-task?id=' + ${task.id}}"><span class="glyphicon glyphicon-trash"></span></a></td></tr>这是我的任务实体的一部分
@Entity@Table(name = "T_TASKS")公共类任务实现可序列化{@Id@GeneratedValue(策略 = GenerationType.AUTO)@Column(name = "ID")私有整数 ID;@Column(name = "NAME")私人字符串名称;@Column(name = "DESCRIPTION")私人字符串描述;@Column(name = "DATE_CREATED")@Temporal(TemporalType.TIMESTAMP)私人日期日期创建;@Column(name = "FINISHED")私有布尔值完成;
这是松弛跟踪中的错误
java.lang.IllegalStateException: BindingResult 和 bean 名称task"的普通目标对象都不能用作请求属性
解决方案
更改控制器映射以接受 GET 和 POST 请求.您收到错误是因为 Spring 无法将 bean 绑定到表单.所以在调用GET时添加一个bean.
@GetMapping("/save-task")公共字符串 addTask(模型模型){任务 task = new Task();task.setDateCreated(new Date();model.addAttribute("task", task);返回保存任务";//或任何页面被调用}
然后进行处理:
@PostMapping("/save-task")public String saveTask(@ModelAttribute("task") 任务任务,BindingResult bindingResult,模型模型){taskService.save(task);model.addAttribute("mode", "MODE_TASKS");返回索引";}
你的豆子看起来不错.我没有看到您在 HTML 代码段中使用了 tasks
,但是如果您只想显示所有任务,那么它会出现在用 @GetMapping
映射的方法中.
I am trying to create a task creation page. There is a form where the user can enter the name, description and task status and save it. However, when I go to the page it shows me this error
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (index)
I also have a delete task in my program and that seems to work perfectly fine.
This is what my mainController looks like
@RequestMapping(value = "/save-task")
public String saveTask(@ModelAttribute("task") Task task, BindingResult bindingResult, Model model) {
task.setDateCreated(new Date());
taskService.save(task);
model.addAttribute("tasks", taskService.findAll());
model.addAttribute("task", new Task());
model.addAttribute("mode", "MODE_TASKS");
return "index";
}
@RequestMapping( value = "/delete-task")
public String deleteTask(@RequestParam (required = false) int id, Model model){
taskService.delete(id);
model.addAttribute("tasks", taskService.findAll());
model.addAttribute("mode", "MODE_TASKS");
return "index";
}
Here is the form
<div class="container text-center">
<h3>Manage Task</h3>
<hr/>
<form class="form-horizontal" method="POST" th:action="@{/save-task}" th:object="${task}">
<input type="hidden" name="id" th:field="*{id}"/>
<div class="form-group">
<label class="control-label col-md-3">Name</label>
<div class="col-md-7">
<input type="text" class="form-control" name="name" th:field="*{name}"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Description</label>
<div class="col-md-7">
<input type="text" class="form-control" name="description" th:field="*{description}"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Finished</label>
<div class="col-md-7">
<input type="radio" class="col-sm-1" th:name="finished" value="true"/>
<div class="col-sm-1">Yes</div>
<input type="radio" class="col-sm-1" th:name="finished" value="false"/>
<div class="col-sm-1">No</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Save"/>
</div>
</div>
</form>
</div>
</div>
Delete Task html
<tr th:each="task: ${tasks}">
<td th:text="${task.id}"></td>
<td th:text="${task.name}"></td>
<td th:text="${task.description}"></td>
<td th:text="${task.dateCreated}"></td>
<td th:text="${task.finished}"></td>
<td><a th:href="@{'delete-task?id=' + ${task.id}}"><span class="glyphicon glyphicon-trash"></span></a> </td>
</tr>
Here is a part of my Task entity
@Entity
@Table(name = "T_TASKS")
public class Task implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "DATE_CREATED")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "FINISHED")
private boolean finished;
Here is the error in the slack trace
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'task' available as request attribute
解决方案
Change your controller mapping to accept GET and POST requests. You're getting the error because Spring can't bind a bean to the form. So add a bean when GET is called.
@GetMapping("/save-task")
public String addTask(Model model) {
Task task = new Task();
task.setDateCreated(new Date();
model.addAttribute("task", task);
return "save-task"; //or whatever the page is called
}
Then do your processing:
@PostMapping("/save-task")
public String saveTask(@ModelAttribute("task") Task task,
BindingResult bindingResult,
Model model) {
taskService.save(task);
model.addAttribute("mode", "MODE_TASKS");
return "index";
}
Your bean looks fine. I don't see that you use tasks
in your HTML snippet, but that would go in the method mapped with @GetMapping
if you're looking to just display all tasks.
这篇关于Spring Boot,Thymeleaf 表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文