TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
js方法:- //点击Ztree 实践 发送请求
1 Y) X+ L; ^( @$ e/ j - function querZtree(){" G9 r; i4 B, w* v
- $.ajax({
1 B( K6 B8 o3 ]- g - url:'baseInfoAction_queryZtree.action', `: R( r4 u8 ?3 R( e4 w4 L
- async:false,
; H+ A( v1 e' | - data:{'time':(new Date()).toString()},& j: s/ m Y' t0 t' I
- success:function(result){4 d4 i9 q* x. u( P: @0 l( j- l2 |
- var array = eval(result);
" \, }" C: }. l* b, a8 d - var setting = {
) y! H- {2 l, f6 P+ O l% ?' F - view: {
& F; d3 h H+ v) y+ k' z - showIcon: false, % c& b3 U+ c9 Y( s- u: d& C! c" U
- showLine: false 6 N k$ c% z0 a) ^/ H" I
- },
* [7 d1 P5 E) a6 P' P6 `: C - callback : {+ J! L- d# t. }' \ W' f
- onClick: zTreeOnClick//回调方法 用户点击 传参数" m" q9 N5 D; f" t7 a
- }
( e0 \# H2 B7 g6 A- L, j$ R - };
3 C8 u6 C2 l# N, [$ ~* Q: d" E - $.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内
9 c! W% B- C( r5 T: o/ J - }
3 `% ]- u/ R$ U! @2 R1 \* @1 r - });# f3 P1 [3 c% @
- }
复制代码 Ztree 回调方法:- //点击知识点 获取对象
3 I$ x) F# x& ` - function zTreeOnClick(event, treeId, treeNode){8 |# S% C- K- o" B9 _) ~
- alert(treeNode.name)
复制代码
2 F: M# m! n% n7 h( jstruts.xml 配置文件:0 L/ A, \6 a: l& W. [
- <action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">
/ B4 Y8 F e% D- `$ g - <result name="knowledge"></result>2 E y& G/ Y6 T( ?" |8 N" K/ F. ^
- </action>
复制代码 Action 后台方法:) I8 K9 J$ w f% |# J4 F
- public void queryZtree(){
3 g4 d+ {! v" Q5 d0 ~" L! ] - try {, o X J1 m* ?
- JSONArray resultArray = new JSONArray();! V6 `2 I' F9 E9 ^; F
- for(int i=0;i<4;i++){
9 E9 l+ n$ x1 b) @4 ~ - JSONObject jsonObject = new JSONObject();
* o6 E# W! O8 S2 D - jsonObject.put("name", "一级"+i);) E, }* k; n( ~" i' a2 f
- jsonObject.put("open", true);
$ {" |: L! `/ z9 V" p& ^ - JSONArray typeArr = new JSONArray();: `7 V" o. _) ?0 r. n6 G
- for(int j=0;j<4;j++){" I( o& i1 \4 |
- JSONObject knObj = new JSONObject();8 }% P% a- o0 M* ^$ a; F L# g
- knObj.put("name", "二级"+j);
$ ?$ J6 S8 s0 d G% _+ F6 b/ y - knObj.put("open", true);& `$ y) W8 ?- z# Z, _4 q$ k; D0 }5 R
- typeArr.put(knObj); D4 r0 i6 m5 ^8 i" i
- jsonObject.put("children", typeArr);, f1 O0 q4 W: y
- }( T! e7 a @/ w, C: m6 d+ X
- resultArray.put(jsonObject);5 g1 J4 `+ I+ X5 [* ^ m2 o [
- }* A- Q' \0 q9 b) |! H
- printMsgToClient(resultArray.toString());4 T: P( F3 v7 u' J, j# g
- } catch (Exception e) {# j: K- W6 k5 r, q9 J
- e.printStackTrace(); {; a# L8 }# I }
- }. _: y- {/ ]1 G" M# U4 k
- }
复制代码 将结果返回给xmlRequest:
- X1 o" M3 X& ~# j+ @( ?& h- /**
( I. i2 m8 N4 E' J3 [ - * 将结果返回给xmlRequest
; G+ ?( S! W! U3 F% n# L( d - * 2 U5 z* N- m) C& u9 E
- * @param s
+ {, C2 z3 J3 V7 h4 ^7 u4 R! ` - * @param dictList* b7 G5 q, d4 g4 J0 w
- * @return# }& o+ }# x: \' T5 ?
- * @throws Exception
{8 m- W+ v( G9 x/ b - */
, @. ?& q. m. q9 j9 R6 n% N' S - public void printMsgToClient(String result) throws Exception {/ r8 V0 Q H, |; |, T+ z# \
- getResponse().setCharacterEncoding("UTF-8");
$ Z6 f! y6 h# u, o2 R V - PrintWriter out = getResponse().getWriter();! ]* J% ~! o. D( U- R
- try {
, D0 E H) M5 A. M - out.print(result);: ?1 o: [1 _6 ~, ]7 e
- }catch (Exception e) {
9 m2 [/ n+ M u4 L8 M5 A, I5 ? - e.printStackTrace();
7 Q/ I* C) Q" R9 J2 x4 S" I - } finally {4 H7 H2 b( Z o
- out.close();
8 w. f5 `2 J& i1 s% u$ x# t. g+ n - }( H+ O$ i X) p. u! H
- }
复制代码 & m9 u7 m4 O0 N
9 r; f5 Z. ~ \- _1 d- j" _! s
/ Z% ?, v1 A6 `. P) _" Q1 p- {. l1 P0 `7 _) g7 P( X5 ?* M
5 w% U& y; R: Y |
|