1 CustomerAction

完整代码:

  1. package www.test.web.action;
  2.  
  3. import java.io.File;
  4.  
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.hibernate.criterion.DetachedCriteria;
  7. import org.hibernate.criterion.Restrictions;
  8.  
  9. import com.opensymphony.xwork2.ActionContext;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import com.opensymphony.xwork2.ModelDriven;
  12.  
  13. import www.test.domain.Customer;
  14. import www.test.service.CustomerService;
  15. import www.test.utils.PageBean;
  16.  
  17. public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
  18.  
  19. private Customer customer = new Customer();
  20.  
  21. private CustomerService cs;
  22. private Integer currentPage;
  23. private Integer pageSize;
  24.  
  25. //上传的文件会自动封装到File对象中
  26. //在后台提供一个与前台<input type=file name=photo/> 组件name相同的属性即可。
  27. private File photo;
  28. //在提交的键名后加上固定的后缀FileName,文件的名称会自动封装到属性中。
  29. private String photoFileName;
  30. //在提交的键名后加上固定的后缀ContentType,文件的MIME类型值会自动封装到属性中。
  31. private String photoContentType;
  32.  
  33. //1 获取客户列表
  34. public String list() throws Exception {
  35. //封装离线查询对象
  36. DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
  37.  
  38. //判断并封装参数
  39. if(StringUtils.isNotBlank(customer.getCust_name())){
  40. dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
  41. }
  42.  
  43. //1 调用Service查询分页数据(PageBean)
  44. PageBean pb = cs.getPageBean(dc,currentPage,pageSize);
  45. //2 将PageBean放入request域,转发到列表页面显示
  46. ActionContext.getContext().put("pageBean", pb);
  47. return "list";
  48. }
  49.  
  50. //2 保存客户
  51. public String add() throws Exception {
  52.  
  53. if(photo!=null){
  54. System.out.println("文件名称:"+photoFileName);
  55. System.out.println("文件MIME类型:"+photoContentType);
  56.  
  57. //将上传文件保存到指定位置
  58. //renameTo相当于剪切==>复制
  59. photo.renameTo(new File("C:/Users/jepson/Pictures/Saved Pictures/"+photoFileName));
  60. }
  61. //===============================================
  62.  
  63. //调用service,保存Customer对象
  64. cs.save(customer);
  65. //重定向到Action
  66. return "toList";
  67.  
  68. }
  69.  
  70. // 3 修改客户
  71. public String toEdit() throws Exception {
  72. // 1调用service层根据id查询客户
  73. Customer c = cs.getById(customer.getCust_id());
  74.  
  75. // 2将获取到的客户放入request域中
  76. ActionContext.getContext().put("customer", c);
  77.  
  78. return "edit";
  79.  
  80. }
  81.  
  82. @Override
  83. public Customer getModel() {
  84.  
  85. return customer;
  86. }
  87.  
  88. public CustomerService getCs() {
  89. return cs;
  90. }
  91.  
  92. public void setCs(CustomerService cs) {
  93. this.cs = cs;
  94. }
  95.  
  96. public Integer getCurrentPage() {
  97. return currentPage;
  98. }
  99.  
  100. public void setCurrentPage(Integer currentPage) {
  101. this.currentPage = currentPage;
  102. }
  103.  
  104. public Integer getPageSize() {
  105. return pageSize;
  106. }
  107.  
  108. public void setPageSize(Integer pageSize) {
  109. this.pageSize = pageSize;
  110. }
  111.  
  112. public File getPhoto() {
  113. return photo;
  114. }
  115.  
  116. public void setPhoto(File photo) {
  117. this.photo = photo;
  118. }
  119.  
  120. public String getPhotoFileName() {
  121. return photoFileName;
  122. }
  123.  
  124. public void setPhotoFileName(String photoFileName) {
  125. this.photoFileName = photoFileName;
  126. }
  127.  
  128. public String getPhotoContentType() {
  129. return photoContentType;
  130. }
  131.  
  132. public void setPhotoContentType(String photoContentType) {
  133. this.photoContentType = photoContentType;
  134. }
  135. }

