Issue
i am creating simple spring boot application project. i have create two various controller those are student and course controller and Index controller is Index controller is a Main Page .when i ran the project index is loaded sucessfully.when i click the course link success visit to course page but course tables is not loaded and course page as link addnewcourse when i click that it will redirect to HTTP Status 404 – Not Found. i don't that what i was happing i attached the full source code on github link here https://github.com/raguram1986/SpringSecuritys
i attached the screenshot image below the folder structure.
Course Controller
@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:/";
}
}
Domain
@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
@Repository
public interface CourseRepository extends JpaRepository<Course, Long>{
}
Service
@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>
Solution
You have @RequestMapping("/Course")
annotation on CourseController
and in Course.html you trying to make request to /addcourse (i guess, please check in your browser development tool) but you need to make request to /Course/addcourse endpoint because of annotation on controller class. So u need to change <h3><a th:href="@{'/addcourse'}">Add new</a></h3>
to <h3><a th:href="@{'/Course/addcourse'}">Add new</a></h3>
Answered By - ARManakhov