当前位置: 首页 > 知识库问答 >
问题:

无法转换“java”类型的值。lang.String'转换为所需类型“long”;对于输入字符串:“{id}”

许博
2023-03-14

你好,我正在用JavaSpring Boot做一个“CRUD网络应用程序”,我正试图将我的“CRUD网络应用程序”按钮功能从一个新页面更改为一个模态,我遇到了问题。

首先,当我单击“编辑”按钮时,我的表单没有填充它从数据库获得的数据,第二,当我单击“保存”时,它会向我显示此消息。

无法转换“java”类型的值。lang.String'转换为所需类型“long”;嵌套的异常是java。lang.NumberFormatException:用于输入字符串“{id}”

我的JavaScript代码:

/** Modal buttons */
$(document).ready(function () {
  /** Modal edit button */
  $('.table .edit').on('click', function(event) {
    event.preventDefault();
      
    var href = $(this).attr('href');

    $.get(href, function (student) {
      $('#idEdit').val(student.id);
      $('#firstNameEdit').val(student.firstName);
      $('#lastNameEdit').val(student.lastName);
      $('#emailEdit').val(student.email);
      $('#numberEdit').val(student.number);
      });

  $('#editStudentModal').modal();
  });

  /** Modal delete button */
  $('.table .delete').on('click', function(event) {
    event.preventDefault();
      
    var href = $(this).attr('href');

    $.get(href, function (student) {
      $('#idEdit').val(student.id);
      $('#firstNameEdit').val(student.firstName);
      $('#lastNameEdit').val(student.lastName);
      $('#emailEdit').val(student.email);
      $('#numberEdit').val(student.number);
    });

    $('#deleteStudentModal').modal();
  });
});

“编辑”按钮所在表格的html代码:

<table class="table table-striped table-hover">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Email</th>
      <th>Phone</th>
      <th sec:authorize="isAuthenticated()">Actions</th>
      </tr>
  </thead>
  <tbody>
    <tr th:each="student : ${SE_StudentList}">
     <td th:text="${student.firstName}"></td>
     <td th:text="${student.lastName}"></td>
     <td th:text="${student.email}"></td>
     <td th:text="${student.number}"></td>
     <td sec:authorize="isAuthenticated()">
        <a th:href="@{/showFormForUpdate_SE/{id}(id=${student.id})}" class="edit"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
        <a th:href="@{/deleteStudent_SE/{id}(id=${student.id})}" class="delete"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
     </td>
    </tr>
  </tbody>
</table>

设计模式表单的html代码:

<!-- Edit Modal HTML -->
    <div id="editStudentModal" class="modal fade">
        <form th:action="@{/showFormForUpdate_SE/{id}}" method="put">
            <!-- Hidden field to handle update -->
            <input type="hidden" id="idEdit" name="id"/>
            <div class="modal-dialog">
                <div class="modal-content">
                        <div class="modal-header">                      
                            <h4 class="modal-title">Edit Student</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        </div>
                        <div class="modal-body">                    
                            <div class="form-group">
                                <label>First Name</label>
                                <input type="text" class="form-control" id="firstNameEdit" name="firstName" required>
                            </div>
                            <div class="form-group">
                                <label>Last Name</label>
                                <input type="text" class="form-control" id="lastNameEdit" name="lastName" required>
                            </div>
                            <div class="form-group">
                                <label>Email</label>
                                <input type="email" class="form-control" id="emailEdit" name="email" required>
                            </div>
                            <div class="form-group">
                                <label>Phone</label>
                                <input type="text" class="form-control" id="numberEdit" name="number" required>
                            </div>                  
                        </div>
                        <div class="modal-footer">
                            <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                            <input type="submit" class="btn btn-info" value="Save">
                        </div>
                </div>
            </div>
        </form>
    </div>

这是我在控制器中更新的代码

// Update Software Engineering Student
   @GetMapping("/showFormForUpdate_SE/{id}")
   public String showFormForUpdate_SE(@PathVariable (value = "id") long id, Model model){
       SoftwareEngineering softwareEngineering = se_service.getSEStudentById(id);
       model.addAttribute("SE_Student", softwareEngineering);
       se_service.saveSEStudent(softwareEngineering);
       return "redirect:/softwareEngineering";
   }

这是我为getSEStudentById提供的服务

@Override
    public SoftwareEngineering getSEStudentById(long id) {
        Optional<SoftwareEngineering> optional = se_repository.findById(id);
        SoftwareEngineering softwareEngineering = null;
        if (optional.isPresent()) {
            softwareEngineering = optional.get();
        } else {
            throw new RuntimeException("Student not found for id ::" + id);
        }
        return softwareEngineering;
    }

共有1个答案

孙俊彦
2023-03-14

我必须做的第一件事是,将getId方法与update方法分开。

原因很简单,我试图用GET方法(这是正确的方法)获取Id,但是当我保存(学生)时,我使用了PUT方法(用于表单),但在另一方面,控制器中的EndPoint是GetMap方法,所以我必须为getStuentId()创建一个新的EndPoint,并将更新Endpoint更改为请求映射并调用两个方法有PUT和GET。

// Get Software Engineering Student By Id
   @GetMapping("/getOneSEStudent")
   @ResponseBody
   public SoftwareEngineering getOne(long id){
       SoftwareEngineering softwareEngineering = se_service.getSEStudentById(id);
       return softwareEngineering;
   }
// Update Software Engineering Student
   @RequestMapping(value = "/updateSEStudent", method = {RequestMethod.PUT, RequestMethod.GET})
   public String showFormForUpdate_SE(SoftwareEngineering softwareEngineering){
       se_service.saveSEStudent(softwareEngineering);
       return "redirect:/softwareEngineering";
   }
 类似资料: