新聞中心
Spring 異常處理例子
下面的例子說明了如何使用 Spring MVC 框架來編寫一個簡單的基于 web 的應用程序,它可以處理它的內置控制器產生的一個或多個異常。為了開始使用它,讓我們在恰當的位置使用 Eclipse IDE,然后按照下面的步驟使用 Spring 的 Web 框架來開發(fā)一個動態(tài)的基于表單的 Web 應用程序:

成都創(chuàng)新互聯(lián)公司主要從事成都網站制作、網站設計、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務陵水黎族,10余年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
| 步驟 | 描述 |
|---|---|
| 1 | 創(chuàng)建一個名稱為 HelloWeb 的動態(tài) Web 項目,并且在已創(chuàng)建的項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。 |
| 2 | 將上面提到的 Spring 和其他庫拖拽到文件夾 WebContent/WEB-INF/lib 中。 |
| 3 | 在 com.tutorialspoint 包下創(chuàng)建一個 Java 類 Student,StudentController 和 SpringException。 |
| 4 | 在 WebContent/WEB-INF 文件夾下創(chuàng)建 Spring 的配置文件 Web.xml 和 HelloWeb-servlet.xml。 |
| 5 | 在 WebContent/WEB-INF 文件夾下創(chuàng)建名稱為 jsp 的子文件夾。在這個子文件夾下創(chuàng)建視圖文件 student.jsp,result.jsp,error.jsp 和 ExceptionPage.jsp。 |
| 6 | 最后一步是創(chuàng)建所有的源代碼和配置文件的內容,并導出該應用程序,正如下面解釋的一樣。 |
這里是 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 的內容
Spring Exception Handling
HelloWeb
org.springframework.web.servlet.DispatcherServlet
1
HelloWeb
/
下面是另一個 Spring Web 配置文件 HelloWeb-servlet.xml 的內容
ExceptionPage
在這里,你指定 ExceptionPage 作為一個異常視圖,以便 SpringException 發(fā)生,如果有任何其他類型的異常發(fā)生,那么一個通用的視圖 error 會發(fā)生。
下面是 Spring 視圖文件 student.jsp 的內容:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Spring MVC Exception Handling
Student Information
Name
Age
id
下面是 Spring 視圖文件 error.jsp 的內容:
Spring Error Page
An error occured, please contact webmaster.
;
下面是 Spring 視圖文件 ExceptionPage.jsp 的內容。在這里,你將通過 ${exception} 訪問異常實例。
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Spring MVC Exception Handling
Spring MVC Exception Handling
${exception.exceptionMsg}
下面是 Spring 視圖文件 result.jsp 的內容:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Spring MVC Form Handling
Submitted Student Information
Name
${name}
Age
${age}
ID
${id}
最后,下面是包含在你的 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
一旦你完成了創(chuàng)建源代碼和配置文件后,導出你的應用程序。右鍵單擊你的應用程序,并且使用 Export > WAR File 選項,并且在 Tomcat 的 webapps 文件夾中保存你的 HelloWeb.war 文件。
現在啟動你的 Tomcat 服務器,并且確保你能夠使用標準的瀏覽器訪問 webapps 文件夾中的其他 web 頁面?,F在嘗試訪問該 URL http://localhost:8080/SpringWeb/student。如果你的 Spring Web 應用程序一切都正常,你應該看到下面的結果:
輸入如上圖所示的值,然后單擊提交按鈕。如果你的 Spring Web 應用程序一切都正常,你應該看到下面的結果:
網頁題目:創(chuàng)新互聯(lián)Spring教程:Spring異常處理例子
文章來源:http://www.5511xx.com/article/dphpdcc.html


咨詢
建站咨詢
