Struts完成客户列表显示 所用的基础知识 在之前的随笔中已经讲过。这篇是介绍如何使用Struts 完成客户列表显示 。
下面是 完成的代码执行逻辑图:
抽取项目部分代码 相信大家认真看一遍就明白的:
jsp 页面代码片段:
<TABLE cellSpacing=0 cellPadding=3 width=165 align=center border=0> <TBODY> <TR> <TD class=mainMenu onClick="MenuDisplay('table_1');"><SPAN class=span id=table_1Span>+</SPAN> 客户管理</TD> </TR> <TR> <TD> <TABLE id=table_1 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/customer/add.jsp" target=main>- 新增客户</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/CustomerAction_list" target=main>- 客户列表</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD background=images/new_027.jpg height=1></TD> </TR> <TR> <TD class=mainMenu onClick="MenuDisplay('table_2');"><SPAN class=span id=table_2Span>+</SPAN> 联系人管理</TD> </TR> <TR> <TD> <TABLE id=table_2 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/linkman/add.jsp" target=main>- 新增联系人</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="linkmanServlet?method=list" target=main>-联系人列表</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD background=images/new_027.jpg height=1></TD> </TR> <TR> <TD class=mainMenu onClick="MenuDisplay('table_5');"><SPAN class=span id=table_5Span>+</SPAN> 客户拜访管理</TD> </TR> <TR> <TD> <TABLE id=table_5 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-新增客户拜访</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-客户拜访列表</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD background=images/new_027.jpg height=1></TD> </TR> <TR> <TD class=mainMenu onClick="MenuDisplay('table_3');"><SPAN class=span id=table_3Span>+</SPAN> 综合查询</TD> </TR> <TR> <TD> <TABLE id=table_3 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>- 客户信息查询</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>- 联系人信息查询</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>- 客户拜访记录查询</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD background=images/new_027.jpg height=1></TD> </TR> <TR> <TD class=mainMenu onClick="MenuDisplay('table_4');"><SPAN class=span id=table_4Span>+</SPAN> 统计分析</TD> </TR> <TR> <TD> <TABLE id=table_4 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-客户行业统计</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-客户来源统计</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD background=images/new_027.jpg height=1></TD> </TR> <TR> <TD class=mainMenu onClick="MenuDisplay('table_6');"><SPAN class=span id=table_6Span>+</SPAN>系统管理</TD> </TR> <TR> <TD> <TABLE id=table_6 style="DISPLAY: none" cellSpacing=0 cellPadding=2 width=155 align=center border=0> <TBODY> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-角色管理</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-用户管理</A></TD> </TR> <TR> <TD class=menuSmall><A class=style2 href="#" target=main>-数据字典</A></TD> </TR> </TBODY> </TABLE> </TD> </TR> </TBODY> </TABLE>
Action类 代码:
1 public class CustomerAction extends ActionSupport { 2 private CustomerService cs = new CustomerServiceImpl(); 3 4 public String list() throws Exception { 5 //1 接受参数 6 String cust_name = ServletActionContext.getRequest().getParameter("cust_name"); 7 //2 创建离线查询对象 8 DetachedCriteria dc =DetachedCriteria.forClass(Customer.class); 9 //3 判断参数拼装条件 10 //StringUtils.isNotBlank 静态方法 判断 11 //当cust_name 不为null 不为 ""(空字符串) ,不为空格时 返回true 12 if(StringUtils.isNotBlank(cust_name)){ 13 dc.add(Restrictions.like("cust_name", "%"+cust_name+"%")); 14 } 15 //4 调用Service将离线对象传递 16 List<Customer> list = cs.getAll(dc); 17 //5 将返回的list放入request域.转发到list.jsp显示 18 //引用ServletActionContext类的静态方法getRequest 19 //getRequest返回HttpServletRequest对象 20 ServletActionContext.getRequest().setAttribute("list", list); 21 22 return "list"; 23 } 24 }
Struts.xml :
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <!-- 指定struts2是否以开发模式运行 7 1.热加载主配置.(不需要重启即可生效) 8 2.提供更多错误信息输出,方便开发时的调试 9 --> 10 <constant name="struts.devMode" value="true"></constant> 11 <package name="crm" namespace="/" extends="struts-default" > 12 <action name="CustomerAction_*" class="cn.itheima.web.action.CustomerAction" method="{1}" > 13 <result name="list" >/jsp/customer/list.jsp</result> 14 </action> 15 </package> 16 </struts>
ServiceImpl 类
2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import org.hibernate.criterion.DetachedCriteria; 8 9 import cn.itheima.dao.CustomerDao; 10 import cn.itheima.dao.impl.CustomerDaoImpl; 11 import cn.itheima.domain.Customer; 12 import cn.itheima.service.CustomerService; 13 import cn.itheima.utils.HibernateUtils; 14 15 public class CustomerServiceImpl implements CustomerService { 16 17 private CustomerDao customerDao = new CustomerDaoImpl(); 18 19 public List<Customer> getAll() { 20 Session session = HibernateUtils.getCurrentSession(); 21 //打开事务 22 Transaction tx = session.beginTransaction(); 23 24 List<Customer> list = customerDao.getAll(); 25 26 27 //关闭事务 28 tx.commit(); 29 return list; 30 } 31 32 public List<Customer> getAll(DetachedCriteria dc) { 33 Session session = HibernateUtils.getCurrentSession(); 34 //打开事务 35 Transaction tx = session.beginTransaction(); 36 37 List<Customer> list = customerDao.getAll(dc); 38 39 40 //关闭事务 41 tx.commit(); 42 return list; 43 } 44 45 }
dao层实现类:
2 3 import java.util.List; 4 5 import org.hibernate.Criteria; 6 import org.hibernate.Session; 7 import org.hibernate.Transaction; 8 import org.hibernate.criterion.DetachedCriteria; 9 10 import cn.itheima.dao.CustomerDao; 11 import cn.itheima.domain.Customer; 12 import cn.itheima.utils.HibernateUtils; 13 14 public class CustomerDaoImpl implements CustomerDao { 15 public List<Customer> getAll() { 16 //1 获得session 17 Session session = HibernateUtils.getCurrentSession(); 18 //2 创建Criteria对象 19 Criteria c = session.createCriteria(Customer.class); 20 return c.list(); 21 } 22 }