目录
1.1 请求路径
1.1.1 窄化请求路径
1.1.2 多路径映射
1.1.3 请求方法限定
1.2 参数绑定
1.2.1 简单参数绑定
1.2.2绑定POJO类型
1.2.3复杂POJO
1.2.4 绑定数组/集合
1.2.5 自定义参数绑定:日期
1.1 请求路径
1.1.1 窄化请求路径
-
@RequestMapping放在类名上边,设置请求前缀
-
@RequestMapping放在方法名上边,设置方法对应请求路径。
-
完整请求:前缀 + 请求路径
@RequestMapping放在类名上边,设置请求前缀
@RequestMapping放在方法名上边,设置方法对应请求路径。
完整请求:前缀 + 请求路径
package mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo1")
public class Demo1 {
@RequestMapping("/index")
public String index(){
return "/index.jsp";
}
}
访问路径
基础-窄化
1.1.2 多路径映射
@RequestMapping 允许配置多个访问路径
@Controller
@RequestMapping("/demo1")
public class Demo1 {
@RequestMapping({"/index","/index2"})
public String index(){
return "/index.jsp";
}
}
访问路径
基础-多路径
1.1.3 请求方法限定
@RequestMapping 默认支持各种请求方式的访问,可以通过 method属性限制不同的请求方式。
| 注解 | 描述 |
|---|---|
| @RequestMapping(method = RequestMethod.GET) | 只有get请求方式可访问 |
| @RequestMapping(method = RequestMethod.POST) | 只有post请求方式可访问 |
| @RequestMapping(method={RequestMethod.GET,RequestMethod.POST}) | get和post都可访问 |
分别使用get和post请求访问
-
get请求抛异常
-
post允许访问
@Controller
@RequestMapping("/demo1")
public class Demo1 {
@RequestMapping(value = "/post",method = RequestMethod.POST)
public String post(){
return "/index.jsp";
}
}
访问路径
基础-get请求访问
1.2 参数绑定
1.2.1 简单参数绑定
1)基本操作
在控制器的方法中,只要有对应的参数名,spring mvc就可以自动完成参数封装
@Controller
@RequestMapping("/demo2")
public class Demo2 {
@RequestMapping("/findById")
public String findById(String id){
System.out.println("id"+id);
return "/index.jsp";
}
}
访问路径
参数-简单参数
参数名必须要和变量名保持一致
2)支持的数据类型
| 数据类型 | 取值 |
|---|---|
| 整型 | Integer、int |
| 字符串 | String |
| 单精度 | Float、float |
| 双精度 | Double、double |
| 布尔型 | Boolean、boolean |
3)自定义变量名:@RequestParam
-
如果请求参数名和方法参数名不一致时,需要使用@RequestParam标记对应关系。
-
需求:使用变量name接收参数id的值
@Controller
@RequestMapping("/demo2")
public class Demo2 {
@RequestMapping("/findById2")
public String findById2(@RequestParam("id") String name){
System.out.println("name"+name);
return "/index.jsp";
}
}
访问路径
参数-简单数据name
1.2.2绑定POJO类型
当提交一组数据时,通常我们会提供一个JAVABean用户数据的封装。
1) 编写JavaBean :User
package mvc.domain;
import java.util.Date;
import java.util.List;
public class User {
private Integer id;
private String username;
private String password;
private List hobbies;
private Date birthday;
}
//空参 满参 toString getter setter
2) 路径多参数
@Controller
@RequestMapping("/demo2")
public class Demo2 {
@RequestMapping("/add")
public String add(User user){
System.out.println(user);
return "/index.jsp";
}
}
访问路径
参数-POJO-路径
3)表单参数
1.2.3复杂POJO
在开放中,除了简单POJO类型POJO类型外,还有复杂POJO(包装POJO)
1)编写JavaBean :Order
package mvc.domain;
public class Order {
private Double price;
private User user;
}
//空参 满参 toString getter setter
2)编写表单
需求:提交订单以及用户数据
@Controller
@RequestMapping("/demo2")
public class Demo2 {
@RequestMapping("/addOrder")
public String addOrder(Order order){
System.out.println(order);
return "/index.jsp";
}
}
表单
1.2.4 绑定数组/集合
如果提交一组数据,可以使用数组或数据进行封装
修改表单
1.2.5 自定义参数绑定:日期
-
Spring MVC 默认支持的日期格式 yyyy/MM/dd
方式1:全局配置
-
配置WebMvcConfigurer
-
WebMvcConfigurer配置类是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
-
修改MvcConfiguration 配置类
-
实现 WebMvcConfigurer 接口,已经有缺省方法,所以不需要实现任何方法。
-
添加 @EnableWebMvc,开启个性定制。
-
覆盖 configureViewResolvers() 配置视图解析器
Spring MVC 默认支持的日期格式 yyyy/MM/dd
方式1:全局配置
-
配置WebMvcConfigurer
-
WebMvcConfigurer配置类是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
-
修改MvcConfiguration 配置类
-
实现 WebMvcConfigurer 接口,已经有缺省方法,所以不需要实现任何方法。
-
添加 @EnableWebMvc,开启个性定制。
-
覆盖 configureViewResolvers() 配置视图解析器
-
-
package com.czxy.mvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages="com.czxy.mvc.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/pages/",".jsp");
}
}
package com.czxy.mvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Configuration
@ComponentScan(basePackages="com.czxy.mvc.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
DateFormatter dateFormatter = new DateFormatter();
registry.addFormatterForFieldType(Date.class, dateFormatter );
}
}
方式2:单独设置
import org.springframework.format.annotation.DateTimeFormat; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birthday;



