SpringMVC-接收参数

使用传统的Request对象接收参数

  • 以下的内容都是紧接着前面的文章的如果你想来玩这个 Demo 请自行参考前面文章开始玩下来即可
  • 页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>receive parameter</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myController.action" method="get">
    商品名称:<input type="text" placeholder="请输入商品名称" name="goods_name"/>
    <br/>
    商品价格:<input type="text" placeholder="请输入商品价格" name="goods_price"/>
    <br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

  • MyFirstController 代码如下:
  • SpringMVC 框架会自动把 Request 对象传递给方法
/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public ModelAndView show(HttpServletRequest request) {

        String goods_name = request.getParameter("goods_name");
        String goods_price = request.getParameter("goods_price");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("goods_name", goods_name);
        modelAndView.addObject("goods_price", goods_price);

        modelAndView.setViewName("/first");

        return modelAndView;
    }
}

  • first.jsp 内容如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC-入门程序</title>
</head>
<body>
商品名称 → ${goods_name}
<br/>
商品价格 → ${goods_price}
</body>
</html>
  • 然后启动服务器,效果图如下:

  • 输入对应的内容提交之后的效果图如下:

不使用Request对象来接收简单数据类型的参数

  • 当请求的参数名称和处理器 形参 名称一致时会将请求参数与形参进行绑定

  • 测试方式同上即可

@RequestParam

  • 形参的类型与绑定的类型不一致时,可以使用 @RequestParam 进行绑定

  • value:请求参数名字
  • required:是否必须,默认是 true,表示请求中一定要有相应的参数,否则将报错
  • defaultValue:默认值,表示如果请求中没有同名参数时的默认值,也就是没有相关的参数会自动的设置一个默认值
  • 测试方式同上

接收JavaBean对象的参数

  • 要求对象当中的属性要和表单当中的名称一致
  • 创建 Goods
/**
 * @author: BNTang
 **/
public class Goods {

    private String goods_name;
    private String goods_price;

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(String goods_price) {
        this.goods_price = goods_price;
    }
}
  • 修改 MyFirstController
/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public ModelAndView show(Goods goods) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("goods", goods);

        modelAndView.setViewName("/first");

        return modelAndView;
    }
}

  • 修改 first.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC-入门程序</title>
</head>
<body>
商品名称 → ${goods.goods_name}
<br/>
商品价格 → ${goods.goods_price}
</body>
</html>
  • 测试方式同上

接收数组类型的参数

  • name 相同时,可以使用数组的形式来接收对应的参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>receive parameter</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myController.action" method="get">
    商品名称:<input type="text" placeholder="请输入商品名称" name="goods_name"/>
    <br/>
    商品价格:<input type="text" placeholder="请输入商品价格" name="goods_price"/>
    <br/>
    爱好:<input type="checkbox" name="hobby" value="篮球"> 篮球
    <input type="checkbox" name="hobby" value="乒乓球"> 乒乓球
    <input type="checkbox" name="hobby" value="足球"> 足球
    <input type="submit" value="提交"/>
</form>
</body>
</html>

  • 修改 Goods

/**
 * @author: BNTang
 **/
public class Goods {

    private String goods_name;
    private String goods_price;
    private String hobby[];

    @Override
    public String toString() {
        return "Goods{" +
                "goods_name='" + goods_name + '\'' +
                ", goods_price='" + goods_price + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(String goods_price) {
        this.goods_price = goods_price;
    }
}
  • 修改 MyFirstController
/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public String show(Goods goods) {
        System.out.println(goods);
        return "/first";
    }
}
  • 测试方式同上

接收包装类类型的参数

  • 创建 QueryVO
/**
 * @author: BNTang
 **/
public class QueryVO {

    private Goods goods;

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }
}
  • 修改 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>receive parameter</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myController.action" method="get">
    商品名称:<input type="text" placeholder="请输入商品名称" name="goods.goods_name"/>
    <br/>
    商品价格:<input type="text" placeholder="请输入商品价格" name="goods.goods_price"/>
    
    ...
</form>
</body>
</html>
  • 修改 MyFirstController

