目录
SpringMVC概述
入门案例
使用Servlet技术开发web程序流程
使用SpringMVC技术开发web程序流程
导入pom.xml文件
UserController
SpringMvcConfig
ServletContainersInitConfig
POST请求中文乱码处理
代参数GET请求
请求参数与传递
普通参数:请求参数名与形参变量名不同,使用@RequestParam绑定参数关系
POJO参数传递和嵌套POJO参数传递
对应实体类
数组和集合传参
接受请求中的JSON数据
设置接受JSON数据@RequestBody
@RequestBody和@RequestParam区别
日期类型参数的传递
SpringMVC概述
1.SpringMVC 是一种基于Java实现MVC模型的轻量级框架
优点
1.1使用简单,开发便捷
1.2灵活性强
入门案例
使用Servlet技术开发web程序流程
1.创建web工程
2.设置tomcat服务器,加载web工程(tomcat插件)
3.导入坐标Servlet
4.定义处理请求的功能类(User Servlet)
5.设置请求映射
使用SpringMVC技术开发web程序流程
1.创建web工程
2.设置tomcat服务器,加载web工程(tomcat插件)
3.导入坐标SpringMVC+Servlet
4.定义处理请求的功能类(UserController)
5.设置请求映射(配置映射关系)
6.将SpringMVC设定加载到Tomcat容器中
导入pom.xml文件
<?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.liyang</groupId>
<artifactId>springMVC_001</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springMVC_001 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
UserController
package com.liyang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @version 1.0
* @auter liyang
*/
//定义controller
//使用@Controller定义bean
@Controller
public class UserController {
//设置当前操作的访问路径
@RequestMapping("/save")
//设置当前操作的返回值类型
@ResponseBody
public String sava(){
System.out.println("user save...");
return "{'module':'springmvc'}";
}
}
SpringMvcConfig
package com.liyang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @version 1.0
* @auter liyang
*/
//创建springmvc的配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.liyang.controller")
public class SpringMvcConfig {
}
ServletContainersInitConfig
package com.liyang.config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* @version 1.0
* @auter liyang
*/
//定义一个servlet容器启动配置类在里面加载spring的配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载springmvc容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置那些请求归属springmvc处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加载spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
因为功能不同,如何避免Spring错误的加载到SpringMvc的bean
加载Spring控制的时候排除掉SpringMVC控制的bean
1.SpringMVC对应bean加载
2.Spring对应bean加载
package com.liyang.config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* @version 1.0
* @auter liyang
*/
//定义一个servlet容器启动配置类在里面加载spring的配置
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
/*
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载springmvc容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置那些请求归属springmvc处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加载spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
}
*/
POST请求中文乱码处理
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter=new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
代参数GET请求
//普通参数
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name,int age){
System.out.println("普通参数 name==>"+name+" "+age);
return "{'module':'common param'}";
}
请求参数与传递
普通参数:请求参数名与形参变量名不同,使用@RequestParam绑定参数关系
@RequestMapping("/commonParamDifferentName")
@ResponseBody
public String commonParamDifferentName(@RequestParam("name") String Username, int age){
System.out.println("普通参数 name==>"+Username+" "+age);
return "{'module':'common param different name'}";
}
POJO参数传递和嵌套POJO参数传递
//POJO参数
@RequestMapping("/PojoParam")
@ResponseBody
public String PojoParam(User user){
System.out.println("POJO参数传递 name==>"+user);
return "{'module':'POJO param'}";
}
@RequestMapping("/PojoContainPojoParam")
@ResponseBody
public String PojoContainPojoParam(User user){
System.out.println("POJO参数传递 name==>"+user);
return "{'module':'POJO Contain Pojo param'}";
}
对应实体类
package com.liyang.domain;
/**
* @version 1.0
* @auter liyang
*/
public class User {
private String name;
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
数组和集合传参
@RequestMapping("/ArrayParam")
@ResponseBody
public String ArrayParam(String[] likes){
System.out.println("POJO参数传递 likes==>"+ Arrays.toString(likes));
return "{'module':'Arrayparam'}";
}
@RequestMapping("/listParam")
@ResponseBody
public String ArrayParam(@RequestParam List<String> likes){
System.out.println("POJO集合参数传递 likes==>"+ likes);
return "{'module':'list param'}";
}
接受请求中的JSON数据
设置发送json数据(请求body中添加JSON数据)
开启自动转换JSON数据支持
//@EnableWebMvc开启 由JSON数据转换实体类数据 //创建springmvc的配置文件,加载controller对应的bean @Configuration @ComponentScan("com.liyang.controller") @EnableWebMvc public class SpringMvcConfig { }
设置接受JSON数据@RequestBody
//JSON参数数据传输
@RequestMapping("/listParamForJson")
@ResponseBody
public String listParamForJson(@RequestBody List<String> likes){
System.out.println("POJO集合参数传递 likes==>"+ likes);
return "{'module':'list for Json param'}";
}
//POJO参数JSON传输
@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user){
System.out.println("POJO集合参数传递 user==>"+ user);
return "{'module':'POJO for Json param'}";
}
//POJO集合参数JSON传输
@RequestMapping("/listpojoParamForJson")
@ResponseBody
public String listPojoParamForJson(@RequestBody List<User> list){
System.out.println("list POJO集合参数传递 user==>"+ list);
return "{'module':'list POJO for Json param'}";
}
@RequestBody和@RequestParam区别
区别:
@RequestParam用于接收url地址传输,表单传输【application/x-www-from-urlencoded】
@RequestBody用于接收json数据(application/json)
运用
后期开发中,发送json格式数据为主,@RequestBody运用较广
如果发送非JSON格式数据,选用@RequestParam接受请求参数