Previously in the use of STRUTS2 development projects, the processing of JSON has been in action, in action directly response, recently studied Struts2 source, found a more elegant solution, define a Resulttype ,
First, we first look at the source code in the Struts2
Defaultactioninvocation under the package COM.OPENSYMPHONY.XWORK2
472 rows
[Java]View Plaincopyprint?
- /**
- * Save The result to be used later.
- * @param actionconfig Current Actionconfig
- * @param methodresult The result of the action.
- * @return The result code to process.
- */
- Protected String Saveresult (actionconfig actionconfig, Object methodresult) {
- if (methodresult instanceof Result) {
- This.explicitresult = (Result) Methodresult;
- //Wire The result automatically
- Container.inject (Explicitresult);
- return null;
- } Else {
- return (String) Methodresult;
- }
- }
If the Resulttype implements the result interface, it executes the
[Java]View Plaincopyprint?
- This.explicitresult = (Result) Methodresult;
- //Wire The result automatically
- Container.inject (Explicitresult);
- return null;
Now let's define an interface (Jsonresult) to handle the generic Pojo object
[Java]View Plaincopyprint?
- Package com.kiloway.struts;
- Import Java.io.PrintWriter;
- Import Javax.servlet.http.HttpServletResponse;
- Import Net.sf.json.JSONObject;
- Import Net.sf.json.JsonConfig;
- Import Org.apache.struts2.ServletActionContext;
- Import Org.apache.struts2.dispatcher.StrutsResultSupport;
- Import com.opensymphony.xwork2.ActionInvocation;
- Public class Jsonresult extends Strutsresultsupport {
- private Object result;
- private Jsonconfig Jsonconfig;
- Public Object GetResult () {
- return result;
- }
- Public Jsonresult (Jsonconfig jsonconfig) {
- super ();
- this.jsonconfig = jsonconfig;
- }
- public void Setresult (Object result) {
- This.result = result;
- }
- private Static final long serialversionuid = 7978145882434289002L;
- @Override
- protected void Doexecute (String finallocation, actioninvocation invocation)
- throws Exception {
- HttpServletResponse response = null;
- try {
- Response = Servletactioncontext.getresponse ();
- PrintWriter printwriter = Response.getwriter ();
- if (jsonconfig! = null) {
- Printwriter.write (Jsonobject.fromobject (Result). ToString ());
- } Else {
- Printwriter.write (Jsonobject.fromobject (result, Jsonconfig)
- . toString ());
- }
- }catch (Exception e) {
- throw New Exception ("JSON parse error!");
- } finally {
- Response.getwriter (). Close ();
- }
- }
- }
Jsonreulst defines how to get struts to handle it?
We can define this in Struts.xml.
[HTML]View Plaincopyprint?
- <package name="Default" namespace="/" extends="Struts-default">
- <result-types>
- <result-type name="Jsonresult" class="Com.kiloway.struts.JsonResult"/>
- </result-types>
- <action name="student" class="com.kiloway.struts.Student">
- <result name="JSON" type="Jsonresult"/>
- </Action>
- </Package>
The name of the REUSLT can be arbitrary, but the type must be the same as the resulttype you register.
This is called directly in the Action
[Java]View Plaincopyprint?
- Public Jsonresult Getjson ()
- {
- UserInfo f = new UserInfo ();
- F.setname ("small core");
- F.setpassword ("haha");
- Jsonresult Jsonresult = new Jsonresult ();
- Jsonresult.setresult (f);
- return jsonresult;
- }
In our action code we do not have to Response.Write, completely to the Reuslt object to deal with (Doexecute)
This makes it easy to work with JSON-formatted data
In the latest Struts development package I downloaded, a JSON processing plugin was found Struts2-json-plugin-2.3.8.jar
The plugin provides a more complete JSON processing solution, and the next article describes how the plugin is used
Source: http://blog.csdn.net/myxx520/article/details/8655088
Extended struts2 result set Strutsresultsupport custom result processing JSON