Struts完成客户列表显示 所用的基础知识 在之前的随笔中已经讲过。这篇是介绍如何使用Struts 完成客户列表显示  。

下面是  完成的代码执行逻辑图:

抽取项目部分代码 相信大家认真看一遍就明白的:

jsp 页面代码片段:

  1. <TABLE cellSpacing=0 cellPadding=3 width=165 align=center border=0>
  2. <TBODY>
  3. <TR>
  4. <TD class=mainMenu onClick="MenuDisplay('table_1');"><SPAN
  5. class=span id=table_1Span></SPAN> 客户管理</TD>
  6. </TR>
  7. <TR>
  8. <TD>
  9. <TABLE id=table_1 style="DISPLAY: none" cellSpacing=0
  10. cellPadding=2 width=155 align=center border=0>
  11. <TBODY>
  12. <TR>
  13. <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/customer/add.jsp"
  14. target=main>- 新增客户</A></TD>
  15. </TR>
  16. <TR>
  17. <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/CustomerAction_list"
  18. target=main>- 客户列表</A></TD>
  19. </TR>
  20.  
  21. </TBODY>
  22. </TABLE>
  23. </TD>
  24. </TR>
  25. <TR>
  26. <TD background=images/new_027.jpg height=1></TD>
  27. </TR>
  28. <TR>
  29. <TD class=mainMenu onClick="MenuDisplay('table_2');"><SPAN
  30. class=span id=table_2Span></SPAN> 联系人管理</TD>
  31. </TR>
  32. <TR>
  33. <TD>
  34. <TABLE id=table_2 style="DISPLAY: none" cellSpacing=0
  35. cellPadding=2 width=155 align=center border=0>
  36. <TBODY>
  37. <TR>
  38. <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/linkman/add.jsp"
  39. target=main>- 新增联系人</A></TD>
  40. </TR>
  41. <TR>
  42. <TD class=menuSmall><A class=style2 href="linkmanServlet?method=list"
  43. target=main>-联系人列表</A></TD>
  44. </TR>
  45.  
  46. </TBODY>
  47. </TABLE>
  48. </TD>
  49. </TR>
  50. <TR>
  51. <TD background=images/new_027.jpg height=1></TD>
  52. </TR>
  53. <TR>
  54. <TD class=mainMenu onClick="MenuDisplay('table_5');"><SPAN
  55. class=span id=table_5Span></SPAN> 客户拜访管理</TD>
  56. </TR>
  57. <TR>
  58. <TD>
  59. <TABLE id=table_5 style="DISPLAY: none" cellSpacing=0
  60. cellPadding=2 width=155 align=center border=0>
  61. <TBODY>
  62. <TR>
  63. <TD class=menuSmall><A class=style2 href="#"
  64. target=main>-新增客户拜访</A></TD>
  65. </TR>
  66. <TR>
  67. <TD class=menuSmall><A class=style2 href="#"
  68. target=main>-客户拜访列表</A></TD>
  69. </TR>
  70.  
  71. </TBODY>
  72. </TABLE>
  73. </TD>
  74. </TR>
  75. <TR>
  76. <TD background=images/new_027.jpg height=1></TD>
  77. </TR>
  78. <TR>
  79. <TD class=mainMenu onClick="MenuDisplay('table_3');"><SPAN
  80. class=span id=table_3Span></SPAN> 综合查询</TD>
  81. </TR>
  82. <TR>
  83. <TD>
  84. <TABLE id=table_3 style="DISPLAY: none" cellSpacing=0
  85. cellPadding=2 width=155 align=center border=0>
  86. <TBODY>
  87. <TR>
  88. <TD class=menuSmall><A class=style2 href="#"
  89. target=main>- 客户信息查询</A></TD>
  90. </TR>
  91. <TR>
  92. <TD class=menuSmall><A class=style2 href="#"
  93. target=main>- 联系人信息查询</A></TD>
  94. </TR>
  95. <TR>
  96. <TD class=menuSmall><A class=style2 href="#"
  97. target=main>- 客户拜访记录查询</A></TD>
  98. </TR>
  99. </TBODY>
  100. </TABLE>
  101. </TD>
  102. </TR>
  103. <TR>
  104. <TD background=images/new_027.jpg height=1></TD>
  105. </TR>
  106. <TR>
  107. <TD class=mainMenu onClick="MenuDisplay('table_4');"><SPAN
  108. class=span id=table_4Span></SPAN> 统计分析</TD>
  109. </TR>
  110. <TR>
  111. <TD>
  112. <TABLE id=table_4 style="DISPLAY: none" cellSpacing=0
  113. cellPadding=2 width=155 align=center border=0>
  114. <TBODY>
  115. <TR>
  116. <TD class=menuSmall><A class=style2 href="#"
  117. target=main>-客户行业统计</A></TD>
  118. </TR>
  119. <TR>
  120. <TD class=menuSmall><A class=style2 href="#"
  121. target=main>-客户来源统计</A></TD>
  122. </TR>
  123.  
  124. </TBODY>
  125. </TABLE>
  126. </TD>
  127. </TR>
  128. <TR>
  129. <TD background=images/new_027.jpg height=1></TD>
  130. </TR>
  131. <TR>
  132. <TD class=mainMenu onClick="MenuDisplay('table_6');"><SPAN
  133. class=span id=table_6Span></SPAN>系统管理</TD>
  134. </TR>
  135. <TR>
  136. <TD>
  137. <TABLE id=table_6 style="DISPLAY: none" cellSpacing=0
  138. cellPadding=2 width=155 align=center border=0>
  139. <TBODY>
  140. <TR>
  141. <TD class=menuSmall><A class=style2 href="#"
  142. target=main>-角色管理</A></TD>
  143. </TR>
  144. <TR>
  145. <TD class=menuSmall><A class=style2 href="#"
  146. target=main>-用户管理</A></TD>
  147. </TR>
  148. <TR>
  149. <TD class=menuSmall><A class=style2 href="#"
  150. target=main>-数据字典</A></TD>
  151. </TR>
  152. </TBODY>
  153. </TABLE>
  154. </TD>
  155. </TR>
  156. </TBODY>
  157. </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 类

  1. import java.util.List;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.criterion.DetachedCriteria;
  6.  
  7. import cn.itheima.dao.CustomerDao;
  8. import cn.itheima.dao.impl.CustomerDaoImpl;
  9. import cn.itheima.domain.Customer;
  10. import cn.itheima.service.CustomerService;
  11. import cn.itheima.utils.HibernateUtils;
  12.  
  13. public class CustomerServiceImpl implements CustomerService {
  14.  
  15. private CustomerDao customerDao = new CustomerDaoImpl();
  16.  
  17. public List<Customer> getAll() {
  18. Session session = HibernateUtils.getCurrentSession();
  19. //打开事务
  20. Transaction tx = session.beginTransaction();
  21.  
  22. List<Customer> list = customerDao.getAll();
  23.  
  24. //关闭事务
  25. tx.commit();
  26. return list;
  27. }
  28.  
  29. public List<Customer> getAll(DetachedCriteria dc) {
  30. Session session = HibernateUtils.getCurrentSession();
  31. //打开事务
  32. Transaction tx = session.beginTransaction();
  33.  
  34. List<Customer> list = customerDao.getAll(dc);
  35.  
  36. //关闭事务
  37. tx.commit();
  38. return list;
  39. }
  40.  
  41. }

