zoukankan      html  css  js  c++  java
  • struts2 json返回试验

    <?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="default" namespace="/"
    extends="struts-default, json-default">
    <action name="getDataAction" class="DataAction">
    <result name="success" type="json">
    <param name="root">root</param>
    </result>
    </action>
    </package>
    </struts>

    struts配置 返回类型是json

    import com.opensymphony.xwork2.ActionSupport;

    public class DataAction extends ActionSupport {
    private String root;
    /**
    * @return
    */
    public String execute() {
    root = "hello word";
    return SUCCESS;

    }
    public String getRoot() {
    return root;
    }
    public void setRoot(String root) {
    this.root = root;
    }

    }

    http://localhost:8090/jsondemo/getDataAction.action

    浏览器返回“hello word”

    ----------------------------------------------------_____________________________________________________________________---------------------------------

    import java.util.HashMap;
    import java.util.Map;

    import net.sf.json.JSONObject;

    import com.opensymphony.xwork2.ActionSupport;

    public class DataAction extends ActionSupport {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private JSONObject root;
    /**
    * @return
    */
    public String execute() {
    Map<String, String> respond = new HashMap<String, String>();
    respond.put("a", "hello a");
    respond.put("b", "hello b");
    respond.put("c", "hello c");
    root=JSONObject.fromObject(respond);
    System.out.println(root.toString());
    return SUCCESS;

    }
    public JSONObject getRoot() {
    return root;
    }
    public void setRoot(JSONObject root) {
    this.root = root;
    }


    }

    浏览器返回

    {"b":"hello b","c":"hello c","a":"hello a"}


  • 相关阅读:
    ioctlsocket()
    阻塞式socket例子学习
    accept()
    listen()
    WSAStartup
    C#动态操作DataTable(新增行、列、查询行、列等)
    C# 连接SQL Server数据库的几种方式--server+data source等方式
    ExcelHelper类
    c#使用椭圆签名算法制作软件序列号
    LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count 等
  • 原文地址:https://www.cnblogs.com/mrcharles/p/4739718.html
Copyright © 2011-2022 走看看