课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 异常处理例子

当前位置:免费教程 » Java相关 » Spring

下面的例子说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的应用程序,它可以处理它的内置控制器产生的一个或多个异常。为了开始使用它,让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态的基于表单的 Web 应用程序:

步骤 描述
1 创建一个名称为 HelloWeb动态 Web 项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint
2 将上面提到的 Spring 和其他库拖拽到文件夹 WebContent/WEB-INF/lib 中。
3 com.tutorialspoint 包下创建一个 Java 类 StudentStudentControllerSpringException
4 WebContent/WEB-INF 文件夹下创建 Spring 的配置文件 Web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF 文件夹下创建名称为 jsp 的子文件夹。在这个子文件夹下创建视图文件 student.jspresult.jsperror.jspExceptionPage.jsp
6 最后一步是创建所有的源代码和配置文件的内容,并导出该应用程序,正如下面解释的一样。

这里是 Student.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class Student {
  3. private Integer age;
  4. private String name;
  5. private Integer id;
  6. public void setAge(Integer age) {
  7. this.age = age;
  8. }
  9. public Integer getAge() {
  10. return age;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public Integer getId() {
  22. return id;
  23. }
  24. }

下面是 SpringException.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class SpringException extends RuntimeException{
  3. private String exceptionMsg;
  4. public SpringException(String exceptionMsg) {
  5. this.exceptionMsg = exceptionMsg;
  6. }
  7. public String getExceptionMsg(){
  8. return this.exceptionMsg;
  9. }
  10. public void setExceptionMsg(String exceptionMsg) {
  11. this.exceptionMsg = exceptionMsg;
  12. }
  13. }

下面是 StudentController.java 文件的内容。这里,你需要使用 @ExceptionHandler 注解一个 service 方法,你可以指定要处理的一个或多个异常。如果你要指定一个以上的异常,那么你可以使用逗号分隔这些值。

  1. package com.tutorialspoint;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.ExceptionHandler;
  4. import org.springframework.web.bind.annotation.ModelAttribute;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.servlet.ModelAndView;
  8. import org.springframework.ui.ModelMap;
  9. @Controller
  10. public class StudentController {
  11. @RequestMapping(value = "/student", method = RequestMethod.GET)
  12. public ModelAndView student() {
  13. return new ModelAndView("student", "command", new Student());
  14. }
  15. @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
  16. @ExceptionHandler({SpringException.class})
  17. public String addStudent( @ModelAttribute("HelloWeb")Student student,
  18. ModelMap model) {
  19. if(student.getName().length() < 5 ){
  20. throw new SpringException("Given name is too short");
  21. }else{
  22. model.addAttribute("name", student.getName());
  23. }
  24. if( student.getAge() < 10 ){
  25. throw new SpringException("Given age is too low");
  26. }else{
  27. model.addAttribute("age", student.getAge());
  28. }
  29. model.addAttribute("id", student.getId());
  30. return "result";
  31. }
  32. }

下面是 Spring Web 配置文件 web.xml 的内容

  1. <web-app id="WebApp_ID" version="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6.  
  7. <display-name>Spring Exception Handling</display-name>
  8.  
  9. <servlet>
  10. <servlet-name>HelloWeb</servlet-name>
  11. <servlet-class>
  12. org.springframework.web.servlet.DispatcherServlet
  13. </servlet-class>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16.  
  17. <servlet-mapping>
  18. <servlet-name>HelloWeb</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21.  
  22. </web-app>

下面是另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9.  
  10. <context:component-scan base-package="com.tutorialspoint" />
  11.  
  12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <property name="prefix" value="/WEB-INF/jsp/" />
  14. <property name="suffix" value=".jsp" />
  15. </bean>
  16.  
  17. <bean class="org.springframework.web.servlet.handler.
  18. SimpleMappingExceptionResolver">
  19. <property name="exceptionMappings">
  20. <props>
  21. <prop key="com.tutorialspoint.SpringException">
  22. ExceptionPage
  23. </prop>
  24. </props>
  25. </property>
  26. <property name="defaultErrorView" value="error"/>
  27. </bean>
  28.  
  29. </beans>

在这里,你指定 ExceptionPage 作为一个异常视图,以便 SpringException 发生,如果有任何其他类型的异常发生,那么一个通用的视图 error 会发生。

下面是 Spring 视图文件 student.jsp 的内容:

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC Exception Handling</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Student Information</h2>
  9. <form:form method="POST" action="/HelloWeb/addStudent">
  10. <table>
  11. <tr>
  12. <td><form:label path="name">Name</form:label></td>
  13. <td><form:input path="name" /></td>
  14. </tr>
  15. <tr>
  16. <td><form:label path="age">Age</form:label></td>
  17. <td><form:input path="age" /></td>
  18. </tr>
  19. <tr>
  20. <td><form:label path="id">id</form:label></td>
  21. <td><form:input path="id" /></td>
  22. </tr>
  23. <tr>
  24. <td colspan="2">
  25. <input type="submit" value="Submit"/>
  26. </td>
  27. </tr>
  28. </table>
  29. </form:form>
  30. </body>
  31. </html>

下面是 Spring 视图文件 error.jsp 的内容:

  1. <html>
  2. <head>
  3. <title>Spring Error Page</title>
  4. </head>
  5. <body>
  6.  
  7. <p>An error occured, please contact webmaster.</p>
  8.  
  9. </body>
  10. </html>;

下面是 Spring 视图文件 ExceptionPage.jsp 的内容。在这里,你将通过 ${exception} 访问异常实例。

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC Exception Handling</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Spring MVC Exception Handling</h2>
  9.  
  10. <h3>${exception.exceptionMsg}</h3>
  11.  
  12. </body>
  13. </html>

下面是 Spring 视图文件 result.jsp 的内容:

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring MVC Form Handling</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Submitted Student Information</h2>
  9. <table>
  10. <tr>
  11. <td>Name</td>
  12. <td>${name}</td>
  13. </tr>
  14. <tr>
  15. <td>Age</td>
  16. <td>${age}</td>
  17. </tr>
  18. <tr>
  19. <td>ID</td>
  20. <td>${id}</td>
  21. </tr>
  22. </table>
  23. </body>
  24. </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 应用程序一切都正常,你应该看到下面的结果:

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号