Struts2Action+JQuery+jsp获取json数据

1、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>

    <constant name="struts.devMode" value="false" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.configuration.xml.reload" value="true" />

    <!-- 设置文件上传大小 -->
    <constant name="struts.multipart.maxSize" value="536870912" />

    <!-- 设置Struts2默认的ObjectFactory为spring -->
    <constant name="struts.objectFactory" value="spring" />

    <!-- 注解参数设置 -->

    <!-- 设置是否从Jar包里搜索Action类。如果开发者喜欢将Acion类打包成JAR,则应将该常量设为true。默认值为true。 -->
    <constant name="struts.convention.action.disableJarScanning" value="true" />

    <!-- 返回资源的结果路径. -->
    <constant name="struts.convention.result.path" value="/WEB-INF/src/" /> 

    <!-- Convention插件以该常量指定包做为根包来搜索Action类  -->
    <constant name="struts.convention.action.packages" value="com.couont.control" />

    <!--  设置Convention插件文件协议类型 -->
    <constant name="struts.convention.action.fileProtocols" value="jar,vfsfile,vfszip,wsjar,zip" />


    <constant name="struts.convention.default.parent.package" value="struts-global"/>  


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

        <global-results>
            <result name="default">/WEB-INF/system/content/default.jsp</result>

        </global-results>


    </package> 

</struts>

2.default.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
${jsonResultStr} 
jsonResultStr为struts接口中自己封转的数据变量

3.自定义control继承ActionSupport

public abstract class AbstractControl extends ActionSupport {


    String jsonResultStr;

    public abstract String getJsonResultStr() ;
    public String printJson(Object obj,String dataformat){
        jsonResultStr =JsonUtils.object2Json(obj, dataformat);
        return jsonResultStr;
    }

4.自定义struts业务接口

public class FooAction extends AbstractControl
{


    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        jsonResultStr = "{"flag":true,"data":[{"id":25,"name":"你是0女生","content":"欢迎来到超级女声0号现场","posttime":"2017-10-16 18:44:30"},{"id":26,"name":"你是1女生","content":"欢迎来到超级女声1号现场","posttime":"2017-10-16 18:44:30"},{"id":27,"name":"你是2女生","content":"欢迎来到超级女声2号现场","posttime":"2017-10-16 18:44:30"},{"id":28,"name":"你是3女生","content":"欢迎来到超级女声3号现场","posttime":"2017-10-16 18:44:30"},{"id":29,"name":"你是4女生","content":"欢迎来到超级女声4号现场","posttime":"2017-10-16 18:44:30"},{"id":30,"name":"你是5女生","content":"欢迎来到超级女声5号现场","posttime":"2017-10-16 18:44:30"},{"id":31,"name":"你是0男生","content":"欢迎来到快乐男声0号现场","posttime":"2017-10-16 18:44:30"},{"id":32,"name":"你是1男生","content":"欢迎来到快乐男声1号现场","posttime":"2017-10-16 18:44:30"},{"id":33,"name":"你是2男生","content":"欢迎来到快乐男声2号现场","posttime":"2017-10-16 18:44:30"},{"id":34,"name":"你是3男生","content":"欢迎来到快乐男声3号现场","posttime":"2017-10-16 18:44:30"},{"id":35,"name":"你是4男生","content":"欢迎来到快乐男声4号现场","posttime":"2017-10-16 18:44:30"},{"id":36,"name":"你是5男生","content":"欢迎来到快乐男声5号现场","posttime":"2017-10-16 18:44:30"}],"code":0,"info":"操作成功!"} 
";
        return DEFAULT;
    }
}

5.js接口访问

    $.post("/data/foo/foo.action", {},
                    function(data) {                                                                             console.log(data);
                    }, 'json').error(function() {
    //指定第四个参数返回json数据,就可以直接用对象.成员的形式来访问 data中返回的数据了                 
    })
你也许会问为什么不用struts自带的json,为什么自己处理json,自己处理json可以定制化输出要显示的字段
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值