跟着别人学ssm+springSecurity(三)
来源:互联网 发布:pubmed数据库首页 编辑:程序博客网 时间:2024/05/16 19:52
Spring+SpringMVC
一、SpringMVC配置文件:目录:resource/config/spring,文件名:spring-mvc.xml
<?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 http://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.magic.rent.controller"/> <!--视图渲染--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--控制器映射器和控制器适配器--> <mvc:annotation-driven/></beans>
二、Spring配置文件:目录:resource/config/spring,文件名:applicationContext-service.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描service--> <context:component-scan base-package="com.zyj.service"/> <!--注册统一异常控制--> <bean id="exception" class="com.zyj.exception.exhandler.CustomExceptionHandler"/></beans>
目录:resource/config/spring,文件名:applicationContext-transaction.xm<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <--这部分,主要是根据方法名称来进行匹配限定,但是我们是用MyBatis自动生成的Mapper接口,所以在这边大家要按照MyBatis的命名规范来设置name的值。 <tx:method name="select*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zyj.service.*.*(..))"/> </aop:config></beans>
三、Spring统一异常处理:目录:com.zyj.exception.exhandler,文件名:CustomExceptionHandler.java
package com.zyj.exception.exhandler;import com.zyj.exception.custom.BusinessException;import com.zyj.exception.custom.ParameterException;import org.springframework.stereotype.Service;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.HashMap;import java.util.Map;public class CustomExceptionHandler implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) { Map<String, Object> model = new HashMap<String, Object>(); model.put("ex", ex); return new ModelAndView("../error/404", model); // 根据不同错误转向不同页面 /*if (ex instanceof BusinessException) { return new ModelAndView("../error/business_error", model); } else if (ex instanceof ParameterException) { return new ModelAndView("../error/parameter_error", model); } else { return new ModelAndView("../error/404", model); }*/ }}
写一个自定义异常范例package com.zyj.exception.custom;public class BusinessException extends RuntimeException { public BusinessException(String message) { super(message); } public BusinessException(String message, Throwable cause) { super(message, cause); }}
阅读全文
0 0
- 跟着别人学ssm+springSecurity(三)
- 跟着别人学ssm+springSecurity(二)
- 跟着别人学ssm+springSecurity(一)
- 跟着别人学:用LFS从零构建linux
- STM8单片机PWM应用(跟着别人学)
- 跟着thinkphp官方论坛学TP 三
- 跟着官方文档学HINT(三)优化器特性
- SpringSecurity(三):表单登陆
- 跟着vivi学bootloader
- 跟着学制表
- 跟着示例学Oozie
- 跟着示例学Oozie
- 跟着示例学Oozie
- 【跟着stackoverflow学Pandas】
- 【跟着stackoverflow学Pandas】
- 跟着示例学Oozie
- 跟着jdk学奇技淫巧~
- SpringSecurity源码学习(三)
- 素数环问题(深搜)
- python实现设计模式(一、工厂模式)
- docker升级
- JNI学习二参数传递
- 一个TCP发送小数据延迟的问题
- 跟着别人学ssm+springSecurity(三)
- 写makefiles(二)
- opencv中的图像校正相关
- .NET 采集数据去掉html标签,以逗号分割的
- Thinkphp5+阿里云SMS短信服务发送验证码
- Android 按钮监听器我常用的两种方法
- 第七课 Netty学习之心跳
- Oracle SCN简述
- 期望DP