问题描述
前几天做了一个通讯录,用strutsjspmysql实现了今天想改善一下改用jquery的ajax但是怎么也想不明白和action的关系还有怎么查询数据库求拯救!!首先我后台查询数据库是写在welcomeAction.java里的是这样的packagecom.contacts;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.Statement;importcom.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")publicclasswelcomeActionextendsActionSupport{privateStringname;privateStringtel;privateStringqq;privateStringmail;publicStringexecute(){inti=0;try{Class.forName("com.mysql.jdbc.Driver").newInstance();Connectioncon=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/contacts","root","root");Statementstmt=con.createStatement();ResultSetrst=stmt.executeQuery("SELECT*FROMcontacts");rst.close();stmt.close();con.close();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}if(i==0)return"success2";elsereturn"null";}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetTel(){returntel;}publicvoidsetTel(Stringtel){this.tel=tel;}publicStringgetQq(){returnqq;}publicvoidsetQq(Stringqq){this.qq=qq;}publicStringgetMail(){returnmail;}publicvoidsetMail(Stringmail){this.mail=mail;}}然后我想利用ajax的post方法从这里获取数据库的数据然后显示:前台:<scripttype="text/javascript"src="jquery.js"></script><scripttype="text/javascript">$(function(){$("#welcome").click(function(){$.get("welcomeAction.java",{},function(data){varname=data.name;vartel=data.tel;varqq=data.qq;varmail=data.mail;varstr="<tr><td>"+name+"</td><td>"+tel+"</td><td>"+qq+"</td><td>"+mail+"</td></tr>";$("#table").html(str);},"json");});});</script>我有几点不能理解1.我知道通过data传递参数,但这个data在后台应该怎么写?2.怎么遍历读出数据库的每一条数据?3.struts中的action应该咋用?<actionname="welcome"class="com.contacts.welcomeAction"><resultname="success"type="json">/</result><resultname="null">/</result></action>我只是想实现在一个页面上数据数据库的数据并显示,现在很乱求指导!!!