Struts2插件 —— struts2-json-plugin-2.x.x

为了方便AJAX与服务器进行数据交换,struts2中加入的json插件可用于对象的序列化和反序列化。


相关JAR

struts2-json-plugin.2.x.x.jar


修改Struts2配置文件

  • 更改package元素的extends属性
    1. <span style="font-size:12px"><package name="default" extends="json-default"></span>  
  • 配置result元素的type属性为json,将把Action中属性序列化返回
    1. <span style="font-size:12px"><result type="json" /> </span>  
  • 如果需要处理以JSON文本提交的请求,可在action元素中添加子元素interceptor引用JSON插件包中声明的拦截器:json。
    1. <span style="font-size:12px"><interceptor-ref name="json" /></span>  

result type参数

result type对应的类为:org.apache.struts2.json.JSONInterceptor。下面是一些常用的result param

root

<paramname="root">person</param>

excludeNullProperties 

是否去掉值为null的属性, 默认值为false

<paramname="excludeNullProperties">true</param>

ignoreHierarchy 

是否忽略继承关系,ignoreHierarchy值默认为true。设置为false后,父类的属性也将被构建在JSON字符串中。

<paramname="ignoreHierarchy">false</param>

includeProperties

响应结果需要包含的属性值,支持正则表达式,可用","分隔以使用多个正则表达式匹配。

<paramname="includeProperties">person.*, person\.name</param>

excludeProperties 

响应结果需要排除的属性值,支持正则表达式,可用“,”分隔以使用多个正则表达式匹配。

