Spring MVC基础

目录

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放在方法名上边,设置方法对应请求路径。

  • 完整请求:前缀 + 请求路径

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";
    }
}

访问路径

<a href="${pageContext.request.contextPath}/demo1/index.action">基础-窄化</a>

1.1.2 多路径映射

@RequestMapping 允许配置多个访问路径

@Controller
@RequestMapping("/demo1")
public class Demo1 {
    @RequestMapping({"/index","/index2"})
    public String index(){
        return "/index.jsp";
    }
}

访问路径

<a href="${pageContext.request.contextPath}/demo1/index2.action">基础-多路径</a>

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";
    }
}

访问路径

<a href="${pageContext.request.contextPath}/demo1/post.action">基础-get请求访问</a><br/>
<form action="${pageContext.request.contextPath}/demo1/post.action" method="post">
    <input type="submit" value="基础-post 请求访问">
</form>

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";
    }
}

访问路径

<a href="${pageContext.request.contextPath}/demo2/findById.action?id=10">参数-简单参数</a>

参数名必须要和变量名保持一致

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";
    }
}

访问路径

<a href="${pageContext.request.contextPath}/demo02/findById2.action?id=10">参数-简单数据name</a> <br/>

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<String> 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";
    }
}

 访问路径

<a href="${pageContext.request.contextPath}/demo2/add.action?id=10&username=xiaoyang&password=1234">参数-POJO-路径</a><br/>

3)表单参数

<form action="${pageContext.request.contextPath}/demo2/add.action" method="post">
    <input type="text" name="id" placeholder="id"/> <br/>
    <input type="text" name="username" placeholder="用户名"/> <br/>
    <input type="text" name="password" placeholder="密码"/> <br/>
    <input type="submit" value="参数-POJO-表单"/>
</form>

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";
    }
}

表单

<form action="${pageContext.request.contextPath}/demo02/addOrder.action" method="post">
	<input type="text" name="price" placeholder="价格" /> <br/>
	<input type="text" name="user.id" placeholder="用户id" /> <br/>
	<input type="text" name="user.username" placeholder="用户名" /> <br/>
	<input type="submit" value="提交"/> <br/>
</form>

1.2.4 绑定数组/集合

如果提交一组数据,可以使用数组或数据进行封装

 修改表单

<form action="${pageContext.request.contextPath}/demo2/add.action" method="post">
    <input type="text" name="id" placeholder="id"/> <br/>
    <input type="text" name="username" placeholder="用户名"/> <br/>
    <input type="text" name="password" placeholder="密码"/> <br/>
    <input type="checkbox" name="hobbies" value="抽烟" />抽烟
    <input type="checkbox" name="hobbies" value="喝酒" />喝酒
    <input type="checkbox" name="hobbies" value="烫头" />烫头 <br/>
    <input type="submit" value="参数-POJO-表单"/>
</form>

1.2.5 自定义参数绑定:日期

  • Spring MVC 默认支持的日期格式 yyyy/MM/dd

    方式1:全局配置

    • 配置WebMvcConfigurer

      • WebMvcConfigurer配置类是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制

      • 修改MvcConfiguration 配置类

        1. 实现 WebMvcConfigurer 接口,已经有缺省方法,所以不需要实现任何方法。

        2. 添加 @EnableWebMvc,开启个性定制。

        3. 覆盖 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 {

    /**
     * 设置日期转换
     * @param registry
     */
@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;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值