2 CustomerService&CustomerServiceImpl

完整代码:

  1. package www.test.service;
  2.  
  3. import org.hibernate.criterion.DetachedCriteria;
  4.  
  5. import www.test.domain.Customer;
  6. import www.test.utils.PageBean;
  7.  
  8. public interface CustomerService {
  9.  
  10. PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize);
  11.  
  12. //保存客户
  13. void save(Customer customer);
  14.  
  15. //根据id查询客户
  16. Customer getById(Long cust_id);
  17.  
  18. }
  1. package www.test.service.impl;
  2.  
  3. import java.util.List;
  4.  
  5. import org.hibernate.criterion.DetachedCriteria;
  6.  
  7. import www.test.dao.CustomerDao;
  8. import www.test.domain.Customer;
  9. import www.test.service.CustomerService;
  10. import www.test.utils.PageBean;
  11.  
  12. public class CustomerServiceImpl implements CustomerService {
  13.  
  14. private CustomerDao cd;
  15. @Override
  16. // 1 封装一个PageBean返回给web层
  17. public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
  18.  
  19. // 1 调用dao层查询总记录数
  20. Integer totalCount = cd.getTotalCount(dc);
  21.  
  22. // 2创建pageBean对象
  23. PageBean pb = new PageBean(currentPage, pageSize, totalCount);
  24.  
  25. // 3查询客户列表
  26. List<Customer> list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());
  27.  
  28. // 4返回pageBean
  29. pb.setList(list);
  30. return pb;
  31. }
  32.  
  33. @Override
  34. // 2 保存客户
  35. public void save(Customer customer) {
  36. //1 维护Customer与数据字典对象的关系
  37. //正常情况下:应该调用Dao取出数据字典对象,将数据字典对象设置到Customer对象的对应属性中。
  38. //但是:由于struts的参数封装会将参数封装到数据字典的id属性。那么无需手动维护关系。
  39.  
  40. //2调用Dao保存客户
  41. cd.saveOrUpdate(customer);
  42. }
  43.  
  44. @Override
  45. // 3 根据id查询客户
  46. public Customer getById(Long cust_id) {
  47. Customer customer = cd.getById(cust_id);
  48. return customer;
  49. }
  50.  
  51. public CustomerDao getCd() {
  52. return cd;
  53. }
  54. public void setCd(CustomerDao cd) {
  55. this.cd = cd;
  56. }
  57.  
  58. }

