struts2JSON插件,拦截器

1.     JSON插件

想要使用json必定会用到jquery。

1.1        导入相关的Ajax-jar包-01资料中ajax文件中。

commons-beanutils-1.8.0.jar

commons-collections-3.1.jar

commons-lang-2.4.jar

commons-logging-1.1.3.jar

ezmorph-1.0.6.jar

jackson-core-asl-1.9.2.jar

jackson-mapper-asl-1.9.2.jar

json-lib-2.3-jdk15.jar

struts2-json-plugin-2.3.24.jar

还要有struts2的相关jar包。

1.2        创建对应的页面。Success.jsp

<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>

<script type="text/javascript">

      $(function(){

           /* 使用ajax技术进行提交 */

           $.ajax({

                 type:"post",

                 url:"/07struts2/getAll.action",

                 dataType:"json",

                 success:function(data){

                      /* 回调函数将data对象中的数据循环添加到页面中 */                      

                      for(var i = 0 ; i < data.length; i++){

                            var con = "<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].pwd+"</td></tr>";

                            // 添加con

                            $("#tab").append(con);

                      }

                 }

           })

      })

</script>

1.3        要完成对应的Action-- getAll.action

//     模拟数据库为user对象添加数据

       private List<User> userList;

public String getAll(){

    //      需要给user赋予数据 模拟的是数据库。从服务层取得到的数据。

              userList = new ArrayList<>();

              userList.add(new User(1, "高圆圆""2345"));

              userList.add(new User(2, "宋慧乔""32345"));

              userList.add(new User(3, "唐嫣""12345"));

              userList.add(new User(4, "陈乔恩""24345"));

              userList.add(new User(5, "赵丽颖""25345"));

              userList.add(new User(6, "陈冠希""256345"));

              return SUCCESS;

       }

1.4        编写struts.xml—将getAll.action 添加

<!-- Struts 要想使用json技术,必须继承json-default: 才可以使用json技术

      struts 继承多个命名空间的时候,要使用,隔开即可!

-->

<package name="default" namespace="/" extends="struts-default,json-default">

<action name="getAll" class="com.bjsxt.action.LoginAction" method="getAll">

                 <!-- 表示该方法返回一个json -->

                 <result type="json">

                      <!-- param:给userList属性从新装备

                            root:表示根,直接将userList可以转化为json

                      -->

                      <param name="root">userList</param>

                 </result>

           </action>

1.5        测试成功!



 

http://blog.csdn.net/m_q_x/article/details/78084696?locationNum=6&fps=1

 

猜你喜欢

转载自zgphacker2010.iteye.com/blog/2401328