zoukankan      html  css  js  c++  java
  • SSH实现ajax

    (1)首先要引入需要pom文件

      <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin -->
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-json-plugin</artifactId>
          <version>2.3.16.3</version>
        </dependency>

    (2)需要的数据,返回什么类型的都可以

        private List<Integer> list;  //要返回前台的值  list集合
        private Integer productId;   //前台获取的值
    
        public Object totalQuantity() {
            System.out.println(productId);
            list = productService.findByID(productId);
            System.out.println(list);
            return "ajax";
        }
    
    //省略getter和setter方法

    (3)配置action

    result节点中的name:返回的值  type:返回序列化的格式   

    root:要序列化的对象  默认将序列化当前Action中所有有返回值的getter方法的值

    ===list( 要返回页面的数据 在action要有getter方法)

     <action name="totalQuantity" class="cn.ssh.action.ProductAction" method="totalQuantity">
                <result name="ajax" type="json" >
                    <param name="root">list</param>
                </result>
            </action>

    (4)前台ajax

      $.ajax({
                       url:"/totalQuantity.action",  //访问action的路径
                       type:"POST",
                       data:{
                             //传递到后台的值
                       },
                       dataType:"JSON",
                       success:function (data) {
    //成功操作 $.each(data, function (i, dom) {
    }); } })
  • 相关阅读:
    jQuery事件
    jQuery的效果
    jQuery 选择器
    中级 jQuery 了解
    回调函数 callback()
    预加载
    表格对象的方法
    script中type属性讲解
    将数据渲染到页面的方式:模版
    将数据渲染到页面的几种方式
  • 原文地址:https://www.cnblogs.com/luoxionghenku/p/9963688.html
Copyright © 2011-2022 走看看