SpringMVC中如何编写异常处理器和拦截器的具体实现代码?

2026-05-16 04:281阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计867个文字,预计阅读时间需要4分钟。

SpringMVC中如何编写异常处理器和拦截器的具体实现代码?

一、异常处理器+1、实现HandlerExceptionResolver接口+package+com.wuxi.exceptions;import+org.springframework.web.servlet.HandlerExceptionResolver;import+org.springframework.web.servlet.ModelAndView;import+javax.servlet.http.HttpServletRequest;

一、异常处理器

SpringMVC中如何编写异常处理器和拦截器的具体实现代码?

1、实现HandlerExceptionResolver接口

package com.wuxi.exceptions; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描组件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--参数类型装换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置调度器不拦截静态资源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--异常处理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--开启springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>

二、拦截器

1、实现HandlerInterceptor接口

package com.wuxi.interceptors; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描组件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--参数类型装换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置调度器不拦截静态资源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--异常处理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--拦截器--> <mvc:interceptors> <!--可配置多个拦截器,执行顺序pre1->pre2->controller->post2->post1->jsp->after2->after1--> <mvc:interceptor> <!--拦截的资源路径--> <mvc:mapping path="/**"/> <!--不拦截的资源路径--> <!--<mvc:exclude-mapping path="/hello"/>--> <bean class="com.wuxi.interceptors.ControllerInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!--开启springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计867个文字,预计阅读时间需要4分钟。

SpringMVC中如何编写异常处理器和拦截器的具体实现代码?

一、异常处理器+1、实现HandlerExceptionResolver接口+package+com.wuxi.exceptions;import+org.springframework.web.servlet.HandlerExceptionResolver;import+org.springframework.web.servlet.ModelAndView;import+javax.servlet.http.HttpServletRequest;

一、异常处理器

SpringMVC中如何编写异常处理器和拦截器的具体实现代码?

1、实现HandlerExceptionResolver接口

package com.wuxi.exceptions; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描组件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--参数类型装换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置调度器不拦截静态资源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--异常处理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--开启springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>

二、拦截器

1、实现HandlerInterceptor接口

package com.wuxi.interceptors; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描组件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--参数类型装换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置调度器不拦截静态资源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--异常处理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--拦截器--> <mvc:interceptors> <!--可配置多个拦截器,执行顺序pre1->pre2->controller->post2->post1->jsp->after2->after1--> <mvc:interceptor> <!--拦截的资源路径--> <mvc:mapping path="/**"/> <!--不拦截的资源路径--> <!--<mvc:exclude-mapping path="/hello"/>--> <bean class="com.wuxi.interceptors.ControllerInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!--开启springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。