《SSI+Jquery实现增删改查(无刷新).doc》由会员分享,可在线阅读,更多相关《SSI+Jquery实现增删改查(无刷新).doc(24页珍藏版)》请在三一办公上搜索。
1、 SSI+jquery简单增删改查一、 配置环境加入jar包,我以前写过一个文档,里面已经将jar包写好,请参考:除了将这些jar包加入后,还得需要几个包,这几个包是jquery所要用到的包commons-beanutils-1.8.3.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging.jar、dwr4struts2.jar、ezmorph-1.0.6.jar、json-lib-2.2.2-jdk15.jar、jsonplugin-0.31.jar二、 编写后台代码及配置文件1、 写实体beanStud
2、ent.javapackage com.vstsoft.model;public class Studentprivate int id ;private String username ;private String stuid ;private String email ;private String phone ;private String address ;public int getId() return id;public void setId(int id) this.id = id;public String getUsername() return username;pub
3、lic void setUsername(String username) this.username = username;public String getStuid() return stuid;public void setStuid(String stuid) this.stuid = stuid;public String getEmail() return email;public void setEmail(String email) this.email = email;public String getPhone() return phone;public void set
4、Phone(String phone) this.phone = phone;public String getAddress() return address;public void setAddress(String address) this.address = address;2、 编写dao层及其实现StudentDao.javapackage com.vstsoft.dao;import java.util.List;import com.vstsoft.model.Student;public interface StudentDao public List queryAll()
5、;/查询全部public boolean save(Student student) ;/增加public boolean del(int id) ;/删除public boolean update(Student student) ;/更新public Student queryById(int id);/按ID查询StudentDaoImpl.javapackage com.vstsoft.dao.impl;import java.sql.SQLException;import java.util.List;import com.ibatis.sqlmap.client.SqlMapCli
6、ent;import com.vstsoft.dao.StudentDao;import com.vstsoft.model.Student;public class StudentDaoImpl extends Student implements StudentDao private SqlMapClient sqlMapClient;public void setSqlMapClient(SqlMapClient sqlMapClient) this.sqlMapClient = sqlMapClient;public boolean del(int id) try int b = sq
7、lMapClient.delete(del, id);if(b0)return true;elsereturn false; catch (SQLException e) e.printStackTrace();return false;public List queryAll() List list = null;try list = sqlMapClient.queryForList(queryAll); catch (SQLException e) e.printStackTrace();return list;public Student queryById(int id) Stude
8、nt student = null;try student = (Student) sqlMapClient.queryForObject(queryById, id); catch (SQLException e) e.printStackTrace();return student;public boolean save(Student student) boolean b ;try sqlMapClient.insert(saveStu, student);b = true ; catch (SQLException e) e.printStackTrace();b = false;re
9、turn b ;public boolean update(Student student) boolean b ;try sqlMapClient.update(updateStu, student);b = true; catch (SQLException e) e.printStackTrace();b = false;return b;3、 编写服务层及其实现StudentService.javapackage com.vstsoft.service;import java.util.List;import com.vstsoft.model.Student;public inter
10、face StudentService public List queryAll();/查询全部public boolean save(Student student) ;/增加public boolean del(int id) ;/删除public boolean update(Student student) ;/更新public Student queryById(int id);/按ID查询StudentServiceImpl.javapackage com.vstsoft.service.impl;import java.util.List;import com.vstsoft.d
11、ao.StudentDao;import com.vstsoft.model.Student;import com.vstsoft.service.StudentService;public class StudentServiceImpl implements StudentService private StudentDao sdao ;public boolean del(int id) boolean b = sdao.del(id);if(b)return true ;elsereturn false;public List queryAll() List list = sdao.q
12、ueryAll();return list;public Student queryById(int id) Student student = sdao.queryById(id);return student;public boolean save(Student student) boolean b = sdao.save(student);if(b)return true;elsereturn false;public boolean update(Student student) boolean b = sdao.update(student);if(b)return true;el
13、sereturn false;public StudentDao getSdao() return sdao;public void setSdao(StudentDao sdao) this.sdao = sdao;/4、编写控制层ActionStudentAction.javapackage com.vstsoft.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.
14、JSONObject;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.vstsoft.model.Student;import com.vstsoft.service.StudentService;public class StudentAction extends ActionSupportprivate StudentService ss ;private List list;private int id ;private Strin
15、g username ;private String stuid;private String phone;private String address;private String email;public String getUsername() return username;public void setUsername(String username) this.username = username;public int getId() return id;public void setId(int id) this.id = id;public List getList() re
16、turn list;public void setList(List list) this.list = list;public StudentService getSs() return ss;public void setSs(StudentService ss) this.ss = ss;HttpServletRequest request = ServletActionContext.getRequest();public String queryAll() throws Exception System.out.println(*queryAll*);List list = ss.q
17、ueryAll();JSONObject json = new JSONObject();json.accumulate(list, list);System.out.println(json+json);HttpServletResponse response = ServletActionContext.getResponse();response.setContentType(text/x-json;charset=UTF-8);response.setHeader(Cache-Control, no-cache); response.getWriter().print(json.toS
18、tring();return null;public String del() throws Exception System.out.println(del);System.out.println(id=+this.getId();ss.del(this.getId();return null;public String queryById() throws Exception System.out.println(id=+this.getId();Student student = ss.queryById(this.getId();JSONObject json = new JSONOb
19、ject();json.accumulate(student, student);System.out.println(json=+json);HttpServletResponse response = ServletActionContext.getResponse();response.setContentType(text/x-json;charset=UTF-8);response.setHeader(Cache-Control, no-cache); response.getWriter().print(json.toString();return null;public Stri
20、ng change() throws Exception System.out.println(*change*);Student student = new Student();student.setUsername(this.username);student.setStuid(this.stuid);student.setPhone(this.phone);student.setEmail(this.email);student.setAddress(this.address);student.setId(this.id);ss.update(student);return null;p
21、ublic String save() throws Exception Student student = new Student();student.setUsername(this.username);student.setStuid(this.stuid);student.setPhone(this.phone);student.setEmail(this.email);student.setAddress(this.address);ss.save(student);return null;public String getStuid() return stuid;public vo
22、id setStuid(String stuid) this.stuid = stuid;public String getPhone() return phone;public void setPhone(String phone) this.phone = phone;public String getAddress() return address;public void setAddress(String address) this.address = address;public String getEmail() return email;public void setEmail(
23、String email) this.email = email;4、 编写配置文件Struts.xml !- - applicationContext.xml classpath:/com/vstsoft/sqlmap/SqlMapConfig.xml Log4j.propertieslog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPatt
24、ern=%d %p %c - %m%n.ibatis=debugmon.jdbc.SimpleDataSource=debugmon.jdbc.ScriptRunner=debug.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debuglog4j.logger.java.sql.Connection=debuglog4j.logger.java.sql.Statement=debuglog4j.logger.java.sql.PreparedStatement=debug,stdoutweb.xmlweb-app version=2.4 xml
25、ns=xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instancexsi:schemaLocation= contextConfigLocationclasspath*:applicationContext.xmlstruts2org.apache.struts2.dispatcher.FilterDispatcherstruts2/*org.springframework.web.context.ContextLoaderListenerindex.jsp ConfigServlet ConfigServlet ConfigServlet /servlet/ConfigServlet SqlMapConfig.xml student.xmlselect * from student order by id asc