Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring 异常处理例子
Spring 异常处理例子
作者:--    发布时间:2019-11-20

下面的例子说明了如何使用 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 文件的内容:

package com.tutorialspoint;
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.tutorialspoint;
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 文件的内容。这里,你需要使用 @exceptionhandler 注解一个 service 方法,你可以指定要处理的一个或多个异常。如果你要指定一个以上的异常,那么你可以使用逗号分隔这些值。

package com.tutorialspoint;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.exceptionhandler;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.servlet.modelandview;
import org.springframework.ui.modelmap;
@controller
public 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( @modelattribute("helloweb")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";
   }
}

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

<web-app id="webapp_id" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xsi:schemalocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>spring exception handling</display-name>

    <servlet>
        <servlet-name>helloweb</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.dispatcherservlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>helloweb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

下面是另一个 spring web 配置文件 helloweb-servlet.xml 的内容

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="
http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.tutorialspoint" />

<bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
   <property name="prefix" value="/web-inf/jsp/" />
   <property name="suffix" value=".jsp" />
</bean>

<bean class="org.springframework.web.servlet.handler.
      simplemappingexceptionresolver">
   <property name="exceptionmappings">
      <props>
         <prop key="com.tutorialspoint.springexception">
            exceptionpage
         </prop>
      </props>
   </property>
   <property name="defaulterrorview" value="error"/>
</bean>

</beans>

在这里,你指定 exceptionpage 作为一个异常视图,以便 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.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 应用程序一切都正常,你应该看到下面的结果:


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册