【问题标题】:Id is not set in form object Thymeleaf SpringId 未在表单对象 Thymeleaf Spring 中设置
【发布时间】:2015-07-03 03:43:01
【问题描述】:

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

这是在 Spring Server 中正确保存的“上一个表单”。

<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):

代码:

<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。 表单中的其余值被正确接收。 有谁知道我的错误在哪里?

【问题讨论】:

    标签: html spring forms jpa thymeleaf


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2017-04-26
      • 1970-01-01
      • 2016-07-13
      • 2020-11-13
      相关资源
      最近更新 更多