跟着别人学ssm+springSecurity(三)

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);
    }
}






猜你喜欢

转载自blog.csdn.net/u011323390/article/details/78402604