SSM简单配置

本文档详细介绍了SSM(Spring、SpringMVC、Mybatis)的整合配置过程,包括web.xml、applicationcontext.xml中Spring配置,springmvc.xml中MVC配置,自定义异常处理和拦截器设置,以及视图解析器的配置,最后展示了成功页面success.jsp的返回。
摘要由CSDN通过智能技术生成

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!--    加载spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationcontext.xml</param-value>
    </context-param>
    <!--编码过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 监听器   配置会默认加载web-inf下的配置文件,但是spring的配置文件放在resources文件夹下便于管理,
    需要在上面另外配置<context-param>-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--    配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring配置文件

(包含mybatis配置,从而去除mybatis主配置文件sqlmapconfig.xml)

applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns="http://www.springframework.org/schema/beans"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置spring注解扫描的包-->
    <context:component-scan base-package="com.he">
        <!--    只希望注解扫描service和dao,@controller注解属于mvc范畴,故排除@Controller注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- (删除mybatis配置文件,放到spring中配置)
          在spring配置mybatis配置文件(连接池,sqlsessionfactory,Dao接口位置)-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/***"/>
        <property name="user" value="root"/>
        <property name="password" value="admin"/>
    </bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.he.dao"/>
    </bean>
    <!--配置申明式事务-->
    <!--    配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--    配置事务通知-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="finf*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--    配置aop增强-->
    <aop:config>
        <aop:advisor advice-ref="txadvice" pointcut="execution(* com.he.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>
</beans>

MVC配置文件

springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns="http://www.springframework.org/schema/beans"
       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">
    <!--    配置mvc注解只扫描controller注解,不包括controller类中的如@autowired注解-->
    <context:component-scan base-package="com.he">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--    配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--    配置静态资源释放-->
    <!--    <mvc:resources mapping="/js/**" location="/js/"/>-->
    <!--    <mvc:resources mapping="" location=""/>-->
    <!--    <mvc:resources mapping="" location=""/>-->
    <!--    配置文件处理器,其他bean的id名字都可以随便起,这个只能写这个-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"/>
    </bean>
    <!--    配置自定义的异常处理器-->
    <bean class="com.he.exception.ExceptionResolver"/>
    <!--    配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <bean class="com.he.interceptors.Interceptor1"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.he.interceptors.Interceptor2"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <!--<mvc:resources mapping="" location=""/>-->
    <!--配置转换类型(日期转换)-->
    <!--    <bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">-->
    <!--    converters 类型为set集合-->
    <!--        <property name="converters">-->
    <!--            <set>-->
    <!--    定义一个类实现ConversionService接口,导入全限定类名-->
    <!--                <bean class="com.he"/>-->
    <!--            </set>-->
    <!--        </property>-->
    <!--    </bean>-->
    <!--    配置处理器映射器(找到controller中的方法),处理器适配器(执行此方法)-->
    <mvc:annotation-driven/>
</beans>

自定义异常类

package com.he.exception;

public class SysException extends Exception {
    public SysException(String msg) {
        super(msg);
    }
}

mvc配置的异常处理器

package com.he.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",e.getMessage());
        //直接视图解析器跳转到error页面
        modelAndView.setViewName("error");
        return modelAndView;
    }
}

拦截器1

package com.he.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Interceptor1 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器执行了。。。。。。。。。。。。。");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("拦截器执行后,controller返回后(视图解析器执行前).........");
//      request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("视图解析器执行之后。。。。。。");
    }
}

拦截器2

package com.he.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Interceptor2 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器执行了。。。。。。。。。。。。。2222");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("拦截器执行后,controller返回后(视图解析器执行前).........222222222");
//        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("视图解析器执行之后。。。。。。22222");
    }
}

domian、dao、service正常配置就行

视图解析器返回的页面

success.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: wsjyhq
  Date: 2019/10/20
  Time: 23:43
  To change this template use File | Settings | File Templates.
--%>
<%--isELIgnored="false" 开启el表达式--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--导入jstl包--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--获取域对象的集合,因为model底层会把值存到request中--%>
${list}
<%--jstl遍历集合--%>
<c:forEach items="${list}" var="account">
    ${account.name}
</c:forEach>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值