Package org.apache.struts2.json
Class JSONResult
java.lang.Object
org.apache.struts2.json.JSONResult
- All Implemented Interfaces:
- Serializable,- Result
This result serializes an action into JSON.
Result parameters:
- excludeProperties - list of regular expressions matching the properties to be excluded. The regular expressions are evaluated against the OGNL expression representation of the properties.
Example:
<!-- START SNIPPET: example --> <result name="success" type="json" /> <!-- END SNIPPET: example -->
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThis result type doesn't have a default param, null is ok to reduce noise in logs
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected StringaddCallbackIfApplicable(jakarta.servlet.http.HttpServletRequest request, String json) protected SMDbuildSMDObject(ActionInvocation invocation) protected StringcreateJSONString(jakarta.servlet.http.HttpServletRequest request, Object rootObject) protected booleanenableGzip(jakarta.servlet.http.HttpServletRequest request) voidexecute(ActionInvocation invocation) protected ObjectfindRootObject(ActionInvocation invocation) protected StringRetrieve the encodingGets a list of regular expressions of properties to exclude from the JSON output.getRoot()booleanbooleanbooleanbooleanbooleanbooleanbooleanprotected ObjectreadRootObject(ActionInvocation invocation) voidsetCallbackParameter(String callbackParameter) voidsetContentType(String contentType) voidsetDefaultDateFormat(String defaultDateFormat) voidsetDefaultEncoding(String val) voidsetDevMode(String val) voidsetEnableGZIP(boolean enableGZIP) voidsetEnableSMD(boolean enableSMD) voidsetEncoding(String encoding) If defined will be used instead ofdefaultEncoding, you can define it with result <result name="success" type="json"> <param name="encoding">UTF-8</param> </result>voidsetEnumAsBean(boolean enumAsBean) voidsetErrorCode(int errorCode) voidsetExcludeNullProperties(boolean excludeNullProperties) voidsetExcludeProperties(String commaDelim) Sets a comma-delimited list of regular expressions to match properties that should be excluded from the JSON output.voidsetExcludeWildcards(String commaDelim) Sets a comma-delimited list of wildcard expressions to match properties that should be excluded from the JSON output.voidsetIgnoreHierarchy(boolean ignoreHierarchy) voidsetIgnoreInterfaces(boolean ignoreInterfaces) voidsetIncludeProperties(String commaDelim) Sets a comma-delimited list of regular expressions to match properties that should be included in the JSON output.voidsetIncludeWildcards(String commaDelim) Sets a comma-delimited list of wildcard expressions to match properties that should be included in the JSON output.voidsetJsonUtil(JSONUtil jsonUtil) voidsetNoCache(boolean noCache) voidsetPrefix(boolean prefix) voidSets the root object to be serialized, defaults to the Action.voidsetStatusCode(int statusCode) voidsetWrapPrefix(String wrapPrefix) voidsetWrapSuffix(String wrapSuffix) voidsetWrapWithComments(boolean wrapWithComments) protected voidwriteToResponse(jakarta.servlet.http.HttpServletResponse response, String json, boolean gzip) 
- 
Field Details- 
DEFAULT_PARAMThis result type doesn't have a default param, null is ok to reduce noise in logs
 
- 
- 
Constructor Details- 
JSONResultpublic JSONResult()
 
- 
- 
Method Details- 
setDefaultEncoding
- 
setDevMode
- 
setJsonUtil
- 
getExcludePropertiesListGets a list of regular expressions of properties to exclude from the JSON output.- Returns:
- A list of compiled regular expression patterns
 
- 
setExcludePropertiesSets a comma-delimited list of regular expressions to match properties that should be excluded from the JSON output.- Parameters:
- commaDelim- A comma-delimited list of regular expressions
 
- 
setExcludeWildcardsSets a comma-delimited list of wildcard expressions to match properties that should be excluded from the JSON output.- Parameters:
- commaDelim- A comma-delimited list of wildcard patterns
 
- 
getIncludePropertiesList- Returns:
- the includeProperties
 
- 
setIncludePropertiesSets a comma-delimited list of regular expressions to match properties that should be included in the JSON output.- Parameters:
- commaDelim- A comma-delimited list of regular expressions
 
- 
setIncludeWildcardsSets a comma-delimited list of wildcard expressions to match properties that should be included in the JSON output.- Parameters:
- commaDelim- A comma-delimited list of wildcard patterns
 
