Table 未使用 Spring 引导加载

Table is not loaded using Spring boot

我正在创建简单的 spring 引导应用程序项目。我创建了两个不同的控制器,它们是学生和课程控制器,索引控制器是索引控制器是主页。当我 运行 加载项目索引时 sucessfully.when 我单击课程 link 成功访问课程页面,但未加载课程表,课程页面为 link addnewcourse 当我单击它将重定向到 HTTP 状态 404 – 未找到 。我不是很高兴我在 github link 上附上了完整的源代码 https://github.com/raguram1986/SpringSecuritys 我在文件夹结构下方附上了屏幕截图。

课程管理员

 @Controller
    @RequestMapping("/Course")
    
    public class CourseController {
        
         @Autowired
            private CourseService service;
    
           @GetMapping("/")
            public String viewHomePage(Model model) {
                List<Course> listcourse = service.listAll();
                model.addAttribute("listcourse", listcourse);
                System.out.print("Get / "); 
                return "course";
            }
    
            @GetMapping("/addcourse")
            public String add(Model model) {
                List<Course> listcourse = service.listAll();
                model.addAttribute("listcourse", listcourse);
                model.addAttribute("course", new Course());
                return "addcourse";
            }
            
    
            @RequestMapping(value = "/save", method = RequestMethod.POST)
            public String saveCourse(@ModelAttribute("course") Course course) {
                service.save(course);
                return "redirect:/";
            }
    
            @RequestMapping("/edit/{id}")
            public ModelAndView showEditCoursePage(@PathVariable(name = "id") int id) {
                ModelAndView mav = new ModelAndView("addcourse");
                Course course = service.get(id);
                mav.addObject("course", course);
                return mav;
                
            }
            @RequestMapping("/delete/{id}")
            public String deleteCoursePage(@PathVariable(name = "id") int id) {
                service.delete(id);
                return "redirect:/";
            }
    }

@Entity
public class Course {
    
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String coursename;
    private int duration;

    public Course()
    {       
        
    }
 
    public Course(Long id, String coursename, int duration) {
        
        this.id = id;
        this.coursename = coursename;
        this.duration = duration;
    }
    

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCoursename() {
        return coursename;
    }
    public void setCoursename(String coursename) {
        this.coursename = coursename;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }


    @Override
    public String toString() {
        return "Course [id=" + id + ", coursename=" + coursename + ", duration=" + duration + "]";
    }
  

}

存储库

@Repository
public interface CourseRepository extends JpaRepository<Course, Long>{

}

服务

@Service
public class CourseService 
{
    @Autowired
    private CourseRepository repo;
    
    public List<Course> listAll() {
        return repo.findAll();
    }
     
    public void save(Course course) {
        repo.save(course);
    }
     
    public Course get(long id) {
        return repo.findById(id).get();
    }
     
    public void delete(long id) {
        repo.deleteById(id);
    }

}

Course.html

<div>
    <h2 >Course Creation</h2>
    <tr>
        <div align = "left" >
           
             <h3><a  th:href="@{'/addcourse'}">Add new</a></h3>  
           
        </div>
    
    </tr>

    <div class="col-sm-8" align = "center">
    <div class="panel-body" align = "center" >
                 
                 
  <table class="table">
  <thead class="thead-dark">
    <tr>
            <th>Course ID</th>
            <th>Course Name</th>
            <th>Duration</th>
             <th>edit</th>
             <th>delete</th>
    </tr>
  </thead>
  <tbody>
      <tr  th:each="course : ${listcourse}">
        <td th:text="${course.id}">Course ID</td>
        <td th:text="${course.coursename}">Course Name</td>
        <td th:text="${course.duration}">Duration</td>

        <td>
            <a th:href="@{'/edit/' + ${course.id}}">Edit</a>
        </td>                               
        <td>
            <a th:href="@{'/delete/' + ${course.id}}">Delete</a>
        </td>           
        </tr> 
   
  </tbody>
</table>

                    
                 </div>

            </div> 

    </tr>

    </tbody>
    </table>
    <div>

您在 CourseController 和 Course.html 中有 @RequestMapping("/Course") 注释,您试图向 /addcourse 发出请求(我想,请检查您的浏览器开发工具),但您需要由于控制器 class 上的注释,向 /Course/addcourse 端点发出请求。所以你需要将 <h3><a th:href="@{'/addcourse'}">Add new</a></h3> 更改为 <h3><a th:href="@{'/Course/addcourse'}">Add new</a></h3>