“春季启动项目”为什么数据不在添加,更新和删除页面/按钮中执行正确的凝乳操作?”

发布于 2025-02-10 23:56:21 字数 8829 浏览 2 评论 0原文

“我的问题为什么数据不在添加,更新和删除页面/按钮中执行正确的凝结操作?” “为什么要在浏览器索引或AddNew中显示,更新文本,为什么不显示HTML页面?”

“这里看过几个类似的问题并将逻辑应用到我的情况下,我仍然在春季/JPA应用程序中因Crud操作失败而感到困惑。” #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();
}

susidenterviceimpl.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> 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>

当我将此URL粘贴到浏览器上时:localhost:8020/student/addNew,然后显示此文本“ newstudent”和 localhost:8020/student,然后显示此文本“索引”和url:localhost:8020/student/sape

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

"my question why not data perform proper CURD operation in add, update and delete page/button?"
"why to show in browser index or addnew ,update text, why not show html pages?"

"having looked at several similar problems here and applied the logic to my situation, I'm still stumped with a crud operation failure in my Spring/JPA app." #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>

When I paste this url on browser: localhost:8020/student/addnew then show the only this text "newstudent" and
when localhost:8020/student then show the only this text "index" and url: localhost:8020/student/save then show the text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2025-02-17 23:56:21

问题在您的控制器类中。

错误消息几乎说明了这一点。当您进入localhost:8020/student/save时,您会遇到该错误,因为/student/student/save没有获取映射。您只有发布映射。

可能的解决方案:

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

The problem is in your Controller class.

The error message pretty much says it. You're getting that error when you go to localhost:8020/student/save because there's no GET mapping for /student/save. You only have POST mapping.

Possible solution:

@GetMapping("/save)
public String saveStudent(Model model){
    model.addAttribute("student", new Student());
    //return the appropriate view;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文