TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
js方法:- //点击Ztree 实践 发送请求
- x1 Y8 l, M% N - function querZtree(){. T* R9 C, ^" x
- $.ajax({
2 B/ r7 {; \' K1 @" K( F/ ^ - url:'baseInfoAction_queryZtree.action',1 j7 d/ h) v( N
- async:false,; x! x( i( E: q$ @$ S
- data:{'time':(new Date()).toString()}," `) r- |/ l" k: o
- success:function(result){$ r1 i. z6 j" t5 b1 ]9 w
- var array = eval(result);
( V: P- i2 h2 l* a, g% O" z% ^ - var setting = {
+ {, l: b+ Y9 \8 B - view: {
9 W4 H6 t8 i+ w \! G - showIcon: false,
3 T; f; L5 Q) L( Q: z( w- a - showLine: false
( a. a7 E, g2 ?: m' w5 j - },
[. i( G' s& R( H0 [ v9 l - callback : {$ _( u& f' m, ~1 w8 b- c: P* u
- onClick: zTreeOnClick//回调方法 用户点击 传参数
- @( z1 K2 t# d1 W2 a8 h - }3 g( e9 j8 U& B" ^) V% ]3 {/ [
- };, U0 c2 @7 A6 P! ^: e. k+ G. ]
- $.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内
0 T, P: C+ J; o; Q& l - }
/ N n6 C$ D* Q4 }" G+ U S - });( C7 H6 O; T( v3 s: ]
- }
复制代码 Ztree 回调方法:- //点击知识点 获取对象
. S+ o2 R/ J5 n% C V# P0 Z - function zTreeOnClick(event, treeId, treeNode){4 y) N" ?( n& }
- alert(treeNode.name)
复制代码 % {# Y1 s) K' @8 E, r* v3 K; ^ a
struts.xml 配置文件:
( X% |+ q% K1 U3 C! O- <action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">* \& O$ P$ H- }( Q5 ~
- <result name="knowledge"></result>4 [- L, M! s$ J4 }: f- U
- </action>
复制代码 Action 后台方法:
: m7 V- s* X4 W" x- public void queryZtree(){8 G7 ~% M2 J2 Z9 X: F
- try {; p" n$ g) [8 |* d8 b) G
- JSONArray resultArray = new JSONArray();
- J. k) ~) k3 a5 v# e - for(int i=0;i<4;i++){
1 p1 e: b h4 _% X6 b* q8 P/ }4 S - JSONObject jsonObject = new JSONObject();2 ~: P4 e8 V1 R9 P
- jsonObject.put("name", "一级"+i);
3 X3 H* K! g5 r+ P+ U - jsonObject.put("open", true);
( P6 [# U/ o) U, a2 \& w - JSONArray typeArr = new JSONArray();
B! g6 `7 ]5 {( b+ R; e. d - for(int j=0;j<4;j++){
* T% L. \- {( P8 N( \' C ~ - JSONObject knObj = new JSONObject();
- {/ N5 X7 p% @1 V% n; \* v! } - knObj.put("name", "二级"+j);# [# `$ f4 M0 @" {. s0 z- o$ a& l
- knObj.put("open", true);
* ^ E; T0 F: h7 F$ U - typeArr.put(knObj);
: s! ?/ f2 o- D9 Y0 o; b - jsonObject.put("children", typeArr);' ]* k/ j* D" I( L8 q- U$ C+ ]
- }, O* s9 a+ ^( g$ {# k( r N3 @" b0 h
- resultArray.put(jsonObject);" v T- C& o% N' H3 Y
- }9 Y, b: Q9 T( N
- printMsgToClient(resultArray.toString());1 w9 X% B6 v) n N& @
- } catch (Exception e) {, m0 C3 y8 V& x$ }6 a
- e.printStackTrace();
: j) U+ j# x& y; _. g5 Z - }
% q) j. \: O1 n, s! F - }
复制代码 将结果返回给xmlRequest:
. z8 }$ S9 I$ C, Y& G P3 q) P0 d- /**
7 z& e( i: `5 v1 ?1 k/ @5 Y+ ] - * 将结果返回给xmlRequest6 |1 u) f% l7 l# P9 e
- * . |# l' e9 l) ^2 R
- * @param s
9 _( Q; j5 i7 x0 ]& k. x6 v - * @param dictList
$ `) }3 g, z+ v6 f" \# z: b' z! }0 r! A - * @return
8 W3 V6 K1 g) H2 e! T4 M - * @throws Exception9 w* J& x* x1 x- ^+ P
- */ _7 b+ \! O# j0 y& h
- public void printMsgToClient(String result) throws Exception {# {$ {* U9 C+ B+ J/ }: C2 ^
- getResponse().setCharacterEncoding("UTF-8");
5 \' e3 y4 n* O9 H - PrintWriter out = getResponse().getWriter();
( s' t. }5 r9 S9 a2 r - try {: P3 A: g: u2 m& Q4 C
- out.print(result);
+ l' f0 \2 w- ?( [. E& n+ a - }catch (Exception e) {- ^7 K( t' q. k2 y4 |. M6 K) T
- e.printStackTrace();
0 i, V1 @( ?5 w" U9 U - } finally {0 q* J, v6 V; Q0 l
- out.close();
& j5 c$ x( b- U6 `- A. ], f - }
. S/ l+ f- v. L+ s% j - }
复制代码 % M7 N+ P6 u% e
# a- R# y; I: t
, S, ?0 e! ]: K, p* H3 U' X& d2 v7 I5 v: Y o+ Y
4 y( z, C3 m8 x! ? |
|