struts2+ext.js中对json的个人理解

本文介绍如何在 Struts2 框架中配置 JSON 插件并实现数据交互,通过示例展示如何定义 struts.xml 中的包及 action 来处理 JSON 数据,并返回 JSON 格式的数据列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在处理json对象时


struts.xml的配置

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- struts.xml文件,有且仅有一个叫struts.xml的文件 
作用:配置action
-->
<struts>


<package name="dept" namespace="/" extends="json-default">
      <!-- 处理登录 -->
      <action name="deptListAction" class="com.hydz.action.DeptAction" method="find">
      <result name="json-deptList" type="json">
      </result>

      </action>
</package>

</struts>

@Controller
public class DeptAction extends ActionSupport{
@Resource
private DeptService deptService;
private Dept dept;
private int total;
private Boolean success;
private List<Dept> deptList;
    public String find() {
    deptList=deptService.find();
    total=deptList.size();
    success=true;
    //JSONObject jo=new JSONObject();
    //JSONArray ja = JSONArray.fromObject(deptList);
    //deptList=jo.put("deptlist", ja);//.toString();
       return "json-deptList";
     
}public int getTotal() {
return total;
}


public void setTotal(int total) {
this.total = total;
}
public DeptService getDeptService() {
return deptService;
}
public void setDeptService(DeptService deptService) {
this.deptService = deptService;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public List<Dept> getDeptList() {
return deptList;
}
public void setDeptList(List<Dept> deptList) {
this.deptList = deptList;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
}


在页面接受到的数据为

  1. deptnull
  2.   deptList[{deptName:的说法都是, deptNum:100}, {deptName:第三方, deptNum:101}, {deptName:多少分但是, deptNum:102}]
  3. deptService{}
  4. successtrue
  5. total3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值