SSI+Jquery实现增删改查(无刷新).doc
-
资源ID:2395041
资源大小:116.50KB
全文页数:24页
- 资源格式: DOC
下载积分:8金币
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
SSI+Jquery实现增删改查(无刷新).doc
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、 写实体beanStudent.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;public 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 setPhone(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<Student> queryAll();/查询全部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.SqlMapClient;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 = sqlMapClient.delete("del", id);if(b>0)return true;elsereturn false; catch (SQLException e) e.printStackTrace();return false;public List<Student> queryAll() List<Student> list = null;try list = sqlMapClient.queryForList("queryAll"); catch (SQLException e) e.printStackTrace();return list;public Student queryById(int id) Student 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;return 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 interface StudentService public List<Student> 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.dao.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<Student> queryAll() List<Student> list = sdao.queryAll();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;elsereturn 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.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<Student> list;private int id ;private String 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<Student> getList() return list;public void setList(List<Student> 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<Student> list = ss.queryAll();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.toString();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 JSONObject();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 String 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;public 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 void 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(String email) this.email = email;4、 编写配置文件Struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-/Apache Software Foundation/DTD Struts Configuration 2.0/EN" "http:/struts.apache.org/dtds/struts-2.0.dtd"><struts> <!- <constant name="struts.objectFactory" value="spring" /> -> <!-定义一个常量 替换i18n 默认信息-> <constant name="struts.custom.i18n.resources" value="message"/> <package name="json" extends="json-default"> <action name="student" class="com.vstsoft.action.StudentAction" method="queryAll"> <result type="json"></result> </action> <action name="student" class="com.vstsoft.action.StudentAction" method="del"> <result type="json"></result> </action> <action name="student" class="com.vstsoft.action.StudentAction" method="queryById"> <result type="json"></result> </action> <action name="student" class="com.vstsoft.action.StudentAction" method="change"> <result type="json"></result> </action> <action name="student" class="com.vstsoft.action.StudentAction" method="save"> <result type="json"></result> </action> </package></struts>applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><!- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). -><beans xmlns="http:/www.springframework.org/schema/beans" xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance" xmlns:aop="http:/www.springframework.org/schema/aop" xmlns:tx="http:/www.springframework.org/schema/tx" xsi:schemaLocation="http:/www.springframework.org/schema/beans http:/www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp:/www.springframework.org/schema/aop http:/www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp:/www.springframework.org/schema/tx http:/www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!- 配置数据源 -> <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:140.100.100.132:1521:oracle"/> <property name="username" value="vspn14"/> <property name="password" value="vspn14"/> </bean> <!-根据dataSource和configLocation创建一个SqlMapClient-> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:/com/vstsoft/sqlmap/SqlMapConfig.xml</value> </property> <property name="dataSource"> <ref bean="myDataSource"/> </property> </bean><bean id="sdao" class="com.vstsoft.dao.impl.StudentDaoImpl"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="udao" class="com.vstsoft.dao.impl.UserDaoImpl"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="ss" class="com.vstsoft.service.impl.StudentServiceImpl"><property name="sdao" ref="sdao"/></bean><bean id="us" class="com.vstsoft.service.impl.UserServiceImpl"><property name="udao" ref="udao"/></bean> <!-初始化user对象-> <bean id="page" class="com.vstsoft.model.Page"></bean> <bean id="student" class="com.vstsoft.model.Student"></bean> <bean id="user" class="com.vstsoft.model.Users"></bean> <bean id="studentAction" class="com.vstsoft.action.StudentAction" scope="prototype"> <property name="ss" ref="ss"/> <property name="page" ref="page"/> </bean> <bean id="userAction" class="com.vstsoft.action.UserAction" scope="prototype"> <property name="us" ref="us"/> </bean></beans>Log4j.propertieslog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%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.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" <context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!- -><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet> <servlet-name> ConfigServlet </servlet-name> <servlet-class> ConfigServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> ConfigServlet </servlet-name> <url-pattern> /servlet/ConfigServlet </url-pattern> </servlet-mapping> </web-app>SqlMapConfig.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-/ibatis.apache.org/DTD SQL Map Config 2.0/EN" "http:/ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig> <!-引入实体映射文件-> <sqlMap resource="com/vstsoft/sqlmap/student.xml"/> <sqlMap resource="com/vstsoft/sqlmap/user.xml"/></sqlMapConfig>student.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-/ibatis.apache.org/DTD SQL Map 2.0/EN" "http:/ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap><!- 设置映射类的别名 -><typeAlias alias="student" type="com.vstsoft.model.Student" /><!- 查询所有学生信息 -><select id="queryAll" resultClass="student">select * from student order by id asc</select><!- 根据学生ID查询学生信息 -><select id="queryById" parameterClass="int" resultClass="student"><!CDATA select * from student where id=#id# ></select><!-添加学生信息-><insert id="saveStu" parameterClass="student">