A_smile

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

过了这么久,又重新把博客拾起来了

来上海工作也已经有将近两周的时间了,

 今天在整理项目的时候,遇到了一个关于参数注入的问题

背景:

     我的开发前台用的是extjs4,在对后台spring mvc提交表单的时候,出现了400错误,开始我就想400主要就是由于参数注入类型不匹配的问题,我的formdata的格式是这样的

  1. uniqueId:
    1
  2. invoiceIssueItems:
    [{"uniqueId":1,"createAccount":"root","createDate":"2017-12-11 14:26:28","modifyAccount":"root","modifyDate":"2017-12-11 14:26:28","ownerDepartmentCode":"1000","ownerDepartmentName":"","protectLevel":0,"invoiceID":1,"itemName":"rfid扫描器","itemType":"手持机","itemUnit":"个","itemCount":3,"itemUnitPrice":12,"totalPrice":45,"taxRate":0.12,"totalTax":23.12,"taxandPrice":34,"invoiceNoRight":"B23447","invoiceDate":"2017-12-11 14:26:28","clientName":"解"},{"uniqueId":2,"createAccount":"root","createDate":"2017-12-11 14:26:33","modifyAccount":"root","modifyDate":"2017-12-11 14:26:33","ownerDepartmentCode":"1000","ownerDepartmentName":"","protectLevel":0,"invoiceID":1,"itemName":"iphone x","itemType":"手机","itemUnit":"个","itemCount":4,"itemUnitPrice":12,"totalPrice":45,"taxRate":0.12,"totalTax":222.12,"taxandPrice":100,"invoiceNoRight":"B23447","invoiceDate":"2017-12-11 14:26:33","clientName":"小红"}]
  3. contractNo:
    AS6351
  4. invoiceNoLeft:
    A7834
  5. invoiceDate:
    2017-12-11 14:26:21
  6. companyBankName:
    工商银行


  7. 我对应的实体是

  8. invoice{

  9.   基本字段;

  10.  List<invoiceIssueItem> invoiceIssueItem;                            ///伪代码希望多多见谅

  11. }

  12. 第一次出现400,我从网上找到是因为日期格式不对,不能format,网上有很多解决方案。我这里就不写了

  13. 第二次出现400,我也找到是因为List<invoiceIssueItem>数据注入不进去,开始我还以为是里面的字段不匹配造成的,因此我特地将每个字段都尝试了一下,但是效果还是那样,我为了找出到底是哪个字段的问题,我将spring的log4j

  14. 的日记级别调到了debug级别,最后也只是看到Object invoice   的字段invoiceIssueItems 拒绝注入。


  15. 最终的还是解决了这个问题,原来spring mvc对这种嵌套的复杂对象是不可以自动注入的,这种获取表单复杂类型的数据有两种方法

  16. 1.第一种比较简单,就是给提交的方法一个HttpServletRequest的request用request.getParamers("fieldname");获得

  17. 2.第二种要复杂一点,spring mvc对这种复杂数据类型提供了一种自定义注入类型,在改类初始化时绑定数据有一个@InitBinder注解,该注解可以实现在spring mvc获取表单的时候,来执行自己定义的注入方法

  18. @InitBinder
     public void initBinder(WebDataBinder binder) {
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

      binder.registerCustomEditor(List.class, "invoiceIssueItem", new CustomItemsEditor());
     }

  19. 第一个是处理时间类型,由spring mvc提供,第二种 处理自定义类型CustomItemsEditor()是一个自定义类,该类继承了PropertiesEditor实现类,该类又实现了PropertyEditor接口

  20. PropertiesEditor类的方法在网上也比较容易找到,我这里要用的是其中的setAsTest(String json)方法
     
     public class CustomItemsEditor extends PropertiesEditor {
       public void setAsText(String str) {   
        List<ClientContact> lists = JsonHelper.getObjectsFromJson(str, ClientContact.class);   
        super.setValue(lists);
       }
     };

     在这里将页面传进来的值,经过自定义的格式化,再注入到bean中

  21. 我用的是jackson包来讲json数据转化成List<object>

  22. 方法在下面

  23. public static <T> List<T> getObjectsFromJson(String json, Class<T> myClass) {
      if(json == null) {
       return new ArrayList<T>();
      }
      JSONArray jsonArray = (JSONArray)JSONSerializer.toJSON(json);
      JsonConfig jsonConfig = new JsonConfig();
      jsonConfig.setArrayMode(JsonConfig.MODE_LIST);
      jsonConfig.setRootClass(myClass);
      List<T> filters = (List<T>)JSONSerializer.toJava(jsonArray, jsonConfig);
      return filters;
     }

在接下来我会尽量多的详细的记录我在学习中遇到的问题,如果转载请注明出处

小白菜一枚,希望各位大神不吝赐教------------------------------------------------ 

posted on 2017-12-11 19:40  气场两米八  阅读(778)  评论(0编辑  收藏  举报