Мой код
Student Controller
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package adil.java.schoolmaven.controller; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import adil.java.schoolmaven.entity.Student; import adil.java.schoolmaven.service.StudentService; @Controller public class StudentController { // Constructor based Dependency Injection private StudentService studentService; public StudentController() { } @Autowired public StudentController(StudentService studentService) { this.studentService = studentService; } @RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET) public ModelAndView hello(HttpServletResponse response) throws IOException { ModelAndView mv = new ModelAndView(); mv.setViewName("index"); return mv; } // Get All Users @RequestMapping(value = "/allStudents", method = RequestMethod.POST) public ModelAndView displayAllUser() { System.out.println("User Page Requested : All Students"); ModelAndView mv = new ModelAndView(); List<Student> studentList = studentService.getAllStudents(); mv.addObject("studentList", studentList); mv.setViewName("allStudents"); return mv; } @RequestMapping(value = "/addStudent", method = RequestMethod.GET) public ModelAndView displayNewUserForm() { ModelAndView mv = new ModelAndView("addStudent"); mv.addObject("headerMessage", "Add Student Details"); mv.addObject("student", new Student()); return mv; } @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public ModelAndView saveNewStudent(@ModelAttribute Student student, BindingResult result) { ModelAndView mv = new ModelAndView("redirect:/index"); if (result.hasErrors()) { return new ModelAndView("error"); } boolean isAdded = studentService.saveStudent(student); if (isAdded) { mv.addObject("message", "New student successfully added"); } else { return new ModelAndView("error"); } return mv; } @RequestMapping(value = "/editStudent/{id}", method = RequestMethod.GET) public ModelAndView displayEditUserForm(@PathVariable Long id) { ModelAndView mv = new ModelAndView("/editStudent"); Student student = studentService.getStudentById(id); mv.addObject("headerMessage", "Редактирование студента"); mv.addObject("student", student); return mv; } @RequestMapping(value = "/editStudent/{id}", method = RequestMethod.POST) public ModelAndView saveEditedUser(@ModelAttribute Student student, BindingResult result) { ModelAndView mv = new ModelAndView("redirect:/index"); if (result.hasErrors()) { System.out.println(result.toString()); return new ModelAndView("error"); } boolean isSaved = studentService.saveStudent(student); if (!isSaved) { return new ModelAndView("error"); } return mv; } @RequestMapping(value = "/deleteStudent/{id}", method = RequestMethod.GET) public ModelAndView deleteUserById(@PathVariable Long id) { boolean isDeleted = studentService.deleteStudentById(id); System.out.println("Удаление студента: " + isDeleted); ModelAndView mv = new ModelAndView("redirect:/index"); return mv; } }
Add Student JSP
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> <title>Home</title> </head> <body> <div class="add"> <br> <br> <br> <br> <center> <h1>${headerMessage}</h1> <form:form method="POST" action="addStudent" modelAttribute="student" > <table> <tr> <td><form:label path="Name">Имя</form:label></td> <td><form:input path="Name"/></td> </tr> <tr> <td><form:label path="Surname">Фамилия</form:label></td> <td><form:input path="Surname"/></td> </tr> <tr> <td> <input name="data" type="file" accept="image/*" </td> </tr> <tr> <td><input class="btn btn-primary" type="submit" value="Добавить"></td> </tr> </table> </form:form> </center> </div> </body> </html>
AllStudent JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> <link href="../css/style.css" rel="stylesheet" type="text/css"> <style><%@include file="/WEB-INF/css/style.css"%></style> <title>Все студенты</title> </head> <body> <br> <br> <br> <br> <div class="it"> <h3>Список всех студентов</h3> ${message} <br> <br> <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Имя</th> <th scope="col">Фамилия</th> <th scope="col">Фото</th> </tr> </thead> <tbody> <c:forEach var="student" items="${studentList}"> <tr> <th scope="row">1</th> <td>${student.name}</td> <td>${student.surname}</td> <td>Должно быть изображение</td> <td><a href="${pageContext.request.contextPath}/editStudent/${student.id}"><button type="button" class="btn btn-primary">Редактировать</button> <td><a href="${pageContext.request.contextPath}/deleteStudent/${student.id}"><button type="button" class="btn btn-primary">Удалить</button> </tr> </c:forEach> </tbody> </table> <a href="${pageContext.request.contextPath}/addStudent"><button type="button" class="btn btn-primary">Добавить студента</button></a> </div> </body> </html>