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可以定制化输出要显示的字段