我需要将数据从html表单发布到数据库中。我有springboot和thymeleaf的gradle项目。 我有java对象类女巫有id,名字,描述,老师和分钟。 在HTML中,我使用模态形式来询问除id之外的所有内容。
main.html中
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Class Scheduler</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="webjars/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"/>
<script type="text/javascript" src="webjars/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
<h2>Class Scheduler</h2>
<div class="text-right">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#addClassModal">Add new class</button>
</div>
<div class="modal fade" id="addClassModal" tabindex="-1" role="dialog" aria-labelledby="addClassModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addClassModalLabel">Add new class</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form th:action="@{/addClass}" th:object="${class}" method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" th:value="*{name}"/>
</div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" class="form-control" id="description" th:value="*{description}"/>
</div>
<div class="form-group">
<label for="teacher">Teacher:</label>
<input type="text" class="form-control" id="teacher" th:value="*{teacherName}"/>
</div>
<div class="form-group">
<label for="minutes">Minutes:</label>
<input type="number" class="form-control" id="minutes" th:value="*{timeMinutes}"/>
</div>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我的控制器看起来像这样:
@Controller
public class ScheduleController {
@Autowired
private ClassService classService;
@Autowired
private StudentService studentService;
@RequestMapping(value = "/addClass", method = RequestMethod.POST)
public String addClass(@RequestAttribute("class") Class newClass, Model model) {
classService.addClass(newClass);
return "main";
}
我收到的错误消息是: java.lang.IllegalStateException:无法将BindingResult和bean名称“class”的普通目标对象用作请求属性。
我做错了什么?
编辑: 我的ClassService看起来像这样:
@Service
public class ClassService{
private static final BeanPropertyRowMapper<Class> CLASS_ROW_MAPPER = BeanPropertyRowMapper.newInstance( Class.class );
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void addClass(final Class newClass) {
MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("id", newClass.getId());
in.addValue("name", newClass.getName());
in.addValue("description", newClass.getDescription());
in.addValue("teacher_name", newClass.getTeacherName());
in.addValue("time_minutes", newClass.getTimeMinutes());
namedParameterJdbcTemplate.update("INSERT INTO class(name, description, teacher_name, time_minutes) values(:name,:description,:teacher_name,:time_minutes)", in);
}
答案 0 :(得分:0)
这里
public String addClass(@RequestAttribute("class") Class newClass, Model model) {
使用ModelAttribute而不是RequestAttribute
public String addClass( @ModelAttribute("class") Class newClass, Model model) {
此外,在您的控制器类中,返回与ModelAttribute
关联的Class实例@ModelAttribute(value = "class")
public Class getClass()
{
return new Class();
}