日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Struts2自定义Result处理JSON

發布時間:2025/3/21 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2自定义Result处理JSON 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前在采用Struts2開發的項目中,對JSON的處理一直都在Action里處理的,在Action中直接Response,最近研讀了一下Struts2的源碼,發現了一個更加優雅的解決辦法,自己定義一個ResultType,

首先大家先看下Struts2中的源碼

包com.opensymphony.xwork2下的DefaultActionInvocation

472行

/** * 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; } }

如果resultType實現了Result接口,則執行

this.explicitResult = (Result) methodResult; // Wire the result automatically container.inject(explicitResult); return null;

現在我們來定義一個接口(JsonResult)來處理一般的POJO對象

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定義好了該如何讓Struts處理呢?

我們在struts.xml里面可以這樣定義

<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>

reuslt的name可以任意,但type必須和你注冊的ResultType相同。

Action 中直接這樣調用

public JsonResult getJson() { UserInfo f = new UserInfo(); f.setName("小睿睿"); f.setPassword("哈哈"); JsonResult jsonResult = new JsonResult(); jsonResult.setResult(f); return jsonResult; }

在我們的Action代碼中就不用response.write了,完全交給了Reuslt對象去處理了(doExecute)

這樣就很方便的處理了JSON格式的數據

struts的開發包里,發現了一個JSON處理插件 struts2-json-plugin-2.3.8.jar

該插件提供了更完善的JSON處理解決方案。

原文http://blog.csdn.net/myxx520/article/details/8655088

?

轉載于:https://www.cnblogs.com/azhqiang/p/4676048.html

總結

以上是生活随笔為你收集整理的Struts2自定义Result处理JSON的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。