- 
execute
- 
readRootObject
- 
findRootObject
- 
createJSONStringprotected String createJSONString(jakarta.servlet.http.HttpServletRequest request, Object rootObject) throws JSONException - Throws:
- JSONException
 
- 
enableGzipprotected boolean enableGzip(jakarta.servlet.http.HttpServletRequest request) 
- 
writeToResponseprotected void writeToResponse(jakarta.servlet.http.HttpServletResponse response, String json, boolean gzip) throws IOException - Throws:
- IOException
 
- 
buildSMDObject
- 
getEncodingRetrieve the encoding- Returns:
- The encoding associated with this template (defaults to the value of param 'encoding', if empty default to 'struts.i18n.encoding' property)
 
- 
addCallbackIfApplicable
- 
getRoot- Returns:
- OGNL expression of root object to be serialized
 
- 
setRootSets the root object to be serialized, defaults to the Action. If the Action implementsModelDriven, the Model will be used instead, with the logic assuming the Model was pushed onto the top of the stack.- Parameters:
- root- OGNL expression of root object to be serialized
 
- 
isWrapWithCommentspublic boolean isWrapWithComments()- Returns:
- Generated JSON must be enclosed in comments
 
- 
setWrapWithCommentspublic void setWrapWithComments(boolean wrapWithComments) - Parameters:
- wrapWithComments- Wrap generated JSON with comments
 
- 
isEnableSMDpublic boolean isEnableSMD()- Returns:
- Result has SMD generation enabled
 
- 
setEnableSMDpublic void setEnableSMD(boolean enableSMD) - Parameters:
- enableSMD- Enable SMD generation for action, which can be used for JSON-RPC
 
- 
setIgnoreHierarchypublic void setIgnoreHierarchy(boolean ignoreHierarchy) 
- 
setIgnoreInterfacespublic void setIgnoreInterfaces(boolean ignoreInterfaces) - Parameters:
- ignoreInterfaces- Controls whether interfaces should be inspected for method annotations You may need to set to this true if your action is a proxy as annotations on methods are not inherited
 
- 
setEnumAsBeanpublic void setEnumAsBean(boolean enumAsBean) - Parameters:
- enumAsBean- Controls how Enum's are serialized : If true, an Enum is serialized as a name=value pair (name=name()) (default) If false, an Enum is serialized as a bean with a special property _name=name()
 
- 
isEnumAsBeanpublic boolean isEnumAsBean()
- 
isEnableGZIPpublic boolean isEnableGZIP()
- 
setEnableGZIPpublic void setEnableGZIP(boolean enableGZIP) 
- 
isNoCachepublic boolean isNoCache()
- 
setNoCachepublic void setNoCache(boolean noCache) - Parameters:
- noCache- Add headers to response to prevent the browser from caching the response
 
- 
isIgnoreHierarchypublic boolean isIgnoreHierarchy()
- 
isExcludeNullPropertiespublic boolean isExcludeNullProperties()
- 
setExcludeNullPropertiespublic void setExcludeNullProperties(boolean excludeNullProperties) - Parameters:
- excludeNullProperties- Do not serialize properties with a null value
 
- 
setStatusCodepublic void setStatusCode(int statusCode) - Parameters:
- statusCode- Status code to be set in the response
 
- 
setErrorCodepublic void setErrorCode(int errorCode) - Parameters:
- errorCode- Error code to be set in the response
 
- 
setCallbackParameter
- 
getCallbackParameter
- 
setPrefixpublic void setPrefix(boolean prefix) - Parameters:
- prefix- Prefix JSON with "{} &&"
 
- 
setContentType- Parameters:
- contentType- Content type to be set in the response
 
- 
getWrapPrefix
- 
setWrapPrefix- Parameters:
- wrapPrefix- Text to be inserted at the begining of the response
 
- 
getWrapSuffix
- 
setWrapSuffix- Parameters:
- wrapSuffix- Text to be inserted at the end of the response
 
- 
setEncodingIf defined will be used instead ofdefaultEncoding, you can define it with result <result name="success" type="json"> <param name="encoding">UTF-8</param> </result>- Parameters:
- encoding- valid encoding string
 
- 
getDefaultDateFormat
- 
setDefaultDateFormat
 
-