文章目录
1.定义
model view controller
用来简化基于mvc架构的web应用开发框架,是spring框架的一部分
2.架构分层
上层调用下层,通过接口实现,当下层实现发生改变时,不影响上一层
3.工作原理(五大组件的关系)
4.SpringMVC 使用
需要在 web.xml 中配置 DispatcherServlet 。并且需要配置 Spring 监听器ContextLoaderListener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 如果不设置init-param标签,则必须在/WEB-INF/下创建xxx-servlet.xml文件,其中xxx是servlet-name中配置的名称。 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.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="net.wanho.controller"/>
<!--mvc注解扫描,使@RequestMapping起作用-->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="fastjsonBean"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!--使用fastjson-->
<bean id="fastjsonBean" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd HH:mm:sss"/>
</bean>
</property>
</bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源-->
<mvc:resources mapping="/img/**" location="/WEB-INF/img/"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="10240000"/>
</bean>
<!--登录拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/**/**.html"/>
<mvc:exclude-mapping path="/**/**.js"/>
<mvc:exclude-mapping path="/**/**.css"/>
<mvc:exclude-mapping path="/**/**.jpg"/>
<mvc:exclude-mapping path="/**/**.png"/>
<mvc:exclude-mapping path="/**/**.gif"/>
<bean class="net.wanho.interceptor.LoginInterCeptor"/> <!--自定义的登陆拦截-->
</mvc:interceptor>
</mvc:interceptors>
</beans>
5.注解
访问路径=类@RequestMapping url +方法url
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
@RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm(@RequestParam("id") String personId) {
return new AppointmentForm();
}
}
6.重定向与转发
@RequestMapping("/testRedirect")
public String testRedirect(){
System.out.println("testRedirect");
// 重定向
return "redirect:/index.do";
// 转发
//return "forward:/index.jsp";
}
7.interceptor
流程
优先级
当有多个拦截器满足要求时,依据配置的先后顺序来执行
filter与interceptor的区别
- filter属于servlet规范,在web.xml中配置,拦截servlet容器的调用过程
- interceptor属于spring框架,在spring-mvc.xml中配置,拦截DispatcherServlet的调用过程