TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
js方法:- //点击Ztree 实践 发送请求
9 E# B6 q2 A M+ e - function querZtree(){
1 H/ Y& d; [+ z7 C M! w9 k - $.ajax({& A6 @! U3 \6 v% K, N+ }
- url:'baseInfoAction_queryZtree.action',
- Z) w2 \- X$ R' S; p - async:false,6 T2 E8 ~' R( Y: q u+ }) }
- data:{'time':(new Date()).toString()},! M9 P `" w N+ k0 m4 D
- success:function(result){
7 E; N. x. G2 { - var array = eval(result);- N, w# y0 y+ U: V+ V! z; h' i y
- var setting = {
. z6 z1 @! @5 M7 }: `; O6 R5 f! p! g - view: { , f- h0 t' ^) p
- showIcon: false, ( a6 y) G- M+ L+ ~7 `0 R
- showLine: false + m/ G) H6 k/ H& a/ A0 D+ i% N
- },
! W- p5 ]. n8 H5 m& h# M6 B5 G! a7 _ - callback : {
3 \8 {8 K1 s9 X- U - onClick: zTreeOnClick//回调方法 用户点击 传参数& x9 c9 E0 k' g. I, A- \
- }
+ [. |) W: p! S+ G - };
3 r2 H1 j: g. z - $.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内
; q4 f" Z3 I( i: l( K3 T# R5 b. v - }
1 ~! M/ {$ O7 z9 `1 v: o: w2 | - });9 J: u- \! z) z, _; Q& g2 d; ?
- }
复制代码 Ztree 回调方法:- //点击知识点 获取对象 a" t" M9 }- {$ i6 Y5 R
- function zTreeOnClick(event, treeId, treeNode){
, s$ J4 j- x I1 c* ~; F# D# `6 M - alert(treeNode.name)
复制代码 % a7 i0 E- s! N& ]9 L" Q7 f* F
struts.xml 配置文件:
0 g$ O+ n$ J% z/ h+ B0 F( s4 `- <action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">
: i* \1 }4 I- U, s$ y' M - <result name="knowledge"></result>
, ]2 M& p" K5 p Z9 T+ M1 ? - </action>
复制代码 Action 后台方法:
2 O0 q* \1 J; e$ H6 H- public void queryZtree(){+ } e! s) T8 f8 m+ Y
- try {8 i9 f1 P; w7 q
- JSONArray resultArray = new JSONArray();
0 U; G! F3 w4 |" P. F$ R3 | - for(int i=0;i<4;i++){
# E. g9 X( {& W7 Z( E5 u - JSONObject jsonObject = new JSONObject();
* O8 _( {9 ?: x8 g/ F7 b/ A1 y - jsonObject.put("name", "一级"+i);
' N& ]9 F6 ]) z - jsonObject.put("open", true);6 E; y0 o- [/ T7 `2 L
- JSONArray typeArr = new JSONArray();
6 U: Q! `4 k8 W+ m3 \ - for(int j=0;j<4;j++){
7 t0 t( I: d. [: [' Z: [; L- K - JSONObject knObj = new JSONObject();
% J4 R+ c S8 g1 k4 D; U - knObj.put("name", "二级"+j);
9 W- O' C, z) L+ ?9 t - knObj.put("open", true);
! L1 R" r# d# |" k - typeArr.put(knObj);
0 _, D0 a% K' x* B, K - jsonObject.put("children", typeArr);
& W2 F( A, |$ z& I3 O - }+ L5 _$ n7 {/ n2 K' B0 g
- resultArray.put(jsonObject);
9 z0 d; J" M( U& V2 L - }
" H+ p# {( X1 p! w+ V - printMsgToClient(resultArray.toString());
2 @' X: t& `+ ~ [. A! D* f0 z - } catch (Exception e) {1 F8 W5 v" u! M) @7 r- d/ w
- e.printStackTrace();
& M2 r; f Q/ C$ N* V8 O9 Y - }6 q9 z# q& a: R& @7 R1 x
- }
复制代码 将结果返回给xmlRequest:
/ s8 O- s% o9 y- /**7 U- w9 u. o* b2 T0 P+ R: T
- * 将结果返回给xmlRequest4 c- ^% v6 I; A ~, l. H- x
- * 8 ^, j t; B9 o2 c9 D( B& _5 d4 K5 l
- * @param s
, r% P% X8 m C3 X - * @param dictList. D- }2 o6 H2 \. A w! W6 S
- * @return
9 J1 ~* ^1 t: K8 H& @ - * @throws Exception
/ w6 t1 F9 h) x3 ]# E: f - */7 Z, a$ [% r! w' s" \2 R1 _# P& K
- public void printMsgToClient(String result) throws Exception {
6 |2 q$ B. e9 t: R - getResponse().setCharacterEncoding("UTF-8");" m" R! m# w i8 {, g
- PrintWriter out = getResponse().getWriter();
4 k! o3 b8 ^3 Z0 H: R( {- l2 `* d - try {
+ Y! X* z. c! U" o - out.print(result);$ T: K- i3 o% @. u& J& M0 X4 [! X
- }catch (Exception e) {
$ Q# m ^( |# Q0 p1 o ^; _ - e.printStackTrace();
8 R. R7 M9 h1 w }, U+ p8 r, O2 j - } finally {' O6 V) j# ?/ m. E$ x
- out.close();0 e7 a6 ~. ^! w% M& _. o
- }
/ T& W& e F) W L - }
复制代码
: g( i4 n( ]' a4 S- P. f3 o. S5 D
9 F6 j( }4 \# Y! g) i! \) N
( t/ G, `) d! _& O7 Z
: T( O6 H0 _% [/ V" M' Q2 Y3 F |
|