zoukankan      html  css  js  c++  java
  • Struts2返回Json数据(使用Struts2插件)

    这篇我将介绍如何使用Struts2struts2-json-plugin.jar插件返回JSON数据。

    一、其中主要步骤有:

    1.将struts2-json-plugin.jar插件拷贝到项目的"/WEB-INF/lib"文件夹下;

    2.编写Action类文件;

    3.在struts.xml文件中配置这个Action,这个Action所在的"<package.../>"必须继承”json-default“, Action Result  类型为  json ,即  type="json" ,而且不对应任何视图资源。

    二、示例代码:

    Action类文件:

    package com.example.action;
    
    import java.util.ArrayList;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class StrutsJsonAction extends ActionSupport {
        private int i=123;
        private String str="str";
        private int[] array={1,2,3};
        private ArrayList<String> list;
        
        public int getI() {
            return i;
        }
        public void setI(int i) {
            this.i = i;
        }
        public String getStr() {
            return str;
        }
        public void setStr(String str) {
            this.str = str;
        }
        public int[] getArray() {
            return array;
        }
        public void setArray(int[] array) {
            this.array = array;
        }
        public ArrayList<String> getList() {
            return list;
        }
        public void setList(ArrayList<String> list) {
            this.list = list;
        }
        public String execute(){
            list = new ArrayList<String>();
            list.add("red");
            list.add("green");
            list.add("yellow");
            return SUCCESS;
        }
    }

    struts.xml文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
        <package name="json-example" namespace="/" extends="json-default">
            <action name="JSONExample" class="com.example.action.StrutsJsonAction">
                <result name="success" type="json"/>
            </action>
        </package>
    </struts>    

    然后在浏览器中访问"   http://localhost:8080/Struts2_JSON/JSONExample  ",显示结果如图:

      JSON 插件会将所有可序列化  Action  属性序列化为 JSON  数据。

    三、配置常用JSON类型的Result

    浏览器是否缓存JSON

    <result type="json">
      <!-- 取消浏览器缓存-->
      <param name="noCache">true</param>
    </result>

     设置浏览器响应类型,默认为text/html

    <result type="json">
      <!-- 设置服务器响应类型-->
      <param name="contentType">application/json</param>
    </result>

     

    排除值为 null 的属性

    <result type="json">
      <!--排除值为null的属性-->
      <param name="excludeNullProperties">true</param>
    </result>

    只序列化指定的Action属性

    <result type="json">
        <!--只序列化Action内的list属性-->
        <param name="root">list</param>
    </result>

    序列化包含的属性(逗号分隔的正则表达式列表)
    <result type="json">
        <!--序列化list属性-->
        <param name="includeProperties">list.*</param>
    </result>
    <result type="json">
        <!--序列化array属性,[和]匹配数组的[]括号,d匹配数字,+表示一次或多次-->
        <param name="includeProperties">array[d+]</param>
    </
    result>

    排除不需要被序列化的属性(逗号分隔的正则表达式列表)
    <result type="json">
         <!--排除list属性-->
      <param name="excludeProperties"> list.* </param>
    </result>
    
    
    
     
  • 相关阅读:
    我的公众号 猫和大叔(maohedashu)
    centos编译php时出现make: *** [sapi/fpm/phpfpm] Error 1
    如何在centos和linux等主机上安装 pstree
    如何在centos下卸载干净nginx
    监控感悟(一)
    ant design vue 获取上传图片的像素
    ant vue中atree默认展开指定的树节点不起作用
    uniapp中组件属性设置不生效的解决方案
    vue+canvas实现简易画板
    ant design vue级联选择懒加载省市区三级数据
  • 原文地址:https://www.cnblogs.com/liubaozhe/p/4418741.html
Copyright © 2011-2022 走看看