springMVC之異常處理,springmvc異常處理

來源:互聯網
上載者:User

springMVC之異常處理,springmvc異常處理

1. 自訂一個異常類: UserException.java

public class UserException extends RuntimeException {private static final long serialVersionUID = 1L;public UserException() {super();}public UserException(String message, Throwable cause) {super(message, cause);}public UserException(String message) {super(message);}public UserException(Throwable cause) {super(cause);}}

2. 使用使用者登入的例子: UserController.java

   假定使用者名稱不存在或使用者密碼錯誤系統會拋出異常, 並跳到error.jsp頁面

@Controller@RequestMapping("/user")public class UserController {// 使用map類比資料庫 private Map<String, User> userMap = new HashMap<String, User>();public UserController() {userMap.put("zhangsan", new User("zhangsan", "123"));userMap.put("lishimin", new User("lishimin", "456"));}// 使用者登入之異常處理// 存取方法: http://localhost/springmvc_user/login.jsp@RequestMapping(value="/login", method=RequestMethod.POST)public String login(String username, String password, HttpSession session) {if(!userMap.containsKey(username)) {throw new UserException("使用者名稱不存在");}User user = userMap.get(username); if(!user.getPassword().equals(password)) {throw new UserException("使用者密碼不正確");}session.setAttribute("loginUser", user);return "redirect:/user/users";}/** * 局部異常處理(只能處理這個控制器中的異常) */@ExceptionHandler(value={UserException.class})public String handlerException(UserException e,HttpServletRequest request) {request.setAttribute("exception",e); return "exception/error";}}

3. 配置全域異常處理: user-servlet.xml

<!-- 全域異常處理 --><bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="com.zdp.exception.UserException">exception/error</prop></props></property></bean>

4. 錯誤資訊頁面: error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>錯誤頁面</title></head><body>發現錯誤: ${exception.message}</body></html>




spring mvc 異常怎處理

通過Spring mvc架構提供的SimpleMappingExceptionResolver來集中處理,該類可以捕獲所有控制器拋出的異常,並將它映射到配置的視圖中。

用法配置如下:
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingEx
ceptionResolver">
<property name="defaultErrorView">
<value>failure</value>
</property>
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">showDBError</prop>
<prop key="java.lang.RuntimeException">showError</prop>
</props>
</property>
</bean>
 
spring mvc類型轉換異常

String和Date直接的轉化和spring、MVC沒有一點關係....
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.