JSONInterceptor源码:

  1. <span style="font-size:12px">/** 
  2.  * Populates an action from a JSON string 
  3.  */  
  4. public class JSONInterceptor extends AbstractInterceptor {  
  5.     private static final long serialVersionUID = 4950170304212158803L;  
  6.     private static final Logger LOG = LoggerFactory.getLogger(JSONInterceptor.class);  
  7.     private boolean enableSMD = false;  
  8.     private boolean enableGZIP = false;  
  9.     private boolean wrapWithComments;  
  10.     private boolean prefix;  
  11.     private String defaultEncoding = "ISO-8859-1";  
  12.     private boolean ignoreHierarchy = true;  
  13.     private String root;  
  14.     private List<Pattern> excludeProperties;  
  15.     private List<Pattern> includeProperties;  
  16.     private boolean ignoreSMDMethodInterfaces = true;  
  17.     private JSONPopulator populator = new JSONPopulator();  
  18.     private JSONCleaner dataCleaner = null;  
  19.     private boolean debug = false;  
  20.     private boolean noCache = false;  
  21.     private boolean excludeNullProperties;  
  22.     private String callbackParameter;  
  23.     private String contentType;  
  24.   
  25.     @SuppressWarnings("unchecked")  
  26.     public String intercept(ActionInvocation invocation) throws Exception {  
  27.         HttpServletRequest request = ServletActionContext.getRequest();  
  28.         HttpServletResponse response = ServletActionContext.getResponse();  
  29.         String contentType = request.getHeader("content-type");  
  30.         if (contentType != null) {  
  31.             int iSemicolonIdx;  
  32.             if ((iSemicolonIdx = contentType.indexOf(";")) != -1)  
  33.                 contentType = contentType.substring(0, iSemicolonIdx);  
  34.         }  
  35.   
  36.         Object rootObject = null;  
  37.         if (this.root != null) {  
  38.             ValueStack stack = invocation.getStack();  
  39.             rootObject = stack.findValue(this.root);  
  40.   
  41.             if (rootObject == null) {  
  42.                 throw new RuntimeException("Invalid root expression: '" + this.root + "'.");  
  43.             }  
  44.         }  
  45.   
  46.         if ((contentType != null) && contentType.equalsIgnoreCase("application/json")) {  
  47.             // load JSON object  
  48.             Object obj = JSONUtil.deserialize(request.getReader());  
  49.   
  50.             if (obj instanceof Map) {  
  51.                 Map json = (Map) obj;  
  52.   
  53.                 // clean up the values  
  54.                 if (dataCleaner != null)  
  55.                     dataCleaner.clean("", json);  
  56.   
  57.                 if (rootObject == null// model overrides action  
  58.                     rootObject = invocation.getStack().peek();  
  59.   
  60.                 // populate fields  
  61.                 populator.populateObject(rootObject, json);  
  62.             } else {  
  63.                 LOG.error("Unable to deserialize JSON object from request");  
  64.                 throw new JSONException("Unable to deserialize JSON object from request");  
  65.             }  
  66.         } else if ((contentType != null) && contentType.equalsIgnoreCase("application/json-rpc")) {  
  67.             Object result;  
  68.             if (this.enableSMD) {  
  69.                 // load JSON object  
  70.                 Object obj = JSONUtil.deserialize(request.getReader());  
  71.   
  72.                 if (obj instanceof Map) {  
  73.                     Map smd = (Map) obj;  
  74.   
  75.                     if (rootObject == null// model makes no sense when using RPC  
  76.                         rootObject = invocation.getAction();  
  77.   
  78.                     // invoke method  
  79.                     try {  
  80.                         result = this.invoke(rootObject, smd);  
  81.                     } catch (Exception e) {  
  82.                         RPCResponse rpcResponse = new RPCResponse();  
  83.                         rpcResponse.setId(smd.get("id").toString());  
  84.                         rpcResponse.setError(new RPCError(e, RPCErrorCode.EXCEPTION, getDebug()));  
  85.   
  86.                         result = rpcResponse;  
  87.                     }  
  88.                 } else {  
  89.                     String message = "SMD request was not in the right format. See http://json-rpc.org";  
  90.   
  91.                     RPCResponse rpcResponse = new RPCResponse();  
  92.                     rpcResponse.setError(new RPCError(message, RPCErrorCode.INVALID_PROCEDURE_CALL));  
  93.                     result = rpcResponse;  
  94.                 }  
  95.             } else {  
  96.                 String message = "Request with content type of 'application/json-rpc' was received but SMD is "  
  97.                         + "not enabled for this interceptor. Set 'enableSMD' to true to enable it";  
  98.   
  99.                 RPCResponse rpcResponse = new RPCResponse();  
  100.                 rpcResponse.setError(new RPCError(message, RPCErrorCode.SMD_DISABLED));  
  101.                 result = rpcResponse;  
  102.             }  
  103.   
  104.             String json = JSONUtil.serialize(result, excludeProperties, getIncludeProperties(),  
  105.                     ignoreHierarchy, excludeNullProperties);  
  106.             json = addCallbackIfApplicable(request, json);  
  107.             boolean writeGzip = enableGZIP && JSONUtil.isGzipInRequest(request);  
  108.             JSONUtil.writeJSONToResponse(new SerializationParams(response, this.defaultEncoding,  
  109.                     this.wrapWithComments, json, true, writeGzip, noCache, -1, -1, prefix, "application/json"));  
  110.   
  111.             return Action.NONE;  
  112.         } else {  
  113.             if (LOG.isDebugEnabled()) {  
  114.                 LOG.debug("Content type must be 'application/json' or 'application/json-rpc'. " +  
  115.                           "Ignoring request with content type " + contentType);  
  116.             }  
  117.         }  
  118.   
  119.         return invocation.invoke();  
  120.     }  
  121.   
  122.     @SuppressWarnings("unchecked")  
  123.     public RPCResponse invoke(Object object, Map data) throws IllegalArgumentException,  
  124.             IllegalAccessException, InvocationTargetException, JSONException, InstantiationException,  
  125.             NoSuchMethodException, IntrospectionException {  
  126.   
  127.         RPCResponse response = new RPCResponse();  
  128.   
  129.         // validate id  
  130.         Object id = data.get("id");  
  131.         if (id == null) {  
  132.             String message = "'id' is required for JSON RPC";  
  133.             response.setError(new RPCError(message, RPCErrorCode.METHOD_NOT_FOUND));  
  134.             return response;  
  135.         }  
  136.         // could be a numeric value  
  137.         response.setId(id.toString());  
  138.   
  139.         // the map is going to have: 'params', 'method' and 'id' (for the  
  140.         // client to identify the response)  
  141.         Class clazz = object.getClass();  
  142.   
  143.         // parameters  
  144.         List parameters = (List) data.get("params");  
  145.         int parameterCount = parameters != null ? parameters.size() : 0;  
  146.   
  147.         // method  
  148.         String methodName = (String) data.get("method");  
  149.         if (methodName == null) {  
  150.             String message = "'method' is required for JSON RPC";  
  151.             response.setError(new RPCError(message, RPCErrorCode.MISSING_METHOD));  
  152.             return response;  
  153.         }  
  154.   
  155.         Method method = this.getMethod(clazz, methodName, parameterCount);  
  156.         if (method == null) {  
  157.             String message = "Method " + methodName + " could not be found in action class.";  
  158.             response.setError(new RPCError(message, RPCErrorCode.METHOD_NOT_FOUND));  
  159.             return response;  
  160.         }  
  161.   
  162.         // parameters  
  163.         if (parameterCount > 0) {  
  164.             Class[] parameterTypes = method.getParameterTypes();  
  165.             Type[] genericTypes = method.getGenericParameterTypes();  
  166.             List invocationParameters = new ArrayList();  
  167.   
  168.             // validate size  
  169.             if (parameterTypes.length != parameterCount) {  
  170.                 // size mismatch  
  171.                 String message = "Parameter count in request, " + parameterCount  
  172.                         + " do not match expected parameter count for " + methodName + ", "  
  173.                         + parameterTypes.length;  
  174.   
  175.                 response.setError(new RPCError(message, RPCErrorCode.PARAMETERS_MISMATCH));  
  176.                 return response;  
  177.             }  
  178.   
  179.             // convert parameters  
  180.             for (int i = 0; i < parameters.size(); i++) {  
  181.                 Object parameter = parameters.get(i);  
  182.                 Class paramType = parameterTypes[i];  
  183.                 Type genericType = genericTypes[i];  
  184.   
  185.                 // clean up the values  
  186.                 if (dataCleaner != null)  
  187.                     parameter = dataCleaner.clean("[" + i + "]", parameter);  
  188.   
  189.                 Object converted = populator.convert(paramType, genericType, parameter, method);  
  190.                 invocationParameters.add(converted);  
  191.             }  
  192.   
  193.             response.setResult(method.invoke(object, invocationParameters.toArray()));  
  194.         } else {  
  195.             response.setResult(method.invoke(object, new Object[0]));  
  196.         }  
  197.   
  198.         return response;  
  199.     }  
  200.   
  201.     @SuppressWarnings("unchecked")  
  202.     private Method getMethod(Class clazz, String name, int parameterCount) {  
  203.         Method[] smdMethods = JSONUtil.listSMDMethods(clazz, ignoreSMDMethodInterfaces);  
  204.   
  205.         for (Method method : smdMethods) {  
  206.             if (checkSMDMethodSignature(method, name, parameterCount)) {  
  207.                 return method;  
  208.             }  
  209.         }  
  210.         return null;  
  211.     }  
  212.   
  213.     /** 
  214.      * Look for a method in clazz carrying the SMDMethod annotation with 
  215.      * matching name and parametersCount 
  216.      *  
  217.      * @return true if matches name and parameterCount 
  218.      */  
  219.     private boolean checkSMDMethodSignature(Method method, String name, int parameterCount) {  
  220.   
  221.         SMDMethod smdMethodAnntotation = method.getAnnotation(SMDMethod.class);  
  222.         if (smdMethodAnntotation != null) {  
  223.             String alias = smdMethodAnntotation.name();  
  224.             boolean paramsMatch = method.getParameterTypes().length == parameterCount;  
  225.             if (((alias.length() == 0) && method.getName().equals(name) && paramsMatch)  
  226.                     || (alias.equals(name) && paramsMatch)) {  
  227.                 return true;  
  228.             }  
  229.         }  
  230.   
  231.         return false;  
  232.     }  
  233.   
  234.     protected String addCallbackIfApplicable(HttpServletRequest request, String json) {  
  235.         if ((callbackParameter != null) && (callbackParameter.length() > 0)) {  
  236.             String callbackName = request.getParameter(callbackParameter);  
  237.             if ((callbackName != null) && (callbackName.length() > 0))  
  238.                 json = callbackName + "(" + json + ")";  
  239.         }  
  240.         return json;  
  241.     }  
  242.   
  243.     public boolean isEnableSMD() {  
  244.         return this.enableSMD;  
  245.     }  
  246.   
  247.     public void setEnableSMD(boolean enableSMD) {  
  248.         this.enableSMD = enableSMD;  
  249.     }  
  250.   
  251.     /** 
  252.      * Ignore annotations on methods in interfaces You may need to set to this 
  253.      * true if your action is a proxy/enhanced as annotations are not inherited 
  254.      */  
  255.     public void setIgnoreSMDMethodInterfaces(boolean ignoreSMDMethodInterfaces) {  
  256.         this.ignoreSMDMethodInterfaces = ignoreSMDMethodInterfaces;  
  257.     }  
  258.   
  259.     /** 
  260.      * Wrap generated JSON with comments. Only used if SMD is enabled. 
  261.      *  
  262.      * @param wrapWithComments 
  263.      */  
  264.     public void setWrapWithComments(boolean wrapWithComments) {  
  265.         this.wrapWithComments = wrapWithComments;  
  266.     }  
  267.   
  268.     @Inject(StrutsConstants.STRUTS_I18N_ENCODING)  
  269.     public void setDefaultEncoding(String val) {  
  270.         this.defaultEncoding = val;  
  271.     }  
  272.   
  273.     /** 
  274.      * Ignore properties defined on base classes of the root object. 
  275.      *  
  276.      * @param ignoreHierarchy 
  277.      */  
  278.     public void setIgnoreHierarchy(boolean ignoreHierarchy) {  
  279.         this.ignoreHierarchy = ignoreHierarchy;  
  280.     }  
  281.   
  282.     /** 
  283.      * Sets the root object to be deserialized, defaults to the Action 
  284.      *  
  285.      * @param root 
  286.      *            OGNL expression of root object to be serialized 
  287.      */  
  288.     public void setRoot(String root) {  
  289.         this.root = root;  
  290.     }  
  291.   
  292.     /** 
  293.      * Sets the JSONPopulator to be used 
  294.      *  
  295.      * @param populator 
  296.      *            JSONPopulator 
  297.      */  
  298.     public void setJSONPopulator(JSONPopulator populator) {  
  299.         this.populator = populator;  
  300.     }  
  301.   
  302.     /** 
  303.      * Sets the JSONCleaner to be used 
  304.      *  
  305.      * @param dataCleaner 
  306.      *            JSONCleaner 
  307.      */  
  308.     public void setJSONCleaner(JSONCleaner dataCleaner) {  
  309.         this.dataCleaner = dataCleaner;  
  310.     }  
  311.   
  312.     /** 
  313.      * @return true if debugging is turned on 
  314.      */  
  315.     public boolean getDebug() {  
  316.         Boolean devModeOverride = FilterDispatcher.getDevModeOverride();  
  317.         return devModeOverride != null ? devModeOverride.booleanValue() : this.debug;  
  318.     }  
  319.   
  320.     /** 
  321.      * Turns debugging on or off 
  322.      *  
  323.      * @param debug 
  324.      *            true or false 
  325.      */  
  326.     public void setDebug(boolean debug) {  
  327.         this.debug = debug;  
  328.     }  
  329.   
  330.     @Inject(StrutsConstants.STRUTS_DEVMODE)  
  331.     public void setDevMode(  
  332.         String mode)  
  333.     {  
  334.         setDebug("true".equalsIgnoreCase(mode));  
  335.     }  
  336.   
  337.     /** 
  338.      * Sets a comma-delimited list of regular expressions to match properties 
  339.      * that should be excluded from the JSON output. 
  340.      *  
  341.      * @param commaDelim 
  342.      *            A comma-delimited list of regular expressions 
  343.      */  
  344.     public void setExcludeProperties(String commaDelim) {  
  345.         Set<String> excludePatterns = JSONUtil.asSet(commaDelim);  
  346.         if (excludePatterns != null) {  
  347.             this.excludeProperties = new ArrayList<Pattern>(excludePatterns.size());  
  348.             for (String pattern : excludePatterns) {  
  349.                 this.excludeProperties.add(Pattern.compile(pattern));  
  350.             }  
  351.         }  
  352.     }  
  353.   
  354.     /** 
  355.      * Sets a comma-delimited list of wildcard expressions to match 
  356.      * properties that should be excluded from the JSON output. 
  357.      *  
  358.      * @param commaDelim 
  359.      *            A comma-delimited list of wildcard expressions 
  360.      */  
  361.     public void setExcludeWildcards(String commaDelim) {  
  362.         Set<String> excludePatterns = JSONUtil.asSet(commaDelim);  
  363.         if (excludePatterns != null) {  
  364.             this.excludeProperties = new ArrayList<Pattern>(excludePatterns.size());  
  365.             for (String pattern : excludePatterns) {  
  366.                 this.excludeProperties.add(WildcardUtil.compileWildcardPattern(pattern));  
  367.             }  
  368.         }  
  369.     }  
  370.   
  371.     /** 
  372.      * Sets a comma-delimited list of regular expressions to match properties 
  373.      * that should be included from the JSON output. 
  374.      *  
  375.      * @param commaDelim 
  376.      *            A comma-delimited list of regular expressions 
  377.      */  
  378.     public void setIncludeProperties(String commaDelim) {  
  379.         includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.REGEXP_PATTERN);  
  380.     }  
  381.   
  382.     /** 
  383.      * Sets a comma-delimited list of wildcard expressions to match 
  384.      * properties that should be included from the JSON output.  The 
  385.      * standard boilerplate (id, error, debug) are automatically included, 
  386.      * as appropriate, so you only need to provide patterns for the 
  387.      * contents of "result". 
  388.      *  
  389.      * @param commaDelim 
  390.      *            A comma-delimited list of wildcard expressions 
  391.      */  
  392.     public void setIncludeWildcards(String commaDelim) {  
  393.         includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.WILDCARD_PATTERN);  
  394.         if (includeProperties != null) {  
  395.             includeProperties.add(Pattern.compile("id"));  
  396.             includeProperties.add(Pattern.compile("result"));  
  397.             includeProperties.add(Pattern.compile("error"));  
  398.             includeProperties.add(WildcardUtil.compileWildcardPattern("error.code"));  
  399.         }  
  400.     }  
  401.   
  402.     /** 
  403.      * Returns the appropriate set of includes, based on debug setting. 
  404.      * Derived classes can override if there are additional, custom 
  405.      * debug-only parameters. 
  406.      */  
  407.     protected List getIncludeProperties() {  
  408.         if (includeProperties != null && getDebug()) {  
  409.             List<Pattern> list = new ArrayList<Pattern>(includeProperties);  
  410.             list.add(Pattern.compile("debug"));  
  411.             list.add(WildcardUtil.compileWildcardPattern("error.*"));  
  412.             return list;  
  413.         } else {  
  414.             return includeProperties;  
  415.         }  
  416.     }  
  417.   
  418.     public boolean isEnableGZIP() {  
  419.         return enableGZIP;  
  420.     }  
  421.   
  422.     /** 
  423.      * Setting this property to "true" will compress the output. 
  424.      *  
  425.      * @param enableGZIP 
  426.      *            Enable compressed output 
  427.      */  
  428.     public void setEnableGZIP(boolean enableGZIP) {  
  429.         this.enableGZIP = enableGZIP;  
  430.     }  
  431.   
  432.     public boolean isNoCache() {  
  433.         return noCache;  
  434.     }  
  435.   
  436.     /** 
  437.      * Add headers to response to prevent the browser from caching the response 
  438.      *  
  439.      * @param noCache 
  440.      */  
  441.     public void setNoCache(boolean noCache) {  
  442.         this.noCache = noCache;  
  443.     }  
  444.   
  445.     public boolean isExcludeNullProperties() {  
  446.         return excludeNullProperties;  
  447.     }  
  448.   
  449.     /** 
  450.      * Do not serialize properties with a null value 
  451.      *  
  452.      * @param excludeNullProperties 
  453.      */  
  454.     public void setExcludeNullProperties(boolean excludeNullProperties) {  
  455.         this.excludeNullProperties = excludeNullProperties;  
  456.     }  
  457.   
  458.     public void setCallbackParameter(String callbackParameter) {  
  459.         this.callbackParameter = callbackParameter;  
  460.     }  
  461.   
  462.     public String getCallbackParameter() {  
  463.         return callbackParameter;  
  464.     }  
  465.   
  466.     /** 
  467.      * Add "{} && " to generated JSON 
  468.      *  
  469.      * @param prefix 
  470.      */  
  471.     public void setPrefix(boolean prefix) {  
  472.         this.prefix = prefix;  
  473.     }  
  474.   
  475.     public void setContentType(String contentType) {  
  476.         this.contentType = contentType;  
  477.     }  
  478. }  
  479. </span> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值