Extended struts2 result set Strutsresultsupport custom result processing JSON

Source: Internet
Author: User
Tags parse error

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?
  1. /**
  2. * Save The result to be used later.
  3. * @param actionconfig Current Actionconfig
  4. * @param methodresult The result of the action.
  5. * @return The result code to process.
  6. */
  7. Protected String Saveresult (actionconfig actionconfig, Object methodresult) {
  8. if (methodresult instanceof Result) {
  9. This.explicitresult = (Result) Methodresult;
  10. //Wire The result automatically
  11. Container.inject (Explicitresult);
  12. return null;
  13. } Else {
  14. return (String) Methodresult;
  15. }
  16. }


If the Resulttype implements the result interface, it executes the

[Java]View Plaincopyprint?
    1. This.explicitresult = (Result) Methodresult;
    2. //Wire The result automatically
    3. Container.inject (Explicitresult);
    4. return null;

Now let's define an interface (Jsonresult) to handle the generic Pojo object

[Java]View Plaincopyprint?
  1. Package com.kiloway.struts;
  2. Import Java.io.PrintWriter;
  3. Import Javax.servlet.http.HttpServletResponse;
  4. Import Net.sf.json.JSONObject;
  5. Import Net.sf.json.JsonConfig;
  6. Import Org.apache.struts2.ServletActionContext;
  7. Import Org.apache.struts2.dispatcher.StrutsResultSupport;
  8. Import com.opensymphony.xwork2.ActionInvocation;
  9. Public class Jsonresult extends Strutsresultsupport {
  10. private Object result;
  11. private Jsonconfig Jsonconfig;
  12. Public Object GetResult () {
  13. return result;
  14. }
  15. Public Jsonresult (Jsonconfig jsonconfig) {
  16. super ();
  17. this.jsonconfig = jsonconfig;
  18. }
  19. public void Setresult (Object result) {
  20. This.result = result;
  21. }
  22. private Static final long serialversionuid = 7978145882434289002L;
  23. @Override
  24. protected void Doexecute (String finallocation, actioninvocation invocation)
  25. throws Exception {
  26. HttpServletResponse response = null;
  27. try {
  28. Response = Servletactioncontext.getresponse ();
  29. PrintWriter printwriter = Response.getwriter ();
  30. if (jsonconfig! = null) {
  31. Printwriter.write (Jsonobject.fromobject (Result). ToString ());
  32. } Else {
  33. Printwriter.write (Jsonobject.fromobject (result, Jsonconfig)
  34. . toString ());
  35. }
  36. }catch (Exception e) {
  37. throw New Exception ("JSON parse error!");
  38. } finally {
  39. Response.getwriter (). Close ();
  40. }
  41. }
  42. }


Jsonreulst defines how to get struts to handle it?

We can define this in Struts.xml.

[HTML]View Plaincopyprint?
  1. <package name="Default" namespace="/" extends="Struts-default">
  2. <result-types>
  3. <result-type name="Jsonresult" class="Com.kiloway.struts.JsonResult"/>
  4. </result-types>
  5. <action name="student" class="com.kiloway.struts.Student">
  6. <result name="JSON" type="Jsonresult"/>
  7. </Action>
  8. </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?
  1. Public Jsonresult Getjson ()
  2. {
  3. UserInfo f = new UserInfo ();
  4. F.setname ("small core");
  5. F.setpassword ("haha");
  6. Jsonresult Jsonresult = new Jsonresult ();
  7. Jsonresult.setresult (f);
  8. return jsonresult;
  9. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.