3 BaseDao&BaseDaoImpl

  1. package www.test.dao;
  2.  
  3. import java.io.Serializable;
  4. import java.util.List;
  5.  
  6. import org.hibernate.criterion.DetachedCriteria;
  7.  
  8. public interface BaseDao<T> {
  9.  
  10. //增或修改 [实质是根据要保存的对象是否有id属性判断的]
  11. void saveOrUpdate(T t);
  12.  
  13. //增
  14. void save(T t);
  15.  
  16. //删
  17. void delete(T t);
  18.  
  19. //删 Serializable包括了8大包装类型
  20. void delete(Serializable id);
  21.  
  22. //改
  23. void update(T t);
  24.  
  25. //查 根据id查询
  26. T getById(Serializable id);
  27.  
  28. //查 符合条件的总记录数
  29. Integer getTotalCount(DetachedCriteria dc);
  30.  
  31. //查 查询分页列表数据
  32. List<T> getPageList(DetachedCriteria dc,Integer start,Integer pageSize);
  33.  
  34. }
  1. package www.test.dao.impl;
  2.  
  3. import java.io.Serializable;
  4. import java.lang.reflect.ParameterizedType;
  5. import java.util.List;
  6.  
  7. import org.hibernate.criterion.DetachedCriteria;
  8. import org.hibernate.criterion.Projections;
  9. import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
  10.  
  11. import www.test.dao.BaseDao;
  12.  
  13. public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
  14.  
  15. private Class clazz; // 用于接收运行期泛型类型
  16.  
  17. public BaseDaoImpl() {
  18. // 获得当前类型的带有泛型类型的父类
  19. // this.getClass().getGenericSuperclass();运行在BaseDaoImpl的儿子(谁继承了它)
  20. // 所以它的父类就是BaseDaoImpl
  21. ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
  22.  
  23. // 获得运行期的泛型类型
  24. clazz = (Class) ptClass.getActualTypeArguments()[0];
  25. }
  26.  
  27. @Override
  28. //保存或者修改
  29. public void saveOrUpdate(T t) {
  30. super.getHibernateTemplate().saveOrUpdate(t);
  31. }
  32.  
  33. @Override
  34. public void save(T t) {
  35. super.getHibernateTemplate().save(t);
  36. }
  37.  
  38. @Override
  39. public void delete(T t) {
  40. super.getHibernateTemplate().delete(t);
  41. }
  42.  
  43. @Override
  44. public void delete(Serializable id) {
  45. // 先获取,然后在删除
  46. T t = this.getById(id);
  47. super.getHibernateTemplate().delete(t);
  48. }
  49.  
  50. @Override
  51. public void update(T t) {
  52. super.getHibernateTemplate().update(t);
  53. }
  54.  
  55. @Override
  56. public T getById(Serializable id) {
  57. return (T) super.getHibernateTemplate().get(clazz, id);
  58.  
  59. }
  60.  
  61. @Override
  62. public Integer getTotalCount(DetachedCriteria dc) {
  63. // 设置查询聚合函数,总记录数
  64. dc.setProjection(Projections.rowCount());
  65.  
  66. List<Long> list = (List<Long>) super.getHibernateTemplate().findByCriteria(dc);
  67.  
  68. // 清空之前设置的聚合函数
  69. dc.setProjection(null);
  70.  
  71. if (list != null && list.size() > 0) {
  72. Long count = list.get(0);
  73. return count.intValue();
  74. } else {
  75. return null;
  76. }
  77. }
  78.  
  79. @Override
  80. public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
  81.  
  82. List<T> list = (List<T>) super.getHibernateTemplate().findByCriteria(dc, start, pageSize);
  83.  
  84. return list;
  85. }
  86.  
  87. }

