1 修改menu.jsp代码
2 jsp/customer/list.jsp代码
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
客户列表
rel=stylesheet>
1 customerservlet->findallcustomerlist方法
//2获取客户列表findallcustomerlist
public void findallcustomerlist(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
//解决乱码问题通过genericencodingfilter
list customerlist = service.findallcustomerlist();
//转换成json
gson gson = new gson();
string json = gson.tojson(customerlist);
//system.out.println(json);
/*
* [
* {"cust_id":2,"cust_name":"谷歌有限公司"},
* {"cust_id":6,"cust_name":"张三","cust_source":"阿里巴巴","cust_level":"vip","cust_linkman":"李四","cust_phone":"1234","cust_mobile":"4321"},
* {"cust_id":7,"cust_name":"新浪科技","cust_level":"贵宾","cust_linkman":"李四"},
* {"cust_id":8,"cust_name":"联想公司"},
* {"cust_id":9,"cust_name":"永安会计事务所","cust_source":"国际事务所成员","cust_level":"vip","cust_linkman":"马大强","cust_phone":"789632541","cust_mobile":"123456789"}
* ]
*/
//回写
response.setcontenttype("text/html;charset=utf-8");
response.getwriter().write(json);
}
1 customerservie接口
package www.test.service;
import java.util.list;
import www.test.domain.customer;
public interface customerservice {
//保存客户
void save(customer customer);
//获取客户列表
list findallcustomerlist();
}
2 customerserviceimpl实现类
@override
//获取客户列表,这里实际上不需要事务控制,只是为了练习而已
public list findallcustomerlist() {
//获得session
session session = hibernateutils.getcurrentsession();
//控制事务
transaction transaction = session.begintransaction();
list customerlist =null;
try {
//调用dao层获取客户列表
customerlist = dao.findallcustomerlist();
} catch (exception e) {
e.printstacktrace();
transaction.rollback();
}finally{
transaction.commit();
}
1 customerdao接口
package www.test.dao;
import java.util.list;
import www.test.domain.customer;
public interface customerdao {
//保存客户
void save(customer customer);
//获取客户列表
list findallcustomerlist();
}
2 customerdaoimpl实现类
@override
//获取客户列表
public list findallcustomerlist() {
//获得session
session session = hibernateutils.getcurrentsession();
//创建执行者对象
criteria criteria = session.createcriteria(customer.class);
//执行操作
list list = criteria.list();
return list;
}
1 baseservlet
package www.test.web.servlet;
import java.io.ioexception;
import java.lang.reflect.method;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
@suppresswarnings("all")
public class baseservlet extends httpservlet {
@override
protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
//解决乱码问题
resp.setcontenttype("text/html;charset=utf-8");
try {
// 1 获得请求的method方法
string methodname = req.getparameter("method");
// 2获得当前被访问的对象的字节码对象
class clazz = this.getclass(); //productservlet.class --- userservlet.class
// 3 获取当前字节码对象中指定的方法
method method = clazz.getmethod(methodname,httpservletrequest.class,httpservletresponse.class);
// 4 执行相应的方法
method.invoke(this,req,resp);
} catch (exception e) {
e.printstacktrace();
}
}
}
2 beanfactory工具类
package www.test.utils;
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
@suppresswarnings("all")
public class beanfactory {
public static object getbean(string id){
//生成对象--根据清单生产--配置文件--将每一个bean对象的生产细节配置到配置文件中
//使用dom4j的xml解析技术 导入两个jar包
// dom4j-1.6.1.jar 和 jaxen-1.1-beta-6.jar
try {
// 1 创建解析器
saxreader reader = new saxreader();
// 2 解析文档--bean.xml 在src下面
string path = beanfactory.class.getclassloader().getresource("bean.xml").getpath();
//读取
document doc = reader.read(path);
// 3 获得元素--参数是xpath规则
element element = (element) doc.selectsinglenode("//bean[@id='" id "']");
//
string classname = element.attributevalue("class");
//www.test.service.impl.adminserviceimpl
//使用反射创建对象
class clazz = class.forname(classname);
object object = clazz.getdeclaredconstructor().newinstance();
return object;
} catch (exception e) {
e.printstacktrace();
}
return null;
}
}
案例42-使用ajax获取crm中的客户列表的相关教程结束。