Define an interceptor by yourself:
Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actioninvocation;
Import com. opensymphony. xwork2.interceptor. abstractinterceptor;
Import com. soumall. admin. utils. jsonutils;
Import java. util. hashmap;
Import java. util. Map;
Public class jsonparamreadinterceptor extends actinterceptor {
Private Static final string jsonparamdata = "jsonrequestparameter ";
@ Override
Public String intercept (actioninvocation Invocation) throws exception {
Actioncontext CTX = actioncontext. getcontext ();
Map <string, Object> requestparam = CTX. getparameters ();
Boolean excuteflag = false;
If (requestparam! = NULL ){
String [] jsonparamobj = (string []) requestparam. Get (jsonparamdata );
If (jsonparamobj! = NULL & jsonparamobj. length> 0 ){
String jsonparamobjstr = (string) (jsonparamobj [0]);
Requestparam = jsonutils. json2map (jsonparamobjstr, requestparam, null, null );
If (requestparam! = NULL & requestparam. Size ()> 0 ){
Excuteflag = true;
For (Object key: requestparam. keyset ()){
System. Out. Print ("key:" + key );
System. Out. println ("value:" + requestparam. Get (key ));
}
}
}
}
If (! Excuteflag ){
System. Out. println ("No JSON paramter or JSON paramter's name have error ");
}
Return invocation. Invoke ();
}
}
/**
* Fill in the data in jsonobject to map.
*
* @ Param <t> Object
* @ Param jsonobjectstr JSON string
* @ Param entity the node that needs to be filled with data
* @ Param excludes the attribute array that does not need to be converted
* @ Param datepattern date conversion mode
* @ Return t
* @ Throws exception java. Lang. instantiationexception,
* Java. Beans. introspectionexception,
* Java. Lang. illegalaccessexception
*/
Public static Map <string, Object> json2map (string jsonobjectstr,
Map <string, Object> map, string [] excludes, string datepattern)
Throws exception {
Jsonobject = jsonobject. fromobject (jsonobjectstr );
// Jsonutils. configjson (excludes, datepattern );
Set <string> excludeset = createexcludeset (excludes );
For (Object object: jsonobject. entryset ()){
Map. Entry entry = (Map. Entry) object;
String propertyname = entry. getkey (). tostring ();
If (excludeset. Contains (propertyname )){
Continue;
}
String propertyvalue = entry. getvalue (). tostring ();
Map. Put (propertyname, propertyvalue );
}
Return map;
}
2, modify the configuration file struts-default.xml <! -- JSON parm driver --> <interceptor name = "jsonparamdriven" class = "com. soumall. admin. Interceptor. jsonparamreadinterceptor"/> <! -- JSON parm driver --> <Interceptor-ref name = "jsonparamdriven"/>