dao层实现类:

  1. import java.util.List;
  2.  
  3. import org.hibernate.Criteria;
  4. import org.hibernate.Session;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.criterion.DetachedCriteria;
  7.  
  8. import cn.itheima.dao.CustomerDao;
  9. import cn.itheima.domain.Customer;
  10. import cn.itheima.utils.HibernateUtils;
  11.  
  12. public class CustomerDaoImpl implements CustomerDao {
  13. public List<Customer> getAll() {
  14. //1 获得session
  15. Session session = HibernateUtils.getCurrentSession();
  16. //2 创建Criteria对象
  17. Criteria c = session.createCriteria(Customer.class);
  18. return c.list();
  19. }
  20. }

Struts2(六) 用Struts完成客户列表显示的更多相关文章

  1. 转载 Struts2的配置 struts.xml Action详解

    在学习struts的时候,我们一定要掌握struts2的工作原理.只有当我们明确了在struts2框架的内部架构的实现过程,在配置整个struts 的框架时,可以很好的进行逻辑上的配置.接下来我就先简 ...

  2. struts2学习笔记--struts.xml配置文件详解

    这一节主要讲解struts2里面的struts.xml的常用标签及作用: 解决乱码问题 <constant name="struts.i18n.encoding" value ...

  3. Struts2学习笔记--Struts例子及开发流程

    参考资料:http://blog.csdn.net/hntyzgn2010/article/details/5547753 http://chenlh.iteye.com/blog/464341 入门 ...

  4. struts2.0中struts.xml配置文件详解

    先来展示一个配置文件 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration ...

  5. 1-1 struts2 基本配置 struts.xml配置文件详解

    详见http://www.cnblogs.com/dooor/p/5323716.html 一. struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomc ...

  6. struts2之配置文件struts.xml详解

    struts配置文件 struts.xml配置参数详解 struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解  以便于理解 和修改 <?xml version=" ...

  7. Struts2学习:struts.xml引入自定义的xml文件

    随着项目代码的增多,用一个struts.xml来管理所有功能模块的Action未免显得臃肿且结构不清晰,因此可以根据实际的功能划分,将各模块的Action放在自定义的xml文件中,再引入struts. ...

  8. Struts2(六)result

    一.result简述 result:输出结果:第个Action返回一个字符串,Struts2根据这个值来决定响应结果 name属性:result的逻辑名.和Actin里的返回值匹配,默认"s ...

  9. Struts2(六.用标签显示用户列表及Value Stack和Stack Context)

    一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...

