struts1不像struts2一样,并未对json做集成,因此要使用json就必须自己写代码实现json对象的封装,可以在BaseAction中写一个共通方法,用于封装后台的数据为json对象并发送到前台:
public class BaseAction extends Action{
private String encoding = "UTF-8";
private String contentType = "application/json";
/**
* to make JSON object that will be returned to the front-end and send it
*
* @param response response
* @param objName jsonObjectName
* @param obj object that is used to make jsonObject
* @throws IOException
*/
protected void makeJSONObject(HttpServletResponse response, String objName, Object obj) throws IOException {
this.contentType = contentType + ";charset=" + encoding;
LogUtil.log.info("Set contentType to: " + contentType);
JSONObject jsonObj = new JSONObject();
jsonObj.put(objName, obj);
response.setContentType(contentType);
response.setCharacterEncoding(encoding);
PrintWriter pw = response.getWriter();
pw.write(jsonObj.toString());
pw.flush();
}
}
action中调用该方法就可以发送json数据了,但是要保证传入的obj是能够序列化的,因此,一般来说需要事先做一些处理,最好的方法当然就是大家都熟悉的bean了:
public class SearchEnToCnAction extends BaseAction {
// 使用普遍依赖注入方式
private SearchEnToCnBean searchEnToCnBean;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 取得前台的Ajax数据
String searchStr = request.getParameter("searchStr");
List<Object[]> enWordInfo = searchEnToCnBean.searchEnToCn(searchStr);
if (enWordInfo.isEmpty()) {
Map<String, Object> jsonResponseLs = new HashMap<String, Object>();
jsonResponseLs.put("enWordInfo", null);
super.makeJSONObject(response, "enWordResponse", jsonResponseLs);
} else {
// json process for EnWord
List<EnWordVo> jsonEnWordLs = new ArrayList<EnWordVo>();
EnWordVo jsonEnWord;
for (int i = 0; i < enWordInfo.size(); i ++) {
Object[] enWord = enWordInfo.get(i);
jsonEnWord = new EnWordVo(enWord[0], enWord[1], enWord[2],
enWord[3], enWord[4], enWord[5], enWord[6], enWord[7],
enWord[8], enWord[9], enWord[10]);
jsonEnWordLs.add(jsonEnWord);
}
// json process for EnExtdWord
List<Object[]> enExtdWordInfo = searchEnToCnBean.searchEnToCnExtdInfo(searchStr);
List<EnExtdWordVo> jsonEnExtdWordLs = new ArrayList<EnExtdWordVo>();
if (enExtdWordInfo.isEmpty()) {
jsonEnExtdWordLs = null;
} else {
EnExtdWordVo jsonEnExtdWord;
for (int i = 0; i < enExtdWordInfo.size(); i ++) {
Object[] enExtdWord = enExtdWordInfo.get(i);
jsonEnExtdWord = new EnExtdWordVo(enExtdWord[0], enExtdWord[1], enExtdWord[2],
enExtdWord[3], enExtdWord[4], enExtdWord[5], enExtdWord[6], enExtdWord[7],
enExtdWord[8], enExtdWord[9], enExtdWord[10], enExtdWord[11]);
jsonEnExtdWordLs.add(jsonEnExtdWord);
}
}
Map<String, Object> jsonResponseLs = new HashMap<String, Object>();
jsonResponseLs.put("enWordInfo", jsonEnWordLs);
jsonResponseLs.put("enExtdWordInfo", jsonEnExtdWordLs);
super.makeJSONObject(response, "enWordResponse", jsonResponseLs);
}
return (mapping.findForward(null));
}
public void setSearchEnToCnBean(SearchEnToCnBean searchEnToCnBean) {
this.searchEnToCnBean = searchEnToCnBean;
}
public SearchEnToCnBean getSearchEnToCnBean() {
return searchEnToCnBean;
}
}
上面所使用的两个VO很简单:
public class EnWordVo {
private Object word;
private Object extdAttr;
private Object mean;
private Object dictId;
private Object wordId;
private Object partOfSpeech;
private Object meaningNum;
private Object exampleNum;
private Object exampleExtdAttr;
private Object enExmp;
private Object exmpMeaning;
public EnWordVo(Object word, Object extdAttr, Object mean, Object dictId,
Object wordId, Object partOfSpeech, Object meaningNum,
Object exampleNum, Object exampleExtdAttr, Object enExmp,
Object exmpMeaning) {
super();
this.word = word;
this.extdAttr = extdAttr;
this.mean = mean;
this.dictId = dictId;
this.wordId = wordId;
this.partOfSpeech = partOfSpeech;
this.meaningNum = meaningNum;
this.exampleNum = exampleNum;
this.exampleExtdAttr = exampleExtdAttr;
this.enExmp = enExmp;
this.exmpMeaning = exmpMeaning;
}
public Object getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public Object getExtdAttr() {
return extdAttr;
}
public void setExtdAttr(String extdAttr) {
this.extdAttr = extdAttr;
}
public Object getMean() {
return mean;
}
public void setMean(String mean) {
this.mean = mean;
}
public Object getDictId() {
return dictId;
}
public void setDictId(String dictId) {
this.dictId = dictId;
}
public Object getWordId() {
return wordId;
}
public void setWordId(String wordId) {
this.wordId = wordId;
}
public Object getPartOfSpeech() {
return partOfSpeech;
}
public void setPartOfSpeech(String partOfSpeech) {
this.partOfSpeech = partOfSpeech;
}
public Object getMeaningNum() {
return meaningNum;
}
public void setMeaningNum(String meaningNum) {
this.meaningNum = meaningNum;
}
public Object getExampleNum() {
return exampleNum;
}
public void setExampleNum(String exampleNum) {
this.exampleNum = exampleNum;
}
public Object getExampleExtdAttr() {
return exampleExtdAttr;
}
public void setExampleExtdAttr(String exampleExtdAttr) {
this.exampleExtdAttr = exampleExtdAttr;
}
public Object getEnExmp() {
return enExmp;
}
public void setEnExmp(String enExmp) {
this.enExmp = enExmp;
}
public Object getExmpMeaning() {
return exmpMeaning;
}
public void setExmpMeaning(String exmpMeaning) {
this.exmpMeaning = exmpMeaning;
}
}