/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public ModelAndView show(QueryVO queryVO) {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("queryVO", queryVO);

        modelAndView.setViewName("/first");

        return modelAndView;
    }
}
  • 修改 first.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC-入门程序</title>
</head>
<body>
商品名称 → ${queryVO.goods.goods_name}
<br/>
商品价格 → ${queryVO.goods.goods_price}
</body>
</html>
  • 测试代码同上

接收List集合类型的参数

  • 创建 Dog
/**
 * @author BNTang
 **/
public class Dog {

    private String name;
    private String color;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
  • 修改 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>receive parameter</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myController.action" method="get">
    商品名称:<input type="text" placeholder="请输入商品名称" name="goods_name"/>
    <br/>
    商品价格:<input type="text" placeholder="请输入商品价格" name="goods_price"/>
    <br/>
    <h1>宠物</h1>
    宠物名称:<input type="text" name="dogs[0].name"/>
    宠物颜色:<input type="text" name="dogs[0].color"/>
    <br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
  • 修改 Goods

/**
 * @author: BNTang
 **/
public class Goods {

    private String goods_name;
    private String goods_price;
    private List<Dog> dogs;

    @Override
    public String toString() {
        return "Goods{" +
                "goods_name='" + goods_name + '\'' +
                ", goods_price='" + goods_price + '\'' +
                ", dogs=" + dogs +
                '}';
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(String goods_price) {
        this.goods_price = goods_price;
    }

    public List<Dog> getDogs() {
        return dogs;
    }

    public void setDogs(List<Dog> dogs) {
        this.dogs = dogs;
    }
}
  • 修改 MyFirstController

/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public String show(Goods goods) {
        System.out.println(goods);
        return "/first";
    }
}

自定义参数绑定

  • 根据业务需求自定义数据显示格式

需求

  • 修改商品日期
  • 由于日期数据有很多种格式,SpringMVC 没办法把字符串转换成日期类型
  • 所以需要自定义参数绑定

分析

  • 前端控制器接收到请求后,找到注解形式的处理器适配器
  • RequestMapping 标记的方法进行适配,并对方法中的形参进行参数绑定
  • 内部已经定义了很多的转换器,比如可以直接使用 int 类型来接收字符串
  • 日期类型与字符串比较特殊,字符串的日期格式,有很多种,可以自己来定义是哪种格式的转换
  • 可以在 SpringMVC 处理器适配器上自定义转换器 Converter 进行参数绑定

开始实现

  • 把发布时间对象的字符串转换成日期接收
  • 修改 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>receive parameter</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myController.action" method="get">
    商品名称:<input type="text" placeholder="请输入商品名称" name="goods_name"/>
    <br/>
    商品价格:<input type="text" placeholder="请输入商品价格" name="goods_price"/>
    <br/>
    发布时间:<input type="text" placeholder="请输入发布时间" name="goods_date"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

  • 修改 Goods
/**
 * @author: BNTang
 **/
public class Goods {

    private String goods_name;
    private String goods_price;
    private Date goods_date;

    @Override
    public String toString() {
        return "Goods{" +
                "goods_name='" + goods_name + '\'' +
                ", goods_price='" + goods_price + '\'' +
                ", goods_date=" + goods_date +
                '}';
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(String goods_price) {
        this.goods_price = goods_price;
    }

    public Date getGoods_date() {
        return goods_date;
    }

    public void setGoods_date(Date goods_date) {
        this.goods_date = goods_date;
    }
}
  • 创建转换器 DateConverter
/**
 * @author BNTang
 **/
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        try {
            if (null != source) {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(source);
            }
        } catch (Exception e) {
            System.out.println("转换异常");
        }
        return null;
    }
}
  • 在 SpringMVC 核心配置文件当中配置自定义转换器

...

<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>

<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="top.it6666.converter.DateConverter"/>
        </list>
    </property>
</bean>

...
  • 控制台代码如下 MyFirstController
/**
 * @author: BNTang
 **/
@Controller
public class MyFirstController {

    @RequestMapping("/myController.action")
    public String show(Goods goods) {
        System.out.println(goods);
        return "/first";
    }
}
  • 测试方式同上
posted @ 2020-10-24 17:52  BNTang  阅读(233)  评论(0编辑  收藏  举报