action的result节点下的param的name
<result name="aaa" type="json">
<param name="bbb">ccc</param>
</result>
aaa:是action里对应返回的result的名字这个大家都知道。
bbb:这个是最关键的,一般大家看到网上的基本就写成root。
这个param的name是JSONResult类中对应的属性名,不能乱取名字,不然json的创建就会出错。不同的name对应着JSONResult类中不同的属性,JSONResult根据name选择的不同的属性以及name对应的值对返回的json数据进行封装。
ccc:这个就是param的name对应的取值。一般是Action中的变量名。
主要关注jar包是:struts2-json-plugin-x.x.x.jar。
以及它下面的一个类 JSONResult类。
package org.apache.struts2.json; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.WildcardUtil; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.json.smd.SMD; import org.apache.struts2.json.smd.SMDGenerator; public class JSONResult implements Result { private static final long serialVersionUID = 8624350183189931165L; private static final Logger LOG = LogManager.getLogger(JSONResult.class); public static final String DEFAULT_PARAM = null; private String encoding; private String defaultEncoding = "UTF-8"; private List<Pattern> includeProperties; private List<Pattern> excludeProperties; private String root; private boolean wrapWithComments; private boolean prefix; private boolean enableSMD = false; private boolean enableGZIP = false; private boolean ignoreHierarchy = true; private boolean ignoreInterfaces = true; private boolean enumAsBean = false; private boolean noCache = false; private boolean cacheBeanInfo = true; private boolean excludeNullProperties = false; private String defaultDateFormat = null; private int statusCode; private int errorCode; private String callbackParameter; private String contentType; private String wrapPrefix; private String wrapSuffix; private boolean devMode = false; @Inject("struts.i18n.encoding") public void setDefaultEncoding(String val) { this.defaultEncoding = val; } @Inject("struts.devMode") public void setDevMode(String val) { this.devMode = BooleanUtils.toBoolean(val); } public List<Pattern> getExcludePropertiesList() { return this.excludeProperties; } public void setExcludeProperties(String commaDelim) { Set<String> excludePatterns = JSONUtil.asSet(commaDelim); if (excludePatterns != null) { this.excludeProperties = new ArrayList(excludePatterns.size()); for (String pattern : excludePatterns) { this.excludeProperties.add(Pattern.compile(pattern)); } } } public void setExcludeWildcards(String commaDelim) { Set<String> excludePatterns = JSONUtil.asSet(commaDelim); if (excludePatterns != null) { this.excludeProperties = new ArrayList(excludePatterns.size()); for (String pattern : excludePatterns) { this.excludeProperties.add(WildcardUtil.compileWildcardPattern(pattern)); } } } public List<Pattern> getIncludePropertiesList() { return this.includeProperties; } public void setIncludeProperties(String commaDelim) { this.includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), "regexp"); } public void setIncludeWildcards(String commaDelim) { this.includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), "wildcard"); } public void execute(ActionInvocation invocation) throws Exception { ActionContext actionContext = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest"); HttpServletResponse response = (HttpServletResponse)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); this.cacheBeanInfo = (!this.devMode); try { Object rootObject = readRootObject(invocation); writeToResponse(response, createJSONString(request, rootObject), enableGzip(request)); } catch (IOException exception) { LOG.error(exception.getMessage(), exception); throw exception; } } protected Object readRootObject(ActionInvocation invocation) { if (this.enableSMD) { return buildSMDObject(invocation); } return findRootObject(invocation); } protected Object findRootObject(ActionInvocation invocation) { Object rootObject; Object rootObject; if (this.root != null) { ValueStack stack = invocation.getStack(); rootObject = stack.findValue(this.root); } else { rootObject = invocation.getStack().peek(); } return rootObject; } protected String createJSONString(HttpServletRequest request, Object rootObject) throws JSONException { String json = JSONUtil.serialize(rootObject, this.excludeProperties, this.includeProperties, this.ignoreHierarchy, this.enumAsBean, this.excludeNullProperties, this.defaultDateFormat, this.cacheBeanInfo); json = addCallbackIfApplicable(request, json); return json; } protected boolean enableGzip(HttpServletRequest request) { return (this.enableGZIP) && (JSONUtil.isGzipInRequest(request)); } protected void writeToResponse(HttpServletResponse response, String json, boolean gzip) throws IOException { JSONUtil.writeJSONToResponse(new SerializationParams(response, getEncoding(), isWrapWithComments(), json, false, gzip, this.noCache, this.statusCode, this.errorCode, this.prefix, this.contentType, this.wrapPrefix, this.wrapSuffix)); } protected SMD buildSMDObject(ActionInvocation invocation) { return new SMDGenerator(findRootObject(invocation), this.excludeProperties, this.ignoreInterfaces).generate(invocation); } protected String getEncoding() { String encoding = this.encoding; if (encoding == null) { encoding = this.defaultEncoding; } if (encoding == null) { encoding = System.getProperty("file.encoding"); } if (encoding == null) { encoding = "UTF-8"; } return encoding; } protected String addCallbackIfApplicable(HttpServletRequest request, String json) { if ((this.callbackParameter != null) && (this.callbackParameter.length() > 0)) { String callbackName = request.getParameter(this.callbackParameter); if (StringUtils.isNotEmpty(callbackName)) { json = callbackName + "(" + json + ")"; } } return json; } public String getRoot() { return this.root; } public void setRoot(String root) { this.root = root; } public boolean isWrapWithComments() { return this.wrapWithComments; } public void setWrapWithComments(boolean wrapWithComments) { this.wrapWithComments = wrapWithComments; } public boolean isEnableSMD() { return this.enableSMD; } public void setEnableSMD(boolean enableSMD) { this.enableSMD = enableSMD; } public void setIgnoreHierarchy(boolean ignoreHierarchy) { this.ignoreHierarchy = ignoreHierarchy; } public void setIgnoreInterfaces(boolean ignoreInterfaces) { this.ignoreInterfaces = ignoreInterfaces; } public void setEnumAsBean(boolean enumAsBean) { this.enumAsBean = enumAsBean; } public boolean isEnumAsBean() { return this.enumAsBean; } public boolean isEnableGZIP() { return this.enableGZIP; } public void setEnableGZIP(boolean enableGZIP) { this.enableGZIP = enableGZIP; } public boolean isNoCache() { return this.noCache; } public void setNoCache(boolean noCache) { this.noCache = noCache; } public boolean isIgnoreHierarchy() { return this.ignoreHierarchy; } public boolean isExcludeNullProperties() { return this.excludeNullProperties; } public void setExcludeNullProperties(boolean excludeNullProperties) { this.excludeNullProperties = excludeNullProperties; } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public void setErrorCode(int errorCode) { this.errorCode = errorCode; } public void setCallbackParameter(String callbackParameter) { this.callbackParameter = callbackParameter; } public String getCallbackParameter() { return this.callbackParameter; } public void setPrefix(boolean prefix) { this.prefix = prefix; } public void setContentType(String contentType) { this.contentType = contentType; } public String getWrapPrefix() { return this.wrapPrefix; } public void setWrapPrefix(String wrapPrefix) { this.wrapPrefix = wrapPrefix; } public String getWrapSuffix() { return this.wrapSuffix; } public void setWrapSuffix(String wrapSuffix) { this.wrapSuffix = wrapSuffix; } public void setEncoding(String encoding) { this.encoding = encoding; } public String getDefaultDateFormat() { return this.defaultDateFormat; } @Inject(required=false, value="struts.json.dateformat") public void setDefaultDateFormat(String defaultDateFormat) { this.defaultDateFormat = defaultDateFormat; } }