简体   繁体   English

在jsp页面中显示来自Controller MVC的异常消息

[英]Display exception's message from Controller MVC in the jsp page

StudentController - here is my controller class that returns exceptions and i want to handle and display these in jsp StudentController-这是我的控制器类,它返回异常,我想在jsp中处理和显示这些异常

 @RequestMapping(value = RequestURL.ADD_STUDENT, method = RequestMethod.POST)
        @ResponseBody
        public void addStudent(@RequestBody AddStudentRequest addStudentRequest) throws StudentGroupNumberNotFoundException, SpecializationNotFoundException {

            User user = new User(addStudentRequest.getUsername(),
                    addStudentRequest.getPassword(), Role.ROLE_STUDENT);
            userService.add(user);

            user = userService.findByUsername(addStudentRequest.getUsername());
            int userId = user.getId();
            try {
                int groupId = studentGroupService
                        .getIdByGroupNumber(addStudentRequest.getGroup());
                int specializationId = specializationService
                        .getIdByName(addStudentRequest.getSpecialization());
                Student student = new Student(userId, specializationId,
                        addStudentRequest.getName(),
                        addStudentRequest.getRegistrationNumber(), groupId,
                        addStudentRequest.getYear());
                studentService.add(student);
            } catch (StudentGroupNumberNotFoundException e) {
                throw new StudentGroupNumberNotFoundException(e.getMessage());
            } catch (SpecializationNotFoundException e) {
                throw new SpecializationNotFoundException (e.getMessage());
            }
        }

student.jsp - jsp page for student student.jsp-学生的jsp页面

    function addStudent() {

            var username = $('#modalStudentUsername').val();
            var password = $('#modalStudentPassword').val();
            var name = $('#modalStudentName').val();
            var registrationNumber = $('#modalStudentRegistrationNumber').val();
            var group = $('#modalStudentGroup').val();
            var year = $('#modalStudentYear').val();
            var specialization = $('#modalStudentSpecializationId').val();

            var data = '{ "username" : "' + username + '", "password": "'
            + password + '", "name":"' + name
            + '","registrationNumber": "' + registrationNumber + '" , "specialization": "' + specialization 
            + '","group": "' + group+'", "year": " ' + year + '" }';

            var token = $('#csrfToken').val();
            var header = $('#csrfHeader').val();

            $.ajax({
                        type : "POST",
                        url : "student/add",
                        contentType : 'application/json',
                        data : data,

                        beforeSend : function(xhr) {
                            xhr.setRequestHeader("Accept", "application/json");
                            xhr.setRequestHeader("Content-Type", "application/json");
                            xhr.setRequestHeader(header, token);
                        }, 

                        success : function(data, status, xhr) {
                            alert("Success!");
                        },
                        error : function(xhr, status, errorThrown) {
                            alert("Error!");
                        },
                    });
        }

I want to display in the alert from ajax the exception's message from controller. 我想在ajax的警报中显示来自控制器的异常消息。 Could anyone help me? 有人可以帮我吗? Thanks! 谢谢!

Change the method return type from void to String & give a call to xhr.responseText in alert() just like below:- 将方法的返回类型从void更改为Stringxhr.responseTextalert()调用xhr.responseText如下所示:

Change In Controller: 控制器变更:

@ResponseBody
public void addStudent(@RequestBody AddStudentRequest addStudentRequest) throws StudentGroupNumberNotFoundException, SpecializationNotFoundException {
     // business logic
}

to

@ResponseBody
public String addStudent(@RequestBody AddStudentRequest addStudentRequest) throws StudentGroupNumberNotFoundException, SpecializationNotFoundException {
     // business logic
}

Change In JavaScript: 更改JavaScript:

function addStudent() {
    // ...
    $.ajax({
        type : "POST",
        url : "student/add",
        contentType : 'application/json',
        data : data,
        beforeSend : function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader(header, token);
        },
        success : function(data, status, xhr) {
            alert(xhr.responseText);
        },
        error : function(xhr, status, errorThrown) {
            alert(xhr.responseText);
        },
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM