当前位置:K88软件开发文章中心编程语言JavaSpring → 文章内容

Spring 异常处理例子

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:21:15

个异常视图,以便 SpringException 发生,如果有任何其他类型的异常发生,那么一个通用的视图 error 会发生。下面是 Spring 视图文件 student.jsp 的内容:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><html><head> <title>Spring MVC Exception Handling</title></head><body><h2>Student Information</h2><form:form method="POST" action="/HelloWeb/addStudent"> <table> <tr> <td><form:label path="name">Name</form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="age">Age</form:label></td> <td><form:input path="age" /></td> </tr> <tr> <td><form:label path="id">id</form:label></td> <td><form:input path="id" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form:form></body></html>下面是 Spring 视图文件 error.jsp 的内容:<html><head> <title>Spring Error Page</title></head><body><p>An error occured, please contact webmaster.</p></body></html>;下面是 Spring 视图文件 ExceptionPage.jsp 的内容。在这里,你将通过 ${exception} 访问异常实例。<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><html><head> <title>Spring MVC Exception Handling</title></head><body><h2>Spring MVC Exception Handling</h2><h3>${exception.exceptionMsg}</h3></body></html>下面是 Spring 视图文件 result.jsp 的内容:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><html><head> <title>Spring MVC Form Handling</title></head><body><h2>Submitted Student Information</h2> <table> <tr> <td>Name</td> <td>${name}</td> </tr> <tr> <td>Age</td> <td>${age}</td> </tr> <tr> <td>ID</td> <td>${id}</td> </tr> </table> </body></html>最后,下面是包含在你的 web 应用程序中的 Spring 和其他库的列表。你仅仅需要将这些文件拖拽到 WebContent/WEB-INF/lib 文件夹中。commons-logging-x.y.z.jarorg.springframework.asm-x.y.z.jarorg.springframework.beans-x.y.z.jarorg.springframework.context-x.y.z.jarorg.springframework.core-x.y.z.jarorg.springframework.expression-x.y.z.jarorg.springframework.web.servlet-x.y.z.jarorg.springframework.web-x.y.z.jarspring-web.jar一旦你完成了创建源代码和配置文件后,导出你的应用程序。右键单击你的应用程序,并且使用 Export > WAR File 选项,并且在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/SpringWeb/student。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:输入如上图所示的值,然后单击提交按钮。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

上一页  [1] [2] 


Spring 异常处理例子