zoukankan      html  css  js  c++  java
  • SpringMVC中参数绑定

      SpringMVC中请求参数的接收主要有两种方式, 一种是基于HttpServletRequest对象获取, 另外一种是通过Controller中的形参获取

    一  通过HttpServletRequest获取请求参数

      首先, 通过一个实例获取jsp页面中提交的参数

    @RequestMapping("/itemEdit.action")
        public ModelAndView itemEdit (HttpServletRequest request) {
            //获取请求id信息
            String idStr = request.getParameter("id");
            int id = Integer.parseInt(idStr);
            //跟局id调用业务层查询对应数据
            Items items = itemService.findById(id);
            //创建modelandview对象
            ModelAndView modelAndView = new ModelAndView();
            //存储查询结果
            modelAndView.addObject("item", items);
            //页面转发
            modelAndView.setViewName("editItem");
            return modelAndView;
        }

      SpringMVC的Controller中默认支持的参数类型

    HttpServletRequest, HttpServletResponse, HttpSession, Model/ModelMap, 绑定简单类型

    二 通过Controller方法中的形参获取请求参数

      SpringMVC中支持通过方法的形参获取页面请求参数

    @RequestMapping("/itemEdit")

         public String itemEdit(Integer id, Model model) {

               Items items = itemService.getItemById(id);

               //向jsp传递数据

               model.addAttribute("item", items);

               //设置跳转的jsp页面

               return "editItem";

         }

    形参中支持的数据类型:

    参数类型推荐使用包装数据类型,因为基础数据类型不可以为null

    整形:Integer、int

    字符串:String

    单精度:Float、float

    双精度:Double、double

    布尔型:Boolean、boolean

    说明:对于布尔类型的参数,请求的参数值为true或false。

    处理器方法:

    public String editItem(Model model,Integer id,Boolean status) throws Exception

    请求url:

    http://localhost:8080/xxx.action?id=2&status=false

    三 @RequestParam注解的使用

    使用@RequestParam常用于处理简单类型的绑定。

    value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入;

    required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报;

    TTP Status 400 - Required Integer parameter 'XXXX' is not present

    defaultValue:默认值,表示如果请求中没有同名参数时的默认值

    定义如下:

    public String editItem(@RequestParam(value="item_id",required=true) String id) {

    }

    形参名称为id,但是这里使用value=" item_id"限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。

    注意:如果请求参数中没有item_id将抛出异常:

    四 绑定pojo类型

    如果提交的参数很多,或者提交的表单中的内容很多的时候可以使用pojo接收数据。要求pojo对象中的属性名和表单中input的name属性一致。

    页面定义如下;

    <input type="text" name="name"/>

    <input type="text" name="price"/>

    Pojo定义:

    请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。

    @RequestMapping("/updateitem")

         public String updateItem(Items items) {

               itemService.updateItem(items);

               return "success";

         }

    注意:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。

    五 绑定数组

    @RequestMapping("/queryitem.action")
        public String queryItem (Model model, int[] ids) {
            //将查询结果保存在model中
            System.out.println(ids);
            //页面转发
            return "forward:/item/itemList.action";
        }

    六 绑定表单提交的list集合

        @RequestMapping("/queryitem.action")
        public String queryItem (QueryVo queryVo, Model model, int[] ids) {
            List<Items> itemList = itemService.findByQueryVo(queryVo);
            //将查询结果保存在model中
            model.addAttribute("itemList", itemList);
            System.out.println(queryVo.getItems().getName());
            System.out.println(queryVo.getItems().getPrice());
            //页面转发
            return "forward:/item/itemList.action";
        }

    QueryVo中的参数

    package cn.rodge.ssm.pojo;

    import java.util.List;

    public class QueryVo {
        private Items items;
        private Integer ids;
        private List<Items> itemList;

        public Integer getIds() {
            return ids;
        }
        public void setIds(Integer ids) {
            this.ids = ids;
        }
        public List<Items> getItemList() {
            return itemList;
        }
        public void setItemList(List<Items> itemList) {
            this.itemList = itemList;
        }
        public Items getItems() {
            return items;
        }
        public void setItems(Items items) {
            this.items = items;
        }
    }

    五 解决POST提交中文乱码的问题

      在web.xml中配置字符集编码过滤器

    <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>

         </filter>

         <filter-mapping>

               <filter-name>CharacterEncodingFilter</filter-name>

               <url-pattern>/*</url-pattern>

         </filter-mapping>

  • 相关阅读:
    vector
    vector-back
    vector-back
    vector-begin
    vector-begin
    vector-at
    vector-at
    Vector-assign
    Vector-assign
    Vector-Constructors
  • 原文地址:https://www.cnblogs.com/rodge-run/p/6545215.html
Copyright © 2011-2022 走看看