struts2-通过action从后台将json集合传到前台的方法(一)

1 篇文章 0 订阅

本方法通过Struts2的拦截器自动将数据转换成json数据后传给页面。

需要导入的jar包:struts2-json-plugin-2.3.20.jar

前台jsp页面:

<table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:250px"
       url="get_user"
       toolbar="#toolbar" pagination="true"
       rownumbers="true" fitColumns="true" singleSelect="true">
  <thead>
  <tr>
    <th field="firstname" width="50">First Name</th>
    <th field="lastname" width="50">Last Name</th>
    <th field="phone" width="50">Phone</th>
    <th field="email" width="50">Email</th>
  </tr>
  </thead>
</table>

其中url是action。URL的返回值必须是json类型。

struts.xml配置文件

<package name="b" namespace="/" extends="struts-default">
        <result-types>
                <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <interceptors>
                <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
                <interceptor-stack name="p1">
                        <interceptor-ref name="defaultStack"/>
                        <interceptor-ref name="json"/>
                </interceptor-stack>
        </interceptors>
        
        <action name="get_user" class="com.action.SyohinGetAction">
                <result type="json">
                        <param name="root">list</param>
                </result>
        </action>
</package>
其中result-type 就是从struts2-json-plugin-2.3.20.jar中引用的。

<action name="get_user" class="com.action.SyohinGetAction">
                <result type="json">
                        <param name="root">list</param>
                </result>
</action>
这个action将list直接转成json数据。(list在后台就是java.util.List)

在初始化jsp页面时会自动调get_user的action显示内容。

json数据的顺序可以与前台不同,但项目必须一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值