我的项目中的页面无法正常工作(Tomcat错误)

时间:2019-08-09 05:48:43

标签: spring spring-mvc spring-security

我的页面没有打开。在此页面上,我可以创建一个用户,并给他一个用户名和密码+角色(管理员或用户)。为什么页面不起作用。您可以看一下代码,看来好像写得正确。也许是我在代码中犯了一个错误。Tomcat给出了错误404。我只需要他打开此页面,即可看到控制器

enter image description here

管理控制器

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/allStudentsAdmin")
    public ModelAndView allStudentsForUser() {
        ModelAndView mv = new ModelAndView();
        List<Student> studentList = studentService.getAllStudents();
        mv.addObject("studentList", studentList);
        mv.setViewName("allStudentsAdmin");
        return mv;
    }

    @GetMapping(value = "/deleteStudent/{id}")
    public ModelAndView deleteUserById(@PathVariable Long id) {
        studentService.deleteStudentById(id);
        ModelAndView mv = new ModelAndView("redirect:/admin/allStudentsAdmin");
        return mv;
    }

    @GetMapping(value = "/editStudent/{id}")
    public ModelAndView displayEditUserForm(@PathVariable Long id) {
        ModelAndView mv = new ModelAndView("adminEditStudent");
        Student student = studentService.getStudentById(id);
        mv.addObject("headerMessage", "Редактирование студента");
        mv.addObject("student", student);
        return mv;
    }

    @PostMapping(value = "/editStudent")
    public String saveEditedUser(
            @RequestParam("id") Long id,
            @RequestParam("name") String name,
            @RequestParam("surname") String surname,
            @RequestParam("avatar") MultipartFile file) {
        try {
            studentService.updateStudent(name, surname, file, studentService.getStudentById(id));
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            return "redirect:/errors";
        }

        return "redirect:/admin/allStudentsAdmin";
    }

    @GetMapping(value = "/addStudentAdmin")
    public ModelAndView displayNewUserForm() {
        ModelAndView mv = new ModelAndView("addStudentAdmin");
        mv.addObject("headerMessage", "Add Student Details");
        mv.addObject("student", new Student());
        return mv;
    }

    @PostMapping(value = "/addStudentAdmin")
    public String saveNewStudent(@RequestParam("name") @NonNull String name,
            @RequestParam("surname") @NonNull String surname,
            @RequestParam("avatar") MultipartFile file)
            throws IOException {

        Student student = new Student();
        student.setSurname(surname);
        student.setName(name);

        if (file != null && !file.isEmpty()) {
            student.setAvatar(studentService.saveAvatarImage(file).getName());
        }
        studentService.saveStudent(student);
        return "redirect:/admin/allStudentsAdmin";
    }

      @GetMapping(value = "/addUser")
    public ModelAndView displayAddUserForm() {
        ModelAndView mv = new ModelAndView("addStudentAdmin");
        mv.addObject("headerMessage", "Add Student Details");
        mv.addObject("student", new Student());
        return mv;
    }

    @PostMapping(value = "/addUser")
    public String saveNewUser(@RequestParam("name") @NonNull String name,
            @RequestParam("surname") @NonNull String surname,
            @RequestParam("role") @NonNull String role)

            throws IOException {

        Student student = new Student();
        student.setSurname(surname);
        student.setName(name);


        studentService.saveStudent(student);
        return "redirect:/admin/allStudentsAdmin";
    }

}

管理员装饰器

<body>
  <div id="container">
    <div id="header">


    </div>
    <div id="nav">
        <ul>
            <li><a href="${pageContext.request.contextPath}/"><span>Главная</span></a></li>

            <li class="dropdown"><a href="${pageContext.request.contextPath}/allStudents"><span>Студенты</span></a>
                <ul>
                    <li><a href="${pageContext.request.contextPath}/allStudents"><span>Список студентов</span></a></li>
                    <sec:authorize access="hasRole('ADMIN') || hasRole('USER')">
                    <li><a href="${pageContext.request.contextPath}/addStudent"><span>Добавить студента</span></a></li>
                    </sec:authorize>
                    <sec:authorize access="hasRole('ADMIN')"> 
                    <li><a href="${pageContext.request.contextPath}/addUser"><span>Добавить юзера</span></a></li>
                    </sec:authorize>
                </ul>
            </li>
            <li><a><span>О нас </span></a></li> 
            <sec:authorize access="!isAuthenticated()"> 
            <li><a href="${pageContext.request.contextPath}/logout"><span>Выйти</span></a></li>
             </sec:authorize>
        </ul>
    </div>
</div>

        <sitemesh:write property='body'/>
    <jsp:include page="/WEB-INF/template/admintemplate.jsp"/>  
    </body>
</html>

AddUser.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="${pageContext.request.contextPath}/admin/addUser" enctype="multipart/form-data">
                    <table>

                        <tr>
                            <td><label path="Name">Name</label></td>
                            <td><input type="text" name="name"/></td>
                        </tr>

                        <tr>
                            <td><label path="Surname">Surname</label></td>
                            <td><input type="text" name="surname"/></td>
                        </tr>
                        <tr>
                            <td><select name="select" size="3" multiple>
                                    <option selected value="s1">Admin</option>
                                    <option value="s2">User</option>

   </select></td>
                            <td>
                                <input type="text" name="Role"/>
                            </td>

                        </tr>
                        <tr>
                            <td><input class="btn btn-primary" type="submit" value="Добавить"></td>
                        </tr>

                    </table>
                </form:form>
            </center>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

将URL更改为 admin / addUser

因为在您的控制器中添加了根级别 admin

@RequestMapping("/admin")

ex。http://localhost:8080/SchoolMaven/admin/addUser

相关问题