TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
js方法:- //点击Ztree 实践 发送请求
; Z& ^) W! o4 V$ B* ^4 x7 f - function querZtree(){
6 @$ ~0 `" b. i7 o, L - $.ajax({
' L# E: R. J/ v) E6 ? - url:'baseInfoAction_queryZtree.action',( g) Q( ^" h' h0 L
- async:false,
" b R9 i+ J8 c q" P8 P* D) ? - data:{'time':(new Date()).toString()},% E C7 P( u: ~0 N: Z+ g/ J; g
- success:function(result){
" I0 C X& o, E/ P1 K; t- a - var array = eval(result);
9 W/ l; D5 W$ w( E! ?' A - var setting = { 7 `" U" }2 \7 k* A+ r" s
- view: { 2 j6 Y8 H }% l4 m2 U8 }( L( i
- showIcon: false, ; n/ |! s# A+ W" W
- showLine: false & h: h$ |$ H% L; r: A( ~
- }, * T, C: R, n$ _
- callback : {
: ~! S, j: c3 j( |) o - onClick: zTreeOnClick//回调方法 用户点击 传参数
& a# @- n% [& Z2 R6 B* R( w7 ?+ C+ o, X - }
9 D% `7 [! h; [8 f9 n) ]2 J - };+ i! o* r, T" @! k2 T" m N
- $.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内) V2 U4 U& f+ o7 F% Q( C1 o/ e
- }3 ~' P1 ^; b( S' M$ K1 @7 e
- });
4 F; h1 c9 ^$ { - }
复制代码 Ztree 回调方法:- //点击知识点 获取对象
/ ?% F' y- Y1 W ^/ \ - function zTreeOnClick(event, treeId, treeNode){
& d+ |* r9 E# G( `3 J2 B" Y - alert(treeNode.name)
复制代码
( R8 f$ y' J2 x5 n2 V/ lstruts.xml 配置文件:- p# v5 V' s7 @9 m, d. @% F! Z) Q
- <action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">& h2 H V1 ]9 e. @8 k0 A
- <result name="knowledge"></result>
; C0 w0 T6 t6 { - </action>
复制代码 Action 后台方法:) A' P _' W; j/ g
- public void queryZtree(){6 p' ]! b+ M' s7 G5 k% o" |
- try {* G6 y! R# {# i6 u: Z/ f
- JSONArray resultArray = new JSONArray();" V2 G$ O! e& F3 C1 f
- for(int i=0;i<4;i++){
2 u3 Q/ k+ y; m% ~$ E Z - JSONObject jsonObject = new JSONObject();
. L1 r( s% w3 n. g4 q- ~: I! b* c; k - jsonObject.put("name", "一级"+i);( U/ i+ G% H" y1 K7 @
- jsonObject.put("open", true);
3 Q) s; V( y) I6 R/ B - JSONArray typeArr = new JSONArray();
" @5 I$ |( y' X) G% e3 u+ h7 ?. x - for(int j=0;j<4;j++){9 ?- _, |/ o( v' R, [" r" I
- JSONObject knObj = new JSONObject();" C6 G. x6 k; i( _7 T& ~
- knObj.put("name", "二级"+j);) T5 H3 F, G9 J A. w9 K- X; q
- knObj.put("open", true);4 l6 B4 T3 g! z M
- typeArr.put(knObj);
8 g3 E% i+ W4 J- d- S - jsonObject.put("children", typeArr);
. C: J% B; ~7 C( D - }
) d# P+ O, q( U - resultArray.put(jsonObject);1 i: G% M3 l5 d4 m% N7 m
- }# O, D% ^$ S" S9 C* m8 W
- printMsgToClient(resultArray.toString());. O4 Z. R. M1 ^( p: M( x( H- W
- } catch (Exception e) {# ^/ J0 y% P- K+ @1 m
- e.printStackTrace();1 Q8 y4 R9 Y: Y4 B' ^5 ~, y
- }& e( I. m3 M8 H6 i; b( u6 E1 L, f) F4 j1 }
- }
复制代码 将结果返回给xmlRequest:
4 N$ s3 o9 E! y/ V8 F& R& x- /**
: `' J: K" i) d6 q, @( W - * 将结果返回给xmlRequest) Z: B5 X0 O( s( V: |
- * " C+ F* k4 \) \
- * @param s! X7 d6 g+ G0 T( y; L# K) M6 V/ _9 w
- * @param dictList$ G# l1 N9 S: [
- * @return
! B8 n+ X' w7 a5 H$ R- g - * @throws Exception" d1 n5 _' Z, S. Q& W0 d
- */# ?" F3 r7 T9 v( E
- public void printMsgToClient(String result) throws Exception {
6 l, S/ i! J4 u P; M$ D, a - getResponse().setCharacterEncoding("UTF-8");5 k2 Z) s! C7 o) q: a
- PrintWriter out = getResponse().getWriter();; N! P: x7 z- L. t6 Y# @0 o
- try {
1 u! O( _3 p7 ]5 P9 {& |" r1 X - out.print(result);$ H& G0 k( R$ r; k
- }catch (Exception e) {+ d2 K" Y% m1 \) z1 ~6 w
- e.printStackTrace();4 u& ~& n/ b) u4 j. V; X! n7 s
- } finally {* G1 Z. ?% X Z3 J% ~
- out.close();" ?- F5 E# d# M( H2 \3 r
- }
8 B3 W; d E5 P- s - }
复制代码
( w) n* @5 d1 D# D5 w! E' p g1 ]; E; E% E( E
- A- [2 V* y: c% {- f3 v
6 d' ~1 |4 H1 M* H8 s4 L" u- C; n
, A1 P) s6 S% P% h: L# W
|
|