簡體   English   中英

“Spring Boot 項目為什么不對數據在添加、更新和刪除頁面/按鈕中執行正確的 CURD 操作?”

[英]"Spring Boot project why not data perform proper CURD operation in add, update and delete page/button ?"

“我的問題為什么數據不能在添加、更新和刪除頁面/按鈕中執行正確的 CURD 操作?” “為什么要在瀏覽器索引中顯示或添加新、更新文本,為什么不顯示 html 頁面?”

“在這里查看了幾個類似的問題並將邏輯應用於我的情況后,我仍然為我的 Spring/JPA 應用程序中的粗魯操作失敗感到難過。” #springbootproblem #support #help StudentMasterController.java

    package com.rinfotek.iiserp.controller;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.PutMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import com.rinfotek.iiserp.entity.StudentMaster;
    import com.rinfotek.iiserp.service.StudentMasterServiceImpl;
    @RestController
    @RequestMapping("/student")
    public class StudentMasterController {
    //  @Autowired
    //  StudentMasterService studentService;
        @Autowired
        private StudentMasterServiceImpl studentServiceImpl;
        //thymeleaf testing
         @PostMapping("/save")
        public String addStudent(@ModelAttribute("student")  StudentMaster student) {
            studentServiceImpl.save(student);
             return "redirect:/";
        }
        //thymeleaf testing
        @GetMapping("/")
        public String viewStudentMasterHomesPage(Model model) {
            model.addAttribute("allstudentlist", studentServiceImpl.getAllStudents());
            return "index";
        }
        @GetMapping("/addnew")
        public String addNewStudent(Model model) {
            StudentMaster student = new StudentMaster();
            model.addAttribute("student", student);
            return "newstudent";
        }
        //update student 
        @PutMapping("/updatestudent/{id}")
        public String updateForm(@PathVariable(value = "id") Integer id, Model model) {
            StudentMaster student = studentServiceImpl.getById(id);
            model.addAttribute("student", student);
            return "update";
        }
        //delete student 
        @DeleteMapping("/deleteStudent/{id}")
        public String deleteThroughId(@PathVariable(value = "id") Integer id) {
            studentServiceImpl.deleteViaId(id);
            return "redirect:/";
        }
    }




```StudentMasterService.java```
package com.rinfotek.iiserp.service;
import java.util.List;
import com.rinfotek.iiserp.entity.StudentMaster;
public interface StudentMasterService {
    StudentMaster addStudent(StudentMaster student);
    StudentMaster getStudentMasterById(Integer sId);
    void updateStudentMaster(StudentMaster student);
    void deleteStudentMasterById(Integer sId);
    List<StudentMaster> getAllStudents();
}

StudentServiceImpl.java

package com.rinfotek.iiserp.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import com.rinfotek.iiserp.entity.StudentMaster;
import com.rinfotek.iiserp.repository.StudentMasterRepository;
@Service
public  class StudentMasterServiceImpl implements StudentMasterService {
@Autowired
private StudentMasterRepository repository;
@Override
public StudentMaster addStudent(StudentMaster student) {
    return repository.save(student);
}
@Override
public StudentMaster getStudentMasterById(Integer sId) {
    return repository.findById(sId).get();
}
@Override
public List<StudentMaster> getAllStudents() {
    return repository.findAll();
}
public void save(StudentMaster student) {
    repository.save(student);
    
}
public StudentMaster getById(Integer sId)
{
    Optional<StudentMaster> optional = repository.findById(sId);
    StudentMaster student = null;
    if (optional.isPresent())
        student = optional.get();
    else
        throw new RuntimeException(
            "Student not found for id : " + sId);
    return student;
}
 public void deleteViaId(Integer sId)
{
     repository.deleteById(sId);
}
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Student</title>
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
    integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
    crossorigin="anonymous">
</head>
<body>
<div class="container my-2" align="center">
<h3>Student List</h3>
<a th:href="@{/student/addnew}" class="btn btn-primary btn-sm mb-3" >Add Student</a>
    <table style="width:80%" border="1"
           class = "table table-striped table-responsive-md">
    <thead>
  <tr>
    <th>Name</th>
    <th>Current Class</th>
    <th>Action</th>
  </tr>
  </thead>
  <tbody>
  <tr th:each="student:${allstudentlist}">
        <td th:text="${student.name}"></td>
        <td th:text="${student.currentClass}"></td>
        <td> <a th:href="@{/updatestudent/{id}(id=${student.id})}"
                class="btn btn-primary">Update</a>
                <a th:href="@{/deleteStudent/{id}(id=${student.id})}"
                class="btn btn-danger">Delete</a>
    </td>
  </tr>
  </tbody>
</table>
</div>
</body>
</html>

newstudent.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Student Management System</title>
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
    integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
    crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <h1>Student Management System</h1>
        <hr>
        <h2>Save Student</h2>
        <form action="#" th:action="@{/student/save}" th:object="${student}"
            method="POST">
            <input type="text" th:field="*{name}" placeholder="Student Name"
                class="form-control mb-4 col-4"> <input type="text"
                th:field="*{currentClass}" placeholder="Current Class"
                class="form-control mb-4 col-4">
            <button type="submit" class="btn btn-info col-2">Save
                Student</button>
        </form>
        <hr>
        <a th:href="@{/student/}"> Back to Student List</a>
    </div>
</body>
</html>

update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Student Management System</title>
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>Student Management System</h1>
        <hr>
        <h2>Update Student</h2>
        <form action="#" th:action="@{/student/save}" th:object="${student}"
            method="POST">
            <!-- Add hidden form field to handle update -->
            <input type="hidden" th:field="*{id}" />
            <input type="text" th:field="*{name}" class="form-control mb-4 col-4">              
                <input type="text" th:field="*{currentClass}" class="form-control mb-4 col-4">
                <button type="submit" class="btn btn-info col-2"> Update Student</button>
        </form>
        <hr>
        <a th:href = "@{/student/}"> Back to Student List</a>
    </div>
</body>
</html>

當我將此網址粘貼到瀏覽器上時: localhost:8020/student/addnew然后僅顯示此文本“newstudent”,當localhost:8020/student則僅顯示此文本“索引”和 url: localhost:8020/student/save然后顯示文本

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

問題出在您的 Controller 類中。

錯誤消息幾乎說明了這一點。 當您轉到localhost:8020/student/save時,您會收到該錯誤,因為 /student /student/save save 沒有 GET 映射。 您只有 POST 映射。

可能的解決方案:

@GetMapping("/save)
public String saveStudent(Model model){
    model.addAttribute("student", new Student());
    //return the appropriate view;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM