转换器和格式要求及传参jackson之页面

   

  • @Controller //转换器   例如 user{id,name,age}  ,请求1-zs-23.  
  • public class ServletConvert {
  •     @RequestMapping("MyConvert")
  •     public String convert(@RequestParam("stu")Student stu){
  •         System.out.println(stu);
  •         return "MyJsp";
  •     }
  •  
  •     //自定义转换器,实现Converter接口
  • public class MyConvert implements Converter<String,Student>{
  •         @Override
  •         public Student convert(String source) {
  •             String[] str = source.split("-");
  •             System.out.println(str);
  •             Student stu = new Student();
  •             stu.setId(Integer.parseInt(str[0]));
  •             stu.setName(str[1]);
  •             return stu;
  •         }
  •     }
  • }
  • @Controller //转换器   例如 user{id,name,age}  ,请求1-zs-23.  
    • public class ServletConvert {
    •     @RequestMapping("MyConvert")
    •     public String convert(@RequestParam("stu")Student stu){
    •         System.out.println(stu);
    •         return "MyJsp";
    •     }

         

    •   //自定义转换器,实现Converter接口
    • public class MyConvert implements Converter<String,Student>{
    •         @Override
    •         public Student convert(String source) {
    •             String[] str = source.split("-");
    •             System.out.println(str);
    •             Student stu = new Student();
    •             stu.setId(Integer.parseInt(str[0]));
    •             stu.setName(str[1]);
    •             return stu;
    •         }
    •     }
    • }

   

   

   

  •     <!--  springmvc的配置 -->
  •     <!-- 配置该注解可以解决很多兼容问题并存问题
  • conversion-service="ConversionService"  将转换器加入到mvc中IOC转换器
  • 统一管理器的class=org.springframework.format.support.FormattingConversionServiceFactoryBean
  • //全部可以转换
  • //自定义类型转化,数据转换不了 org.springframework.context.support.ConversionServiceFactoryBean
  • -->
  •         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
  •         <!-- 自定义转化器 -->
  •         <!-- 第一步,将自定义myConvert纳入IOC容器 -->
  •             <bean id="myConvert" class="cn.tri.convert.MyConvert"></bean>
  •         <!-- 第二步 ,将自定义转换器加入到统一管理器中-->
  •             <!-- <bean id="conversionService" class="org.springframework.format.support.ConversionServiceFactoryBean"> -->
  •             <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  •                 <property name="converters">
  •                     <set>
  •                         <ref bean="myConvert"/>
  •                     </set>
  •                 </property>
  •             </bean>

   

   

   

   

  •     @RequestMapping("testDateTimeFormat")
  •     public String testDateTimeFormat(Student stu,BindingResult result){
  •         System.out.println(stu);
  •         if(result.getErrorCount() > 0){ // 错误信息总数大于1条,将遍历打印
  • for (FieldError error : result.getFieldErrors()) {
  •                 System.out.println(error.getDefaultMessage());
  •             }
  •         }
  •         return "MyJsp";
  •     }

   

   

   

   

  •     @ResponseBody     //此注解直接像页面写值
  • @RequestMapping("testAjaxJson")   //因为添加了jackson.jar  返回为List,实际为到达页面后为json
  • public List<Student> testAjaxJson(){
  •         System.out.println("------------join---testAjaxJson-------------");
  •         Student stu1 = new Student(1, "zs", new Date(), null);
  •         Student stu2 = new Student(1, "ls", new Date(), null);
  •         Student stu3 = new Student(1, "ww", new Date(), null);
  •         List<Student> list = new ArrayList<>();
  •         list.add(stu1);
  •         list.add(stu2);
  •         list.add(stu3);
  •         return list;
  •     }

   

  •     //ajax请求
  •     <script type="text/javascript">
  •        $(document).ready(function(){
  •       $("#btn1").click(function(){
  •        $.post(
  •       "testAjaxJson",
  •        function (result){//json数组
                                for (var i = 0; i < result.length; i++) {
  •                              console.log(result[i].id+"---"+result[i].name+"---"+result[i].birthday);
  •                              alert(result[i].id+"---"+result[i].name+"---"+result[i].birthday);
  •                          }
  •                      }
  •        );
  •        });
  •        });
  •       </script>

   

posted @ 2020-10-17 14:14  黑质白章  阅读(101)  评论(0编辑  收藏  举报