4 add.jsp(重点)

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <%@ taglib uri="/struts-tags" prefix="s" %>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <TITLE><s:property value="#customer==null?'添加':'修改'"/>客户</TITLE>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <LINK href="${pageContext.request.contextPath }/css/Style.css" type=text/css rel=stylesheet>
  11. <LINK href="${pageContext.request.contextPath }/css/Manage.css" type=text/css
  12. rel=stylesheet>
  13. <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
  14. <script type="text/javascript" src="${pageContext.request.contextPath }/js/my.js"></script>
  15. <script type="text/javascript">
  16. $(function(){
  17. //cust_level.dict_id是对象驱动的提交方式
  18. loadSelect("006","level","cust_level.dict_id"<s:if test="#customer.cust_level!=null">,<s:property value="#customer.cust_level.dict_id"/></s:if>);
  19.  
  20. /* <s:if test="#customer.cust_level!=null">
  21. ,<s:property value="#customer.cust_level.dict_id"/>
  22. </s:if>
  23. */
  24. /* 编译器报错,看不懂是正常的 */
  25. loadSelect("002","source","cust_source.dict_id"<s:if test="#customer.cust_source!=null">,<s:property value="#customer.cust_source.dict_id"/></s:if>);
  26. loadSelect("001","industry","cust_industry.dict_id"<s:if test="#customer.cust_industry!=null">,<s:property value="#customer.cust_industry.dict_id"/></s:if>);
  27. });
  28. </script>
  29.  
  30. <META content="MSHTML 6.00.2900.3492" name=GENERATOR>
  31. </HEAD>
  32. <BODY>
  33. <!-- 文件上传页面的三个要求
  34. 1.表单必须是post提交
  35. 2.表单提交类型必须为enctype,多段式提交。
  36. 3.文件上传使用<input type="file"/>
  37.  
  38. -->
  39.  
  40. <FORM id=form1 name=form1
  41. action="${pageContext.request.contextPath }/CustomerAction_add"
  42. method=post enctype="multipart/form-data">
  43.  
  44. <!-- 使用隐藏域将客户的id保存 -->
  45. <input type="hidden" name="cust_id" value="<s:property value="#customer.cust_id"/>">
  46.  
  47. <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
  48. <TBODY>
  49. <TR>
  50. <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg"
  51. border=0></TD>
  52. <TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg"
  53. height=20></TD>
  54. <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg"
  55. border=0></TD>
  56. </TR>
  57. </TBODY>
  58. </TABLE>
  59. <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
  60. <TBODY>
  61. <TR>
  62. <TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMG
  63. src="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD>
  64. <TD vAlign=top width="100%" bgColor=#ffffff>
  65. <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
  66. <TR>
  67. <TD class=manageHead>当前位置:客户管理 &gt; <s:property value="#customer==null?'添加':'修改'"/>客户</TD>
  68. </TR>
  69. <TR>
  70. <TD height=2></TD>
  71. </TR>
  72. </TABLE>
  73.  
  74. <TABLE cellSpacing=0 cellPadding=5 border=0>
  75.  
  76. <TR>
  77. <td>客户名称:</td>
  78. <td>
  79. <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_name"/>"
  80. style="WIDTH: 180px" maxLength=50 name="cust_name">
  81. </td>
  82. <td>客户级别 :</td>
  83. <td id="level">
  84.  
  85. </td>
  86. </TR>
  87.  
  88. <TR>
  89.  
  90. <td>信息来源 :</td>
  91. <td id="source">
  92.  
  93. </td>
  94. <td>客户行业:</td>
  95. <td id="industry">
  96.  
  97. </td>
  98. </TR>
  99.  
  100. <TR>
  101. <td>固定电话 :</td>
  102. <td>
  103. <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_phone"/>"
  104. style="WIDTH: 180px" maxLength=50 name="cust_phone">
  105. </td>
  106. <td>移动电话 :</td>
  107. <td>
  108. <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_mobile"/>"
  109. style="WIDTH: 180px" maxLength=50 name="cust_mobile">
  110. </td>
  111. </TR>
  112. <!-- 文件上传页面的三个要求
  113. 1.表单必须是post提交
  114. 2.表单提交类型必须为enctype,多段式提交。
  115. 3.文件上传使用<input type="file"/>
  116.  
  117. -->
  118. <TR>
  119. <td>图片上传 :</td>
  120. <td>
  121. <input type="file" name="photo">
  122. </td>
  123.  
  124. </TR>
  125.  
  126. <tr>
  127. <td rowspan=2>
  128. <INPUT class=button id=sButton2 type=submit
  129. value="保存 " name=sButton2>
  130. </td>
  131. </tr>
  132. </TABLE>
  133.  
  134. </TD>
  135. <TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg">
  136. <IMG src="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD>
  137. </TR>
  138. </TBODY>
  139. </TABLE>
  140. <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
  141. <TBODY>
  142. <TR>
  143. <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg"
  144. border=0></TD>
  145. <TD align=middle width="100%"
  146. background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD>
  147. <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg"
  148. border=0></TD>
  149. </TR>
  150. </TBODY>
  151. </TABLE>
  152. </FORM>
  153. </BODY>
  154. </HTML>

5 list.jsp

6 struts.xml

