<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置注解扫描-->
<context:component-scan base-package="com.hhh.web.controller"/>
<!-- 配置注解驱动-->
<mvc:annotation-driven/>
<!-- 配置试图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置前端控制器:
修改TomCat的目录为根目录:
package com.hhh.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UsersController {
@RequestMapping("/getUsers")
public String getUsers(){
// String str = null;
// str.length(); //这里会出现空指针异常
int[] arr = new int[1];
arr[2] = 10; //这里会出现数组越界问题
return "showuser";
}
/**
* 用来处理空指针异常的
* @param e
* @param model
* @return
*/
@ExceptionHandler({java.lang.NullPointerException.class})
public String NullPointExceptionHandle(Exception e, Model model){
model.addAttribute("msg",e);
return "error";
}
/**
* 这里处理上面定义好的异常外的所有异常问题
* @param e
* @param model
* @return
*/
@ExceptionHandler({java.lang.Exception.class})
public String ExceptionHandle(Exception e,Model model){
model.addAttribute("msg",e);
return "error2";
}
}