博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC异常处理实例
阅读量:7003 次
发布时间:2019-06-27

本文共 6696 字,大约阅读时间需要 22 分钟。

以下内容引用自:

例子:

pom.xml:

4.0.0
com.jsoft.testspring
testexception
war
0.0.1-SNAPSHOT
testexception Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-web
4.1.4.RELEASE
org.springframework
spring-webmvc
4.1.4.RELEASE
testexception
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
org.eclipse.jetty
jetty-maven-plugin
9.4.3.v20170317

web.xml:

Spring MVC Application
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-context.xml
1
spring-mvc
/

springmvc-context.xml:

/WEB-INF/jsp/
.jsp
ExceptionPage

说明:指定ExceptionPage作为一个异常视图,当SpringException发生时触发,另外,如果有任何其他类型的异常发生,通用的视图error就会触发。

Student.java:

package com.jsoft.testspring.testexception;public class Student {    private Integer age;    private String name;    private Integer id;    public void setAge(Integer age) {        this.age = age;    }    public Integer getAge() {        return age;    }    public void setName(String name) {        this.name = name;    }    public String getName() {        return name;    }    public void setId(Integer id) {        this.id = id;    }    public Integer getId() {        return id;    }}

SpringException.java:

package com.jsoft.testspring.testexception;public class SpringException extends RuntimeException {    private String exceptionMsg;    public SpringException(String exceptionMsg) {        this.exceptionMsg = exceptionMsg;    }    public String getExceptionMsg() {        return this.exceptionMsg;    }    public void setExceptionMsg(String exceptionMsg) {        this.exceptionMsg = exceptionMsg;    }}

StudentController.java:

package com.jsoft.testspring.testexception;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class StudentController {        @RequestMapping(value = "/student", method = RequestMethod.GET)    public ModelAndView student() {        return new ModelAndView("student", "command", new Student());    }    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)    @ExceptionHandler({ SpringException.class })    public String addStudent(Student student, ModelMap model) {        if (student.getName().length() < 5) {            throw new SpringException("Given name is too short");        } else {            model.addAttribute("name", student.getName());        }        if (student.getAge() < 10) {            throw new SpringException("Given age is too low");        } else {            model.addAttribute("age", student.getAge());        }        model.addAttribute("id", student.getId());        return "result";    }}

student.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    Spring MVC Exception Handling

Student Information

Name
Age
id

result.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    Spring MVC Form Handling

Submitted Student Information

Name ${name}
Age ${age}
ID ${id}

ExceptionPage.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    Spring MVC Exception Handling

Spring MVC Exception Handling

${exception.exceptionMsg}

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    Spring Error Page

An error occured, please contact webmaster.

 

测试工程:

==>如有问题,请联系我:easonjim#163.com,或者下方发表评论。<==

转载地址:http://nzgvl.baihongyu.com/

你可能感兴趣的文章
opencv python 基于KNN的手写体识别
查看>>
能在编码时做的事,就不要推迟到运行时
查看>>
【Chrome】对ios-safari移动端的H5页面进行调试(ios-webkit-debug-proxy)
查看>>
JavaScript之变量提升
查看>>
简单实现 ES6 Promise
查看>>
配置mail使用SMTP发送邮件
查看>>
个人服务器常用基础配置
查看>>
前端进阶系列-目录
查看>>
<Solidity学习系列二>深入理解Solidity之二---Solidity源代码文件结构
查看>>
基于bluestore的rocksdb的调优,测试ceph-4K-randwrite性能
查看>>
NodeJs系列之package.json
查看>>
gorose orm+dotweb框架快速构建go web网站实战(五)
查看>>
使用DOM Breakpoints找到修改属性的Javascript代码
查看>>
创建一个离线优先,数据驱动的渐进式 Web 应用程序
查看>>
使用机器学习预测天气(第二部分)
查看>>
解决使用jwt刷新token带来的问题
查看>>
浅谈分布式存储系统Pangu2.0——它让双11运维变得智能起来
查看>>
通过Apache Flume向HDFS存储数据
查看>>
像盖房子一样写代码:当我以测试驱动开发的时候,我在想些什么
查看>>
html5使用Drag事件编辑器拖拽上传图片
查看>>