简介
- springMVC是spring组件的一部分,springMVC基于spring
- MVC
model 数据模型 (service entity dao)
view 视图 表现层(html,jsp,freemarker,xml,excel)
controller 控制器 (servlet,springMVC,struts 1,2) - 主要解决了控制层问题
基本构造
- 前端控制器 DispatcherServlet (中央控制器)
- 处理器 handler 处理用户的业务
- 处理的结果返回前端控制器(DispatcherServlet根据处理的结果判断,如果有页面请求,返回视图)
搭建
- 搭建spring spring要和web集成
- 配置前端控制器 DispatcherServlet
a.配置url地址需要使用后缀名配置 *.action
b.restful 分格配置 /
springMVC会默认加载 WEB-INF/(servlet的名字)-servlet.xml 这个配置文件
a.在servletContext中指定 mvc的配置文件
b.在servletConfig中指定 mvc的配置文件 - 建立控制层 将控制层放到spring容器中 (扫包,打标记)
- 配置2个核心组件 处理器映射器,处理器适配器
a.配置HandlerMapping 处理器映射器 (地址映射)
在类上@RequestMapping 指定类的位置
在方法上加 @RequestMapping指定方法的位置
b.配置HandlerAdapter处理器适配器 处理业务
c.同时配置2大组件可以使用注解驱动来配置 <mvc:annotation-driven></mvc:annotation-driven> - 配置视图解析器 org.springframework.web.servlet.view.InternalResourceViewResolver
a.前缀和后缀 ,指定视图的位置
b.配置视图的种类 默认是JSP视图
DispatcherServlet
- 配置核心组件需要指定springMVC的配置文件
- 配置地址映射 后缀名*.action | / (restful风格)
/ 配置之后会出现 静态资源文件不能被访问的问题 (图片,视频,音频,js文件,css文件)
a.在springMVC的配置文件中加上 <mvc:default-servlet-handler />会对静态资源拦截转由默认Servlet进行处理
b.在springMVC的配置文件中加上<mvc:resources location="/WEB-INF/public/" mapping="/resources/**"/>将location的目录地址映射成mapping的地址进行访问
处理器映射与适配
处理器映射器
- requestMapping 可以只指定方法 类可以省略(不建议)
路径不能重复 类的路径不重复 同一个类中 方法的路径不能重复 - 路径可以带/ 也可以带.action 还可以多级目录 也可以什么都不带
- requestMapping value可以指定数组 多路径指向一个方法
- method 参数指定请求的类型 RequestMethod.GET POST …
处理器适配器 ==>调用handler
- 默认返回ModelAndView 如果只是为了跳转页面 可以直接返回String
- 如果跳转页面
转发 默认使用视图解析器前缀+后缀拼接地址 forward: 地址要写全路径 (视图解析器前缀和后缀失效)(不推荐)
重定向 redirect: 地址要写全路径 (视图解析器前缀和后缀失效) - 返回数据
ModelAndView
a.设置viewName 转发的页面
b.设置ModelMap addAllObject() 发送到前台的数据或者直接设置键值对 addObject()
(原理 request.setAttribute()) - 支持原生态的servlet
springMVC所有的方法自带 HttpServletRequest request, HttpServletResponse response
乱码问题
- 配置spring字符过滤器 org.springframework.web.filter.CharacterEncodingFilter
- 指定编码为UTF-8
参数接收
- 单个普通类型 支持request获取
支持直接指定方法参数 - 多个普通类型 支持封装成一个参数Bean
在使用springMVC参数映射的时候基本类型请全部使用包装类 - 数组类型
在后台使用数组接收 单个参数或者封装成对象里面设置 都可以 - 集合类型
a.在后台要用集合接收
b.接收的时候前台的name属性要指定下标
c.指定下标后再通过 xx[flag].属性 的方式设置内部的属性
配置示例
web.xml
<?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">
<!--spring字符过滤器-->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载spring配置文件的地址-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 前端控制器-->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.igeek.action"></context:component-scan>
<!--处理器映射器-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />-->
<!--处理器适配器-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />-->
<!--处理器驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--默认是jsp的视图-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- 配置逻辑视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 配置逻辑视图的后缀 -->
<property name="suffix" value=".jsp" />
<!--前缀+视图字符串+后缀-->
</bean>
<!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler />
<!--<mvc:resources location="/WEB-INF/publicResources/" mapping="/resources/**"/>-->
</beans>
@Controller
@RequestMapping("/login")
public class LoginAction {
@RequestMapping(value = {"xx","login"},method = RequestMethod.GET)
public ModelAndView gotoLogin(HttpServletRequest request){
ModelAndView view = new ModelAndView();
view.setViewName("login");
view.addObject("username","xiaowang");
//业务代码
return view;
}
@RequestMapping("serv")
public void testLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("username","小王");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
}
@RequestMapping("param")
public void getPara(UserBean user){
System.out.println(user);
}
}