什么是SpringMVC
(1) SpringMVC是Spring的子框架,属于同一家公司产品,可以和spring框架无缝整合
(2) SpringMVC运行效率远高于struts2框架:struts2的Action是多例 ; SpringMVC的 Controller是单例
(3) 注解式开发更高效:SpringMVC推荐注解式开发 ; struts2注解式开发 配置文件开发
MVC模型:
M:Module,模型层 , entity+dao+service, mybatis
V:View,视图层 , webapp, html/css+jsp
C:Controller,控制层,action, struts2(不使用),被SpringMVC框架替换
springmvc相关配置:
1.引入相关jar依赖:spring-webmvc
2.在web.xml文件中配置SpringMVC的前端控制器,处理post中文请求乱码,spring
自动工厂,加载工厂配置,告知服务器启动spring工厂文件的位置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--处理post中文请求乱码-->
<filter>
<filter-name>ch</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>ch</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springMVC的前端控制器 -->
<servlet>
<servlet-name>springmvc</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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置spring自动工厂-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--加载工厂配置,告知服务器启动spring工厂文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
3. 创建配置spring.xml文件
位于main->resources->spring.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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--0.配置小文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--1.扫描器-->
<context:component-scan base-package="com.xxx"/>
<!--2.配置数据源-->
<bean id="dc" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.mydriver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.配置工厂-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factory">
<property name="dataSource" ref="dc"/>
<property name="typeAliasesPackage" value="com.xxx.lsg.entity"/>
<property name="mapperLocations" value="classpath:com/xxx/lsg/mapper/*Mapper.xml"/>
</bean>
<!--4.管理dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"/>
<property name="basePackage" value="com.xxx.lsg.dao"/>
</bean>
<!--5.spring声明式事务-->
<!--事务管理器-->
<bean id="dstm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dc"/>
</bean>
<!--6.配置事务的属性-->
<tx:annotation-driven transaction-manager="dstm"/>
</beans>
4. 创建配置springmvc.xml文件
位于main->resources->springmvc.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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解包-->
<context:component-scan base-package="com.baizhi.zmj.controller"/>
<!--请求处理器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- <!–配置文件上传 –>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000000"/>
</bean>-->
<!--解析静态资源的访问-->
<mvc:default-servlet-handler/>
</beans>
父子容器污染的解决
注意:spring springMVC,各自管理自己的组件,不要重复管理组件,否则会带来父子容器的污染。解决方案如下:
spring.xml文件中:
<!-- 4. 开启注解包 -->
<context:component-scan base-package="com.xxx.lsg" use-default-filters="true">
<!-- 指定要排除的注解类型 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
springmvc.xml文件中:
<!-- 1. 开启注解扫描 -->
<context:component-scan base-package="com.xxx.lsg.controller" use-default-filters="false">
<!-- 指定只扫描的注解类型为:Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
SpringMVC中拦截器(interceptor)
作用:
类似于javaweb中的Filter,用来对请求进行拦截,可以将多个Controller中执行的共同代码放入拦截器中执行,减少Controller类中代码的冗余.
特点:
(1)请求之前会经过对应拦截器,响应回来还会回到对应拦截器中执行
(2)拦截器不能拦截静态资源和jsp页面相关请求,只能拦截Controller相关请求
(3)拦截器可以随时中断|改变请求执行轨迹
自定义拦截器
public class 类名 implements HandlerInterceptor {}
package com.xxx.lsg.inters;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Override
/*
请求预处理,请求拦截器之后优先进入prehandle方法执行中,
返回值boolean类型,flase--中断请求
true--请求继续执行,执行之后会进入对应请求控制器发型执行
*/
/*例如强制登录*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
HttpSession session = request.getSession(true);
Object obj = session.getAttribute("user");
if(obj != null){
return true;
}else{
response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
在springmvc.xml中配置拦截器
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 指定拦截器要拦截的资源,支持通配符
/file/* 代表web应用file目录下的所有资源
/file/** 代表web应用file目录及子目录下的所有资源
/* 代表web应用根目录下的所有资源
/** 代表web应用根目录及子目录下的所有资源
-->
<mvc:interceptor>
<mvc:mapping path="/acc/**"/>
<bean class="com.xxx.lsg.inter.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>