Id未在表单对象Thymeleaf Spring中设置

时间:2015-04-24 13:43:47

标签: html spring forms jpa thymeleaf

我正在尝试使用Thymeleaf和Spring Server编辑以前填充的表单。

这是“以前的表单”,它是Spring服务器中的POST,可以正确保存。

<form id="addingnovoatributo" action="#" th:action="@{'/addingoextraattributes/'}" th:object="${new_point_attributes}" method="post">
    <p>Id do Attribute: <input type="text" th:field="*{id}" readonly="readonly"/></p>
    <p>Id do Point: <input type="text" th:value="${point.id}" id="pointid" name="pointid" readonly="readonly"/></p>
    <p>Nome do atributo: <input type="text" th:field="*{attribute_name}" /></p>
    <p>Valor do atributo: <input type="text" th:field="*{attribute_value}" /></p>
    <p><input type="submit" value="Submit" class="btn btn-lg btn-success"/> <input type="reset" value="Reset" class="btn btn-lg btn-warning"/></p>
</form>

Spring Server正确保存它(使用JPA CrudRepository)并为该对象(这是一个POJO类)提供唯一的ID:

@Entity
public class Point_attributes {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long id;
    public String attribute_name;
    public String attribute_value;
    public long pointid;

    public Point_attributes() {
        // TODO Auto-generated constructor stub
    }
    public long getPointid() {
        return pointid;
    }
    public void setPointid(long pointid) {
        this.pointid = pointid;
    }
    public String getAttribute_name() {
        return attribute_name;
    }
    public void setAttribute_name(String attribute_name) {
        this.attribute_name = attribute_name;
    }
    public String getAttribute_value() {
        return attribute_value;
    }
    public void setAttribute_value(String attribute_value) {
        this.attribute_value = attribute_value;
    }
    public long getId() {
        return id;
    }
}

当我想使用此表单编辑它时出现问题。 这是Spring Controller方法,它发送保存的填充对象:

@RequestMapping(value = "EditOneAttri/{id_survey}", method = RequestMethod.GET)
public ModelAndView EditOneAttr(@PathVariable("id_survey") long id_survey,@ModelAttribute Point_attributes mmPoint_attributes, Model model) 
{
    ModelAndView mModelAndView= new ModelAndView("editing_one_attri");
    mModelAndView.addObject("point_attributes",Points_attributesRepository.findById(id_survey));
    return mModelAndView;
}

我们可以看到ID不是0(editing_one_attri.html): enter image description here

代码:

<html>
<head>
<meta charset="UTF-8">
<title>Editing one Attribute</title>
          <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
     <!-- Versión compilada y comprimida del CSS de Bootstrap -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">

<!-- Tema opcional -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">

<!-- Versión compilada y comprimida del JavaScript de Bootstrap -->
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js">     </script>
</head>
<body th:inline="text">
  <div class="container theme-showcase"  role="main">
    <div class="jumbotron">
                <h2>Editing Attribute: [[${point_attributes.id}]]</h2>

                <form id="editing_question_survey" action="#" th:action="@{'/addingoextraattributes/'}" th:object="${point_attributes}" method="post">
                    <p>Id do Attribute: <input type="text" th:field="*{id}" readonly="readonly"/></p>
                    <p>Id do Point: <input type="text" th:field="*{pointid}" readonly="readonly"/></p>
                    <p>Nome do atributo: <input type="text" th:field="*{attribute_name}" /></p>
                    <p>Valor do atributo: <input type="text" th:field="*{attribute_value}" /></p>
                    <p><input type="submit" value="Submit" class="btn btn-lg btn-success"/> <input type="reset" value="Reset" class="btn btn-lg btn-warning"/></p>
            </form>
        </div>           
  </div>
</body>
</html>

Spring服务器接收对象但id始终为0。 表单中的其余值正确接收。 谁知道我的错误在哪里?

1 个答案:

答案 0 :(得分:4)

我解决了这个问题。主要的错误是我没有在ID中包含一个setter,因此thymeleaf和/或html表单无法在对象中设置ID。

因此,在POJO类中添加public void setId(){this.id=id;}解决了这个问题。