Spring 异常处理例子


当前第2页 返回上一页

下面是 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.jar

  • org.springframework.asm-x.y.z.jar

  • org.springframework.beans-x.y.z.jar

  • org.springframework.context-x.y.z.jar

  • org.springframework.core-x.y.z.jar

  • org.springframework.expression-x.y.z.jar

  • org.springframework.web.servlet-x.y.z.jar

  • org.springframework.web-x.y.z.jar

  • spring-web.jar

一旦你完成了创建源代码和配置文件后,导出你的应用程序。右键单击你的应用程序,并且使用 Export > WAR File 选项,并且在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。

现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/SpringWeb/student。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:


输入如上图所示的值,然后单击提交按钮。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:




标签:Spring IoC

返回前面的内容

相关阅读 >>

spring 中 sql 的存储过程

spring 页面重定向例子

spring bean 生命周期

spring 注入集合

spring bean 后置处理器

spring @autowired 注释

spring 自动装配 byname

spring 由构造函数自动装配

spring 基于设值函数的依赖注入

spring 中的事件处理

更多相关阅读请进入《Spring IoC》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...