现在的位置: 首页 > 综合 > 正文

struts2+json返回一个list

2018年04月30日 ⁄ 综合 ⁄ 共 1589字 ⁄ 字号 评论关闭

注意点:1在配置文件中配置excludeProperties 或者includeProperties,不需要配置root

        2在页面得到empList的时候

一,在action中

@Controller

public
class
OperateEmp extends ActionSupport {

    @Resource

    private EmpService
empService;

 

private List<Emp>  empList;
//返回这个empList

 

    public String delEmp() {

        //这是我service层的方法,得到一个emp集合

        empsList = empService.getAllEmps();

        return
SUCCESS; //
这里要是SUCCESS

    }

 

    public List<Emp> getEmpList () {

        return
jsonArrary;

    }

 

    public
void
setEmpList (List<Emp> empList) {

        this.empList = empList;

    }

 

}

 

二 配置文件:

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

        <action
name="empAction"class="operateEmp"
method="delEmp">

            <result
type="json">

<!--  这里不需要配置root ,
如果配置了root的话,那么excludeProperties或者includeProperties就没有用了

为什么要用includeProperties或者excludeProperties
参数:

主要是为了过滤掉接口,pojosetlist、其他对象等不需要的数据防止循环取其他对象或找不到。如果不配置,默认是处理action中的所有属性,如果action中有接口注入,json拦截器可能找不到返回不了结果,还有如果action中有一个对象,这个对象与好多对象都有关联,json拦截器会将相关联的所有对象的属性全部转换成json格式,如果其他对象有listset,其返回结果...有可能是死循环,无法返回

excludeProperties 参数:输出结果需要剔除的属性值,也支持正则表达式匹配属性名,可以用“,”分割填充多个正则表达式,类同includeProperties

-->

            <param
name="excludeProperties">empList\[\d+\]\.dep</param>

            </result>
<!-- 无需视图配置 -->

        </action>

    </package>

 

 

三,页面

<script
type="text/javascript">

    function delEmp(empId){

      $.post("${pageContext.request.contextPath}/operateEmp/operate_delEmp.do",{'empId':empId},function(data){

    <!--  要注意的是这里,返回的是一个 {“empList”:[{“empId”:1,”empName”:”hwt”},{…},{…}]}
这种形式,所以,这里要得到empList的值需要注意这里 -->

        $.each(data.empList,function(){

            alert(this.empId);

            });

        });

    }

</script>

 

抱歉!评论已关闭.