案例53-crm练习修改客户功能实现的更多相关文章

  1. 【JAVAEE学习笔记】hibernate01:简介、搭建、配置文件详解、API详解和CRM练习:保存客户

    今日学习:hibernate是什么 一.hibernate是什么 框架是什么: 1.框架是用来提高开发效率的 2.封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架 ...

  2. JAVAEE学习——hibernate01:简介、搭建、配置文件详解、API详解和CRM练习:保存客户

    今日学习:hibernate是什么 一.hibernate是什么 框架是什么: 1.框架是用来提高开发效率的 2.封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架 ...

  3. .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能

    原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...

  4. JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册   显示错误信息到页面上的另一种方法: public ...

  5. CRM系统对管理客户的帮助

    我们可以把客户关系看做是一种长期的投资,在资源有限的基础上,把人力财力物力放到那些能够持续创造价值的客户身上,从而为企业带来源源不断的收益.通过进行客户关系管理,能够让企业与客户之间建立沟通的渠道,形 ...

  6. 企业管理CRM不只是客户录入系统

    企业在举办营销活动或者展会之后,将会收集到大量的客户信息,将这些信息有效地整理.完善.储存也是一个不小的工程.如果您的企业经常面遇到这样的情况,不妨使用Zoho CRM系统来帮您完成.但是,Zoho ...

  7. oracle修改审计功能

    oracle修改审计功能 如果没有关闭审计功能,审计日志文件默认保存在位置为$ORACLE_BASE/admin/$ORACLE_SID/adump/ 关闭审计:alter system set au ...

  8. Microsoft Dynamics CRM 如何修改域密码

    一.安装IIS6脚本工具,如下图所示: 二.复制iisadmpwd文件夹到AD Server的C:\Windows\SysWOW64\inetsrv文件夹下 三.注册Iisadmpwd目录下的IISp ...

  9. Oracle块修改跟踪功能

    块修改跟踪功能是指使用二进制文件记录数据库中数据库更改的过程. 其目的是提高增量备份操作的性能,因为RMAN可以使用快修改跟踪文件找到上次执行备份操作后被修改的数据块.这可以节省大量时间,因为如果不这 ...

随机推荐

  1. Android之ContextMenu的使用方法以及与OptionMenu的区别(转)

    >> ContextMenu是android的context menu上下文菜单,选择某项VIEW后长按menu键,就会显示出来.比如EditeText就可以通过长按来弹出拥有“cut”, ...

  2. appium自动化安装(二)

    第二节  安装Android开发环境 如果你的环境是MAC那么可以直接跳过这一节.就像我们在用Selenium进行web自动化测试的时候一样,我们需要一个浏览器来执行测试脚本.那么移动端自动化测试,我 ...

  3. c# 匿名函数和lamda表达式语法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. NPOI读取Excel文件

    public class ExcelOperator { public static List<DataTable> Read(string fileName) { List<Dat ...

  5. Django之QuerySet 创建对象

    在前面的模型介绍中设置了3个对象,出版商(publisher),作者(Authro),书籍(book).首先我们在网页中添加各个对象信息填写的界面.填写后点击提交.将会传递给后端.传递方式采用post ...

  6. JDK源码 Integer.bitCount(i)

    1.问题:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 2.解决方法很多,JDK提供了一种,如下图 /** * Returns the number of one-bits in ...

  7. P3749 [六省联考2017]寿司餐厅 最小割

    \(\color{#0066ff}{ 题目描述 }\) Kiana 最近喜欢到一家非常美味的寿司餐厅用餐. 每天晚上,这家餐厅都会按顺序提供 \(n\) 种寿司,第 \(i\) 种寿司有一个代号 \( ...

  8. Qt 学习之路 2(4):信号槽

    Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽  豆子  2012年8月23日  Qt 学习之路 2  110条评论 信号槽是 Qt 框架引以 ...

  9. mysql 配置utf8 编码,支持 emoji 方法!!!

    utf8_general_ci 已经 过时了...请以后用mysql 考虑使用 utf8mb4, utf8mb4_unicode_ci!!! 兼容性更好. mysql的utf8编码的一个字符最多3个字 ...

  10. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_方法执行

    [前言] 方法执行前,CLR 会检测方法内代码引用的所有类型.同时 CLR 会分配一个内部数据结构,用来管理对所有引用的类型的访问. 首次执行方法时,托管程序集会把 IL 转换成本地 CPU 指令,并 ...