【问题标题】:Spring+Thymeleaf: post data from modal formSpring+Thymeleaf:从模态表单发布数据
【发布时间】:2018-05-17 02:25:14
【问题描述】:

我需要将数据从 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">&times;</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:Bean 名称“类”的 BindingResult 和普通目标对象都不能用作请求属性。

我做错了什么?

编辑: 我的 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);
    }

【问题讨论】:

  • 你能发布你的 ClassService 课程吗?

标签: java spring-boot thymeleaf


【解决方案1】:

这里

 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();
}

【讨论】:

  • @Jack69:更新了我的答案,现在检查
猜你喜欢
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多