springmvc基础(1)


SpringMvc介绍

  • 使用@Controller创建控制器对象

  • SpringMvc中有一个对象是Servlet

    • DispatcherServlet:负责接收用户所有请求,把请求转发给Controller对象
  • Controller用来处理请求

创建SpringMvc项目

  • 在Maven中勾上Create from archetype(从原型创建)
  • 选择org.apache.maven.archetype:maven-archetype-webapp
  • pom.xml中加入依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
  • 将web.xml更替为4.0版本

  • 创建DispatcherServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
<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:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
  • DispatcherServlet介绍

    • Tomcat服务器启动后,会创建DispatcherServlet对象的实例
    • 在创建DispatcherServlet对象的实例时,会springmvc容器对象,所以会读取dispatcherServlet.xml
    • load-on-startup标签数值(>=0的整数)越小表示tomcat创建对象的时间越早
    • init-param标签用来自定义springmvc配置文件的位置,若不写,则路径是/WEB-INF/< servlet-name >-servlet.xml
    • url-pattern标签用来指定要处理的请求
  • 在index.jsp页面中发起请求

1
<a href="Nayuki.do">发起Nayuki.do请求</a>
  • 创建Controller类
1
2
3
4
5
6
7
8
9
10
@Controller
public class MyController{
@RequestMapping(value= "/Nayuki.do" )
public ModelAndView doNayuki(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.addObject("massage","发起Nayuki.do请求成功!");
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
}
  • 创建result.jsp读取modelAndView的massage参数
1
<h3>massage数据:${massage}</h3>
  • 创建dispatcherServlet.xml
1
<context:component-scan base-package="com.nayuki.controller"/>

配置视图解析器

  • 在WEB-INF创建view文件夹

  • 把result.jsp放入view文件夹

  • WEB-INF不允许客户端直接访问

  • 在dispatcherServlet.xml中配置视图解析器

    1
    2
    3
    4
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
  • 在Controller类中指定视图

    1
    modelAndView.setViewName("result");

RequestMapping的定义请求规则

  • 多个请求可以映射到同一个方法
1
@RequestMapping(value= {"/Nayuki.do","/Chiyao.do"} )
  • 可以加入前缀路径来统一管理

    • 可以直接放到类上面当模块名称

      1
      @RequestMapping("/user" )
    • 也可以单独在方法上指定

      1
      @RequestMapping("/user/Nayuki.do" )
    • 指定后要修改请求

      1
      <a href="user/Nayuki.do">发起Nayuki.do请求</a>

指定请求的接收方式

  • 指定为POST时只能接收POST方式的请求

    1
    @RequestMapping(value= "/Nayuki.do",method = RequestMethod.POST)
    • 指定后要修改请求方式

      1
      2
      3
      <form action="Nayuki.do" method="post">
      <input type="submit" value="发起Nayuki.do请求"/>
      </form>

接收请求的参数

  • request、response、session

    1
    2
    3
    4
    5
    public ModelAndView doNayuki(HttpServletRequest request,HttpServletResponse response,HttpSession session){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.addObject("massage","发起Nayuki.do请求成功!"+request.getParameter("name"));
    modelAndView.setViewName("/result.jsp");
    }
  • 逐个接收

    • 创建请求表单

      1
      2
      3
      4
      5
      <form action="Nayuki.do" method="POST">
      姓名:<input type="text" name="name"/><br>
      年龄:<input type="text" name="age" /><br>
      <input type="submit" value="发起Nayuki.do请求"/>
      </form>
    • 创建接收请求方法

      1
      2
      3
      4
      5
      6
      7
      public ModelAndView doNayuki(String name,int age){
      ModelAndView modelAndView =new ModelAndView();
      modelAndView.addObject("name",name);
      modelAndView.addObject("age",age);
      modelAndView.setViewName("/result.jsp");
      return modelAndView;
      }

解决POST乱码问题

  • 在web.xml中加入过滤器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <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>forceRequestEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    <init-param>
    <param-name>forceResponseEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>