springmvc

SpringMVC简介

  1. SpringMVC中重要组件:
    • DispatcherServlet:前端控制器,接收所有请求(如果配置/则不包含jsp)
    • HandlerMapping:解析请求格式,判断希望要执行哪个具体的方法。
    • HandlerAdapter:负责调用具体的方法。
    • ViewResolver:视图解析器。解析结果,准备跳转到具体的物理视图。
  2. SpringMVC运行原理图。

1572525012_1_.png

  1. Spring容器和Springmvc容器的关系
    • 代码

Spring-springmvc.png

  • Spring容器和Springmvc容器的关系是父子容器
    • Springmvc容器中能够调用Spring容器的所有内容
    • 图示

springmvc02.png

SpringMVC环境搭建

  1. 导入jar包

1572584411_1_.png

  1. 在web.xml中配置前端控制器:DispatcherServlet

    • 如果不配置<init-param>,则会在/WEB-INF/找配置文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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_3_0.xsd" version="3.0">
    <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>
    <!-- 自启动 -->
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>
  2. 新建Springmvc.xml配置文件,引入xmlns:mvc属性命名空间。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?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="com.gdaib.controller"></context:component-scan>
    <!-- 配置注解驱动 --><!-- 相当于配置如下两个属性 -->
    <!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
    <!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 静态资源 -->
    <!-- 第一个参数:不拦截某个目录,第二个参数访问的路径,**代表该目录下的所有文件 -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    </beans>
  3. 编写控制器类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package com.gdaib.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class DemoController {
    @RequestMapping("demo")
    public String demo() {
    System.out.println("执行demo");
    return "main.jsp";
    }
    @RequestMapping("demo2")
    public String demo2() {
    System.out.println("指定demo2");
    return "main.jsp";
    }
    }

字符编码过滤器

  1. web.xml中配置Filter
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <!-- 解决中文乱码 -->
    <filter>
    <filter-name>encoding</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>encoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

传参

  1. 把内容写到方法(HandlerMethod)参数中, SpringMVC中只要有这个内容,注入内容

  2. 基本数据类型参数,默认保证参数名称和参数名称一样

    1
    2
    3
    4
    5
    6
    @RequestMapping("demo")
    public String demo(String name,int age) {
    System.out.println("执行demo");
    System.out.println(name + "|" + age);
    return "main.jsp";
    }
  3. 如果请求名和参数名不对应应使用@RequestParam()赋值

    1
    2
    3
    4
    5
    6
    @RequestMapping("demo")
    public String demo(@RequestParam(value="name")String name,@RequestParam(value="age")int age) {
    System.out.println("执行demo");
    System.out.println(name + "|" + age);
    return "main.jsp";
    }
  4. 如果方法参数是基本数据类型(不是封装类),可以为其设置默认值

    1
    2
    3
    4
    5
    6
    //如果参数为空时,为他们设置默认值
    @RequestMapping("page")
    public String page(@RequestParam(defaultValue = "2")int pageSize,@RequestParam(defaultValue = "1")int pageNumber) {
    System.out.println(pageSize+" "+pageNumber);
    return "main.jsp";
    }
  5. 如果强制要求必须有某个参数

    1
    2
    3
    4
    5
    @RequestMapping("demo2")
    public String demo2(@RequestParam(required = true)String name) {
    System.out.println("必须传递name参数:"+name);
    return "main.js";
    }
  6. HandlerMethod中参数是对象类型,请求参数名和对象中属性一致(get/set)

    1
    2
    3
    4
    @RequestMapping("demo4")
    public String demo4(People peo) {
    return "main.js";
    }
  7. 请求参数中包含多个同名参数的获取方法

    • 复选框传递的参数就是多个同名参数
    1
    2
    3
    4
    5
    @RequestMapping("demo5")
    public String demo5(String name,int age,@RequestParam("hover")List<String> hover) {
    System.out.println(name + " "+age+" "+hover);
    return "main.jsp";
    }
  8. 请求参数中对象.属性格式

    • JSP代码
    1
    2
    <input type="text" name="peo.name"/>
    <input type="text" name="peo.age"/>
  • 新建一个类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Demo {
private People peo;

public People getPeo() {
return peo;
}

public void setPeo(People peo) {
this.peo = peo;
}

@Override
public String toString() {
return "Demo [peo=" + peo + "]";
}

}
  • 控制器
1
2
3
4
5
@RequestMapping("demo06")
public String demo6(Demo demo) {
System.out.println(demo);
return "main.jsp";
}
  1. 在请求参数中传递集合对象类型对象
    • JSP页面
    1
    2
    3
    4
    <input type="text" name="peo[0].name"/>
    <input type="text" name="peo[0].age"/>
    <input type="text" name="peo[1].name"/>
    <input type="text" name="peo[1].age"/>
  • 新建一个类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Demo {
private List<People> peo;

public List<People> getPeo() {
return peo;
}

public void setPeo(List<People> peo) {
this.peo = peo;
}

@Override
public String toString() {
return "Demo [peo=" + peo + "]";
}
}
  • 控制器类
1
2
3
4
5
@RequestMapping("demo06")
public String demo6(Demo demo) {
System.out.println(demo);
return "main.jsp";
}
  1. restful风格
    • 简化jsp中参数编写格式
    • 在JSP中设定特定的格式
    1
    <a href="demo08/1232/abcd">跳转</a>
  • 在控制器中
    • @RequestMapping中一定要和请求格式对应
    • {名称}中名称自定义
    • @PathVariable获取@RequestMapping中内容,默认按照方法参数名称去寻找。
1
2
3
4
5
6
//restful
@RequestMapping("demo08/{id1}/{name1}")
public String demo8(@PathVariable("id1") int id,@PathVariable(value = "name1")String name) {
System.out.println(id+" "+name);
return "/main.jsp";
}

跳转方式

  1. 默认的跳转方式:请求转发。

  2. 设置返回字符串的内容

    • 添加 redirect:资源路径 ————> 重定向
    • 添加 forward:资源路径/省略 ————> 转发

    视图解析器

  3. Springmvc会提供默认解析器

  4. 程序员自定义解析器

    1
    2
    3
    4
    5
    <!-- 配置视图解析器 -->
    <bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
  5. 如果希望不执行自定义解析器,在方法返回值前面添加forward或者redirect

    1
    2
    3
    4
    5
    @RequestMapping("demo07")
    public String demo7(int id,String name) {
    System.out.println(id+" "+name);
    return "forward:main.jsp";
    }

@ResponseBody

  1. 在方法上只有@RequestMapping时,无论方法返回值是什么都认为需要跳转。
  2. 在方法上添加@ResponseBody
    • 如果返回值满足key-value形式(对象或map)
      • 把相应响应头设置为application/json;charset=utf-8
      • 把转换后的内容以输出流的形式响应给客户端。
    • 如果返回值不满足key-value,例如返回值为String
      • 把响应头设置为text/html
      • 把方法返回值以流的形式输出。
      • 如果返回值包含中文,出现中文乱码
      • produces表示响应头中Content-Type值

    SSM三大框架整合

  3. web.xml配置文件内容