1 在springMVC.xml定义
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
//必须 ,如果没有这个,它会找JSTLView, 报404错
<bea id="jsonView2" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
//如果有底下的InternalResourceViewResolver 必须放在后面
<bean class="com.zhihe.office.web.controller.SpringExceptionResolver" /> //自定义异常处理
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
2 SpringExceptionResolver.java
public class SpringExceptionResolver implements HandlerExceptionResolver{
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
String url = httpServletRequest.getRequestURL().toString();
ModelAndView mv;
Map<String,String> map = new HashMap<String,String>();
map.put("test", "test");
mv = new ModelAndView("jsonView2", map);
System.out.println("exception ok!");
return mv;
}
}