MVC是什么
MVC是一种设计思想,将业务逻辑从界面中分离出来,允许单独改变而不会互相影响
spring mvc是什么
能做什么?
Spring mvc 请求流程分析
springmvc环境配置
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shxt</groupId>
<artifactId>spring_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc01 Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc01</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/springmvc</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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:servelt-context.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>
</web-app>
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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.shxt.springmvc.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
URL地址映射配置
通过@RequestMapping 设置地址 @RequestMapping
- 设置在方法级别-方法路径
- 设置在类级别-类路径
- 设置类级别与方法-类路径/方法路径
1.映射单个地址
@RequestMapping("u01")
public ModelAndView url01(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","url01");
modelAndView.setViewName("hello");
return modelAndView;
}
2.映射多个地址配置
@RequestMapping(value = {"u03_01","u03_02"})
public ModelAndView url02(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","url02");
modelAndView.setViewName("hello");
return modelAndView;
}
3.设置类路径+方法路径 如果有类路径,则访问时需要将类路径作为访问的父路径(方法路径前需要加类路径)
此时访问地址变成类路径+方法路径-url/u04 4.设置请求地址的请求方式 通过注解的method属性设置(GET|POST|PUT|DELETE) RequestMethod.GET或RequestMethod.POST
@RequestMapping(value = "u05",method = RequestMethod.GET)
public ModelAndView url05(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","url05");
modelAndView.setViewName("hello");
return modelAndView;
}
5.设置参数路径–了解即可 通过注解的eparams属性设置参数路径
@RequestMapping(params = "uname")
public ModelAndView url06(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","url06");
modelAndView.setViewName("hello");
return modelAndView;
}
此时访问路径变成?+参数名
参数绑定
1.基本数据类型 1.传递的参数名与方法的形参名保持一致 2.如果参数未传递或参数值为null会报500异常
@RequestMapping("p01")
public void param01(int age,double money){
System.out.println("age:"+age+"money"+money);
}
可以通过@RequestParam注解设置参数的相关信息(注解声明在形参前) 通过defaultValue属性设置参数的默认值 可以通过属性name设置参数的别名
@RequestMapping("p02")
public void param02(@RequestParam (defaultValue = "18",name = "uage")int age, @RequestParam(defaultValue = "100") double money){
System.out.println("age:"+age+"money"+money);
}
``
2.包装类型-如果数据时基本数据类型,建议使用对应的包装类
1.传递的参数名与方法的形参名保持一致
2.参数未传递,默认值为null
```java
@RequestMapping("p03")
public void param03(Integer age, Double money){
System.out.println("age:"+age+"money"+money);
}
3.字符串类型 1.传递的参数名与方法的形参名保持一致 2.可以接收null值
@RequestMapping("p04")
public void param04(String msg){
System.out.println("msg"+msg);
}
4.数组类型 传递参数格式:ids=&ids=2&ids=3
@RequestMapping("p05")
public void param05(String[] ids){
for(String id : ids){
System.out.println(id);
}
}
5.java bean 类型 参数名与javaBean对线中的属性字段名保持一致
@RequestMapping("p06")
public void param06(User user){
System.out.println(user);
}
public class User {
private Integer userId;
private String username;}
GET SET.....方法
7.list类型 User 实体需要定义对应list属性(对于集合的参数绑定,一般需要使用javabean对象进行包装) private List list = new ArrayList();
@RequestMapping("p07")
public void param07(User user){
System.out.println(user);
}
请求转发和重定向
重定向 SpringMvc内部默认采用请求转发形式 请求转发-地址栏不发生改变,以Forward:开头 重定向-地址栏改变,以redirect:开头 请求转发跳转JSP页面 默认采用请求转发,会去视图解析器中设定的路径下查找指定的视图(在WEB-INF/)下寻找text.jsp文件
请求转发跳转JSP页面
使用forward:页面名转发,此时会从项目的根路径(webapp目录)下查找指定页面 如果传递参数,在页面中可以通过${param.参数名}获取对应的参数值
@RequestMapping("view02")
public ModelAndView view02(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","请求转发1");
modelAndView.setViewName("forward:view.jsp");
return modelAndView;
}
}
、 第二种方法 返回视图名字符串 默认会从视图解析器设置的路径下查找,(WEB-INF/jsp目录) 常用对象可以设置到方法的形参中,直接使用
@RequestMapping("view05")
public String view05(HttpServletRequest request){
request.setAttribute("msg","请求转发5");
return "text";
}
也可以使用forward
请求转发跳转控制器 使用forward:路径转发,此时会从项目的根路径下查找指定控制器
@RequestMapping("view04")
public ModelAndView view04(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","请求转发1");
modelAndView.setViewName("forward:hello");
return modelAndView;}
请求转发跳转控制器
@RequestMapping("view05")
public String view05(HttpServletRequest request){
request.setAttribute("msg","请求转发5");
return "forward:hello";
}
重定向
重定向到JSP页面 默认从项目的根目录下查找资源
@RequestMapping("view09")
public ModelAndView view09(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","重定向1");
modelAndView.setViewName("redirect:view.jsp");
return modelAndView;
}
如果要传递中文则无法获取,需要使用RedirectAttributes对象设置 返回页面字符串
@RequestMapping("view11")
public String view11(){
return "redirect:view.jsp";
}
重定向到控制器
@RequestMapping("view10")
public ModelAndView view10(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","重定向");
modelAndView.setViewName("redirect:hello");
return modelAndView;
}
返回页面字符串
@RequestMapping("view12")
public String view12(){
return "redirect:hello";
}
设置请求域 1.使用ModelAndView addObject(“名称”,“值”)
@RequestMapping("data01")
public ModelAndView data01(){
ModelAndView modelAndView = new ModelAndView("data");
modelAndView.addObject("msg","设置请求域");
return modelAndView;
}
2.使用HttpServletRequest setAttribute(“名称”,“值”)
@RequestMapping("data02")
public String data02(HttpServletRequest request){
request.setAttribute("msg","通过HttpServletRequest对象设置请求域");
return "data";
}
3.使用Model
@RequestMapping("data03")
public String data03(Model model){
model.addAttribute("msg","通过Model对象设置请求域");
return "data";
}
4.使用ModelMap
@RequestMapping("data04")
public String data04(ModelMap modelmap){
modelmap.addAttribute("msg","通过HttpServletRequest对象设置请求域");
return "data";
}
5.使用Map
@RequestMapping("data05")
public String data05(Map map){
map.put("msg","通过HttpServletRequest对象设置请求域");
return "data";
}
JSON数据开发
依赖 处理器 @ ResponseBody 默认控制器中方法的返回值时会去找对应的视图页面,如果想要返回数据,需要将返回的结果转换成字符串响应(转换成JSON字符串)
@RequestBody 要求传递的参数是一个JOSN格式的字符串!!!
拦截器实现
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("MyInterceptor-->preHandle方法....在目标方法执行前执行");
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor-->postHandle方法....在目标方法执行后,视图生成前执行");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("MyInterceptor-->afterCompletion方法....在目标方法执行后,视图生成后执行");
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/url/*"/>
<bean class="com.shxt.springmvc.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors
拦截器第二种实现 继承HandlerInterceptorAdapter 重写Prehandle方法
public class MyInterceptor02 extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
}
配置拦截器
如果多个拦截器同时生效,则先配置的先执行 先配置的拦截器种对应的preHandle先执行 先配置的Posthandle,afterCompletion后执行
非法访问拦截
拦截器继承HandlerInterceptorAdapter 如果未登录session为空
文件上传
上传表单(二进制表单) 1.表单的提交方式 method=post 2。表单的类型 普通表单-enctype=" " 二进制表单 3.设置文件域的name属性值 配置: RESTFUL URL设计风格 异常抛出 2.实现HandlerExceptionResolver接口
|