Originally STRUTS2 has its own JSON type of return result, and provides plug-ins, but it has a problem, that is, it will all the serialized fields are returned, if you want to make a return action of a property, you need to configure the result when the configuration parameters (here is just an example):
<name= "root">responsemap</param>
With this parameter configured, the return result is returned from the Responsemap in the action as the root.
However, if you customize the result type, you can control it yourself, and you do not need the Struts2-json-result plug-in, the following is the configuration information:
< Packagename= "Default"extends= "Struts-default"namespace= "/login"> <Result-types> <Result-typename= "Jsonresult"class= "Com.lxl.erp.base.JSONResult" /> </Result-types> <Actionname="*/*"class= "{1}"Method= "{2}"> <resulttype= "Jsonresult"name= "Success" /> </Action></ Package>
Com.lxl.erp.base.JSONResult is the implementation class for the return type that we need to implement:
Packagecom.lxl.erp.base;ImportJava.io.PrintWriter;Importjava.lang.reflect.UndeclaredThrowableException;ImportJava.util.HashMap;ImportJava.util.Locale;ImportJava.util.Map;ImportJava.util.ResourceBundle;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.struts2.ServletActionContext;ImportOrg.apache.struts2.dispatcher.StrutsResultSupport;Importcom.lxl.erp.common.HttpConstant;ImportCom.lxl.erp.common.ResourceLanguage;Importcom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.ExceptionHolder;ImportCom.opensymphony.xwork2.util.ValueStack;ImportNet.sf.json.JSONObject;/*** @ClassName: Jsonresult * @Description: Result fomed by JSON *@authorAaron * @date Nov 7, 4:27:06 PM *@versionV1.0*/ Public Final classJsonresultextendsstrutsresultsupport{Private Static Final LongSerialversionuid = 1L; @Overrideprotected voidDoexecute (String finallocation, actioninvocation invocation)throwsException {baseaction bas=(baseaction) invocation.getaction (); Bas.clearerrorsandmessages (); HttpServletResponse Response=(HttpServletResponse) invocation. Getinvocationcontext (). get (Http_response); Response.setcontenttype (Httpconstant.response_content_type); @SuppressWarnings ("Unchecked") Map<string, object> Responsemap = (map<string, object>) invocation. Getstack (). Findvalue (Httpconstant.responsemap); if(Responsemap = =NULL) {Responsemap=NewHashmap<string, Object> (3); } Else if(Responsemap.get (httpconstant.retcode) = =NULL) {Valuestack s=Invocation.getstack (); for(inti = S.size (); i > 0; i--) {Object obj=S.pop (); if(objinstanceofExceptionholder) {Responsemap.put (Httpconstant.retcode, Httpconstant.error_code); Responsemap.put (httpconstant.retmsg, httpconstant.unknownerror); Object o=((exceptionholder) obj). GetException (); if(Oinstanceofserviceexception) {String Accept_language=Servletactioncontext. Getrequest (). GetHeader (Httpconst Ant. Request_header_lang); String language= Accept_language.split (",") [0]; Locale Locale=Locale.getdefault (); if(Language.tolowercase (). IndexOf (Resourcelanguage.chinese)>-1) {locale=Locale.china; } Else if(Language.tolowercase (). IndexOf (Resourcelanguage.english)>-1) {locale=locale.us; } resourcebundle Bundle=Resourcebundle.getbundle (resourcelanguage.sourcelocation, locale); Serviceexception Exception=(serviceexception) o; Responsemap.put (Httpconstant.retmsg, exception.geterrormsg (bundle)); } Else if(OinstanceofException) {Exception Exception=(Exception) o; Responsemap.put (Httpconstant.retmsg, Exception.getcause (). GetMessage ()); } Else if(Oinstanceofundeclaredthrowableexception) {o=((undeclaredthrowableexception) O). getundeclaredthrowable (); } Break; }}} PrintWriter pw=Response.getwriter (); Pw.println (Jsonobject.fromobject (Responsemap)); return; }}
This way, as long as you add an attribute in the action Responsemap (requires the code in the result type with the custom
(Map<string, object>) Invocati. Getstack (). Findvalue (Httpconstant.responsemap)
Httpconstant.responsemap corresponds to, Httpconstant.responsemap is the custom constant).
In this action request handler function, the result of the request data to be returned is put in Responsemap, and then returned to success, the foreground can be accessed through Ajax request.
STRUTS2 custom return JSON type result