Java的Struts2框架配合Ext JS處理JSON數(shù)據(jù)的使用示例
最近嘗試用extjs來(lái)展示樹(shù)狀菜單。著實(shí)花了一番功夫。樹(shù)狀菜單的菜單項(xiàng)需要?jiǎng)討B(tài)加載,而目前版本的extjs中只支持JSON格式的數(shù)據(jù)。查了一些資 料,決定使用struts2的json-plugin。首先按照例子做了一個(gè),但是結(jié)果就是不成功,界面上只出來(lái)了一個(gè)js中生成的root節(jié)點(diǎn),不能加 載從后臺(tái)生成的數(shù)據(jù)。研究后發(fā)現(xiàn)是數(shù)據(jù)格式有問(wèn)題。使用json-plugin生成的數(shù)據(jù)格式如下:
{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}
而extjs需要的數(shù)據(jù)格式如下:
[{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}]
區(qū)別很小,就只相差最外面的兩個(gè)方括號(hào)。但是少了這兩個(gè)方括號(hào),在json中,含義迥然不同,前者表示一個(gè)對(duì)象,而后者表示一個(gè)數(shù)組。而extjs中 tree的dataloader需要的數(shù)據(jù)必須是一個(gè)數(shù)組。而這樣的數(shù)據(jù)格式是json-plugin自動(dòng)生成的,無(wú)法改變。所以,我最后放棄了json -plugin,轉(zhuǎn)而使用json-lib來(lái)解決這個(gè)問(wèn)題。
1. 下載json-lib, http://json-lib.sourceforge.net/
2. lib目錄下的jar文件清單:
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.6.jar
commons-lang-2.3.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ezmorph-1.0.4.jar
freemarker-2.3.8.jar
javassist-3.8.1.jar
json-lib-2.2.1-jdk15.jar
log4j-1.2.13.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xml-apis-1.0.b2.jar
xwork-2.0.4.jar
首先配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
然后是struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true"/> <constant name="struts.i18n.encoding" value="UTF-8"/> <package name="person" extends="struts-default"> <action name="menus" method="execute" class="com.lab.MenuAction"> <result>/menu.jsp</result> </action> </package> </struts>
3. 樹(shù)的節(jié)點(diǎn)模型(省略了getter,setter)
public class Menu {
private int id;
private String text;
private boolean leaf;
private String cls;
private List<Menu> children;
}
4. action
package com.lab;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
public class MenuAction {
private String menuString;
private List<Menu> menus;
public String execute() {
menus = new ArrayList<Menu>();
Menu benz = new Menu();
benz.setText("Benz");
benz.setCls("folder");
benz.setLeaf(false);
benz.setId(10);
menus.add(benz);
List<Menu> benzList = new ArrayList<Menu>();
benz.setChildren(benzList);
Menu menu;
menu = new Menu();
menu.setText("S600");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(11);
benzList.add(menu);
menu = new Menu();
menu.setText("SLK200");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(12);
benzList.add(menu);
Menu bmw = new Menu();
bmw.setText("BMW");
bmw.setCls("folder");
bmw.setLeaf(false);
bmw.setId(20);
menus.add(bmw);
List<Menu> bmwList = new ArrayList<Menu>();
bmw.setChildren(bmwList);
menu = new Menu();
menu.setText("325i");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(21);
bmwList.add(menu);
menu = new Menu();
menu.setText("X5");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(22);
bmwList.add(menu);
JSONArray jsonObject = JSONArray.fromObject(menus);
try {
menuString = jsonObject.toString();
} catch (Exception e) {
menuString = "ss";
}
return "success";
}
public String getMenuString() {
return menuString;
}
public void setMenuString(String menuString) {
this.menuString = menuString;
}
}
5. menu.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <s:property value="menuString" escape="false"/>
6. html頁(yè)面和js
我使用的就是extjs的example中的reorder.html和reorder.js,更改了reorder.js中treeloader的dataurl: menus.action
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Reorder TreePanel</title> <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" /> <!-- GC --> <!-- LIBS --> <script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script> <!-- ENDLIBS --> <script type="text/javascript" src="extjs/ext-all.js"></script> <script type="text/javascript" src="reorder.js"></script> <!-- Common Styles for the examples --> <link rel="stylesheet" type="text/css" href="extjs/resources/css/example.css" /> </head> <body> <script type="text/javascript" src="../examples.js"></script><!-- EXAMPLES --> <h1>Drag and Drop ordering in a TreePanel</h1> <p>This example shows basic drag and drop node moving in a tree. In this implementation there are no restrictions and anything can be dropped anywhere except appending to nodes marked "leaf" (the files). <br></p> <p>Drag along the edge of the tree to trigger auto scrolling while performing a drag and drop.</p> <p>In order to demonstrate drag and drop insertion points, sorting was <b>not</b> enabled.</p> <p>The data for this tree is asynchronously loaded with a JSON TreeLoader.</p> <p>The js is not minified so it is readable. See <a href="reorder.js">reorder.js</a>.</p> <div id="tree-div" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div> </body> </html>
js:
/*
* Ext JS Library 2.0.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.onReady(function(){
// shorthand
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
el:'tree-div',
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true,
loader: new Tree.TreeLoader({
dataUrl:'http://localhost:8080/lab/menus.action'
})
});
// set the root node
var root = new Tree.AsyncTreeNode({
text: 'Ext JS',
draggable:false,
id:'source'
});
tree.setRootNode(root);
// render the tree
tree.render();
root.expand();
});
7.解析為L(zhǎng)ist數(shù)據(jù)
代碼如下:
EXTJS中的json數(shù)據(jù)
var comboStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url:'adminGroup', //這里是struts請(qǐng)求到action
method:'POST' //請(qǐng)求方式
}),
reader: new Ext.data.JsonReader({
//總記錄數(shù)
totalProperty: 'results', //總記錄數(shù)
root: 'items', //記錄集合
id:'roleId'
},
['roleId','roleName'] //顯示的兩個(gè)字段
)
});
JSON數(shù)據(jù)內(nèi)容
{"items":[{"password":"ahui","adminId":1,"role":{"roleName":"系統(tǒng)管理員","roleId":2,"sequence":"2","admin":null,"logoutMark":"否"},"adminName":"ahui","logout":"否"},
{"password":"xiao","adminId":2,"role":{"roleName":"系統(tǒng)管理員","roleId":2,"sequence":"2","admin":null,"logoutMark":"否"},"adminName":"xiao","logout":"是"},"results":13}
下面是struts2里面的action代碼 里面封裝了ExtHelper工具類,里面有轉(zhuǎn)換xml和json兩種格式
public String findAll() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
List list = groupService.getGroup(); //調(diào)用service里面的方法,把所有的數(shù)據(jù)都查詢出來(lái)
String json = ExtHelper.getJsonFromList(list);//把list轉(zhuǎn)換為json格式的數(shù)據(jù)
response.setContentType("text/json;charset=UTF-8");//設(shè)置數(shù)據(jù)到前臺(tái)顯示的字符編碼,如果不轉(zhuǎn)會(huì)有亂碼
response.getWriter().write(json);
System.out.println(json);
return null;
}
解析json的方法有很多,所以是怎么方便怎么來(lái),json自己的包里也可以進(jìn)行轉(zhuǎn)換,但如果項(xiàng)目中用的是Struts2,直接用Struts2提供的方法更方便。
- struts2框架的登錄制作圖文教程
- struts2框架入門
- java struts2框架簡(jiǎn)介
- 概述Java的struts2框架
- java Struts2框架下實(shí)現(xiàn)文件上傳功能
- Java的Struts2框架中攔截器使用的實(shí)例教程
- 使用MyEclipse 開(kāi)發(fā)struts2框架實(shí)現(xiàn)登錄功能(結(jié)構(gòu)教程)
- 詳解在Java的Struts2框架中配置Action的方法
- 詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式
- 搭建EXTJS和STRUTS2框架(ext和struts2簡(jiǎn)單實(shí)例)
- Struts2框架初學(xué)接觸
相關(guān)文章
GC調(diào)優(yōu)實(shí)戰(zhàn)之過(guò)早提升Premature?Promotion
這篇文章主要為大家介紹了GC調(diào)優(yōu)實(shí)戰(zhàn)之過(guò)早提升Premature?Promotion2022-01-01
詳解Java回調(diào)的原理與實(shí)現(xiàn)
回調(diào)函數(shù),顧名思義,用于回調(diào)的函數(shù)?;卣{(diào)函數(shù)只是一個(gè)功能片段,由用戶按照回調(diào)函數(shù)調(diào)用約定來(lái)實(shí)現(xiàn)的一個(gè)函數(shù)?;卣{(diào)函數(shù)是一個(gè)工作流的一部分,由工作流來(lái)決定函數(shù)的調(diào)用(回調(diào))時(shí)機(jī)。2017-03-03
SpringBoot自定義FailureAnalyzer詳解
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer詳解,FailureAnalyzer是一種在啟動(dòng)時(shí)攔截?exception?并將其轉(zhuǎn)換為?human-readable?消息的好方法,包含在故障分析中,需要的朋友可以參考下2023-11-11
javaCV開(kāi)發(fā)詳解之收流器實(shí)現(xiàn)
這篇文章主要介紹了javaCV開(kāi)發(fā)詳解之收流器實(shí)現(xiàn),對(duì)javaCV有研究的同學(xué),可以參考下2021-04-04
Java實(shí)現(xiàn)視頻格式轉(zhuǎn)化的操作代碼
在當(dāng)今數(shù)字化時(shí)代,視頻已成為我們?nèi)粘I詈凸ぷ髦胁豢苫蛉钡囊徊糠?不同的設(shè)備和平臺(tái)可能支持不同的視頻格式,因此,視頻格式轉(zhuǎn)換的需求也日益增長(zhǎng),本文將介紹如何使用Java實(shí)現(xiàn)視頻格式轉(zhuǎn)換,需要的朋友可以參考下2025-01-01
Spring Boot整合Web項(xiàng)目常用功能詳解
這篇文章主要介紹了Spring Boot整合Web項(xiàng)目常用功能詳解,在Web應(yīng)用開(kāi)發(fā)過(guò)程中,可以通過(guò)Spring Boot的Starter來(lái)將這些常用功能進(jìn)行整合與集中維護(hù),以達(dá)到開(kāi)箱即用的目的。,需要的朋友可以參考下2019-06-06
Spring ApplicationListener源碼解析
這篇文章主要為大家介紹了Spring ApplicationListener源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