随机推荐

  1. 夏令营讲课内容整理 Day 7.

    Day7是夏令营的最后一天,这一天主要讲了骗分技巧和往年经典的一些NOIP试题以及比赛策略. 这天有个小插曲,上午的day7T3是一道和树有关的题,我是想破脑袋也想不出来,正解写不出来就写暴力吧,暴力 ...

  2. BZOJ 2084: [Poi2010]Antisymmetry [Manacher]

    2084: [Poi2010]Antisymmetry Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 609  Solved: 387[Submit] ...

  3. CocosCreator游戏开发---菜鸟学习之路(一)

    PS(废话): 辞职后在家好久好久了,久到经济不允许了,接着就准备再次出去找工作了,然而工作哪有那么好找,特别是像我这种菜鸟.而且我还准备转行,准备去做游戏,技能等级接近于0,那工作就更难找了.既然如 ...

  4. 使用netstat检测及监测网络连接

    在讲监测检测之前,先给大家在普及一个知识,那就是TCP连接的状态,TCP进行3次握手,其过程有很多状态,不同的连接状态,都有想对应的状态码,看下面列表: LISTEN:侦听来自远方的TCP端口的连接请 ...

  5. [bzoj3875] [Ahoi2014]骑士游戏

    3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 844  Solved: 440[Submit][Status ...

  6. 微信开发系列——微信订阅号前端开发利器:WeUI

    前言:年前的两个星期,学习了下微信公众号的开发.后端基本能够基于盛派的第三方sdk能搞定大部分事宜.剩下的就是前端了,关于手机端的浏览器的兼容性,一直是博主的一块心病,因为博主一直专注于bootstr ...

  7. Java经典编程题50道之十五

    输入三个整数x,y,z,请把这三个数由小到大输出. public class Example15 {    public static void main(String[] args) {       ...

  8. Spring_Spring与DAO_Spring的Jdbc模板

    一.导入Jar包 二.定义实体类与DB表 public class Student { private Integer id; private String name; private int age ...

  9. js中boolean类型的理解

    <html> <head> <script type="text/javascript"> var x="12"; aler ...

  10. 轻松搭建Git服务器(Ubuntu)

    搭建Git服务器 在远程仓库节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业 ...