zoukankan      html  css  js  c++  java
  • 案例53-crm练习修改客户功能实现

    1 CustomerAction

    完整代码:

    package www.test.web.action;
    
    import java.io.File;
    
    import org.apache.commons.lang3.StringUtils;
    import org.hibernate.criterion.DetachedCriteria;
    import org.hibernate.criterion.Restrictions;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    import www.test.domain.Customer;
    import www.test.service.CustomerService;
    import www.test.utils.PageBean;
    
    public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
    
        private Customer customer = new Customer();
        
        private CustomerService cs;
        private Integer currentPage;
        private Integer pageSize;
        
        //上传的文件会自动封装到File对象中
        //在后台提供一个与前台<input type=file name=photo/> 组件name相同的属性即可。
        private File photo; 
        //在提交的键名后加上固定的后缀FileName,文件的名称会自动封装到属性中。
        private String photoFileName;
        //在提交的键名后加上固定的后缀ContentType,文件的MIME类型值会自动封装到属性中。
        private String photoContentType;
        
        
        //1  获取客户列表
        public String list() throws Exception {
            //封装离线查询对象
            DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
            
            //判断并封装参数
            if(StringUtils.isNotBlank(customer.getCust_name())){
                dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
            }
            
            //1 调用Service查询分页数据(PageBean)
            PageBean pb = cs.getPageBean(dc,currentPage,pageSize);
            //2 将PageBean放入request域,转发到列表页面显示
            ActionContext.getContext().put("pageBean", pb);
            return "list";
        }
    
        //2  保存客户
        public String add() throws Exception {
            
            if(photo!=null){
                System.out.println("文件名称:"+photoFileName);
                System.out.println("文件MIME类型:"+photoContentType);
                
                //将上传文件保存到指定位置
                //renameTo相当于剪切==>复制
                photo.renameTo(new File("C:/Users/jepson/Pictures/Saved Pictures/"+photoFileName));
            }
            //===============================================
            
            //调用service,保存Customer对象
            cs.save(customer);
            //重定向到Action
            return "toList";
            
        }
    
        // 3 修改客户
        public String toEdit() throws Exception {
            // 1调用service层根据id查询客户
            Customer c = cs.getById(customer.getCust_id());
            
            // 2将获取到的客户放入request域中
            ActionContext.getContext().put("customer", c);
            
            return "edit";
            
        }
        
        
        
    
    
        @Override
        public Customer getModel() {
            
            return customer;
        }
    
    
    
        public CustomerService getCs() {
            return cs;
        }
    
    
    
        public void setCs(CustomerService cs) {
            this.cs = cs;
        }
    
    
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
    
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
    
    
        public Integer getPageSize() {
            return pageSize;
        }
    
    
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public File getPhoto() {
            return photo;
        }
    
        public void setPhoto(File photo) {
            this.photo = photo;
        }
    
        public String getPhotoFileName() {
            return photoFileName;
        }
    
        public void setPhotoFileName(String photoFileName) {
            this.photoFileName = photoFileName;
        }
    
        public String getPhotoContentType() {
            return photoContentType;
        }
    
        public void setPhotoContentType(String photoContentType) {
            this.photoContentType = photoContentType;
        }
    }
    View Code

    2 CustomerService&CustomerServiceImpl

    完整代码:

    package www.test.service;
    
    import org.hibernate.criterion.DetachedCriteria;
    
    import www.test.domain.Customer;
    import www.test.utils.PageBean;
    
    public interface CustomerService {
    
        PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize);
    
        //保存客户
        void save(Customer customer);
    
        //根据id查询客户
        Customer getById(Long cust_id);
    
    
    }
    View Code
    package www.test.service.impl;
    
    import java.util.List;
    
    import org.hibernate.criterion.DetachedCriteria;
    
    import www.test.dao.CustomerDao;
    import www.test.domain.Customer;
    import www.test.service.CustomerService;
    import www.test.utils.PageBean;
    
    public class CustomerServiceImpl implements CustomerService {
    
        private CustomerDao cd;
        @Override
        // 1 封装一个PageBean返回给web层
        public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
    
            // 1 调用dao层查询总记录数
            Integer totalCount = cd.getTotalCount(dc);
            
            // 2创建pageBean对象
            PageBean pb = new PageBean(currentPage, pageSize, totalCount);
            
            
            // 3查询客户列表
            List<Customer> list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());
            
            // 4返回pageBean
            pb.setList(list);
            return pb;
        }
        
        @Override
        // 2 保存客户
        public void save(Customer customer) {
            //1 维护Customer与数据字典对象的关系
            //正常情况下:应该调用Dao取出数据字典对象,将数据字典对象设置到Customer对象的对应属性中。
            //但是:由于struts的参数封装会将参数封装到数据字典的id属性。那么无需手动维护关系。
            
            //2调用Dao保存客户
            cd.saveOrUpdate(customer);
        }
        
        @Override
        // 3 根据id查询客户
        public Customer getById(Long cust_id) {
            Customer customer = cd.getById(cust_id);
            return customer;
        }
        
        
        public CustomerDao getCd() {
            return cd;
        }
        public void setCd(CustomerDao cd) {
            this.cd = cd;
        }
        
    
    }
    View Code

    3 BaseDao&BaseDaoImpl

    package www.test.dao;
    
    import java.io.Serializable;
    import java.util.List;
    
    import org.hibernate.criterion.DetachedCriteria;
    
    public interface BaseDao<T> {
    
        //增或修改 [实质是根据要保存的对象是否有id属性判断的]
        void saveOrUpdate(T t);
        
        //
        void save(T t);
        
        //
        void delete(T t);
        
        //删  Serializable包括了8大包装类型
        void delete(Serializable id);
        
        //
        void update(T t);
        
        
        //查 根据id查询
        T getById(Serializable id);
         
        //查  符合条件的总记录数
        Integer getTotalCount(DetachedCriteria dc);
        
        //查 查询分页列表数据
        List<T> getPageList(DetachedCriteria dc,Integer start,Integer pageSize);
        
    }
    View Code
    package www.test.dao.impl;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.util.List;
    
    import org.hibernate.criterion.DetachedCriteria;
    import org.hibernate.criterion.Projections;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import www.test.dao.BaseDao;
    
    public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
    
        private Class clazz; // 用于接收运行期泛型类型
    
        public BaseDaoImpl() {
            // 获得当前类型的带有泛型类型的父类
            // this.getClass().getGenericSuperclass();运行在BaseDaoImpl的儿子(谁继承了它)
            // 所以它的父类就是BaseDaoImpl
            ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
    
            // 获得运行期的泛型类型
            clazz = (Class) ptClass.getActualTypeArguments()[0];
        }
    
        @Override
        //保存或者修改
        public void saveOrUpdate(T t) {
            super.getHibernateTemplate().saveOrUpdate(t);
        }
        
        
        
        @Override
        public void save(T t) {
            super.getHibernateTemplate().save(t);
        }
    
        @Override
        public void delete(T t) {
            super.getHibernateTemplate().delete(t);
        }
    
        @Override
        public void delete(Serializable id) {
            // 先获取,然后在删除
            T t = this.getById(id);
            super.getHibernateTemplate().delete(t);
        }
    
        @Override
        public void update(T t) {
            super.getHibernateTemplate().update(t);
        }
    
        @Override
        public T getById(Serializable id) {
            return (T) super.getHibernateTemplate().get(clazz, id);
    
        }
    
        @Override
        public Integer getTotalCount(DetachedCriteria dc) {
            // 设置查询聚合函数,总记录数
            dc.setProjection(Projections.rowCount());
    
            List<Long> list = (List<Long>) super.getHibernateTemplate().findByCriteria(dc);
    
            // 清空之前设置的聚合函数
            dc.setProjection(null);
    
            if (list != null && list.size() > 0) {
                Long count = list.get(0);
                return count.intValue();
            } else {
                return null;
            }
        }
    
        @Override
        public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
            
            List<T> list = (List<T>) super.getHibernateTemplate().findByCriteria(dc, start, pageSize);
            
            return list;
        }
    
        
    
    }
    View Code

    4 add.jsp(重点)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <TITLE><s:property value="#customer==null?'添加':'修改'"/>客户</TITLE> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <LINK href="${pageContext.request.contextPath }/css/Style.css" type=text/css rel=stylesheet>
    <LINK href="${pageContext.request.contextPath }/css/Manage.css" type=text/css
        rel=stylesheet>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/my.js"></script>
    <script type="text/javascript">
        $(function(){
            //cust_level.dict_id是对象驱动的提交方式
            loadSelect("006","level","cust_level.dict_id"<s:if test="#customer.cust_level!=null">,<s:property value="#customer.cust_level.dict_id"/></s:if>); 
            
            /* <s:if test="#customer.cust_level!=null">
                   ,<s:property value="#customer.cust_level.dict_id"/>
                </s:if> 
             */
            /* 编译器报错,看不懂是正常的 */
            loadSelect("002","source","cust_source.dict_id"<s:if test="#customer.cust_source!=null">,<s:property value="#customer.cust_source.dict_id"/></s:if>);
            loadSelect("001","industry","cust_industry.dict_id"<s:if test="#customer.cust_industry!=null">,<s:property value="#customer.cust_industry.dict_id"/></s:if>);
        });
    </script>
    
    <META content="MSHTML 6.00.2900.3492" name=GENERATOR>
    </HEAD>
    <BODY>
        <!-- 文件上传页面的三个要求
            1.表单必须是post提交
            2.表单提交类型必须为enctype,多段式提交。
            3.文件上传使用<input type="file"/>
                                    
         -->
         
        <FORM id=form1 name=form1
            action="${pageContext.request.contextPath }/CustomerAction_add" 
            method=post enctype="multipart/form-data">
            
            <!-- 使用隐藏域将客户的id保存 -->
            <input type="hidden" name="cust_id" value="<s:property value="#customer.cust_id"/>">
            
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg"
                            border=0></TD>
                        <TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg"
                            height=20></TD>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg"
                            border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMG
                            src="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD>
                        <TD vAlign=top width="100%" bgColor=#ffffff>
                            <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
                                <TR>
                                    <TD class=manageHead>当前位置:客户管理 &gt; <s:property value="#customer==null?'添加':'修改'"/>客户</TD>
                                </TR>
                                <TR>
                                    <TD height=2></TD>
                                </TR>
                            </TABLE>
                            
                            <TABLE cellSpacing=0 cellPadding=5  border=0>
                              
                                
                                <TR>
                                    <td>客户名称:</td>
                                    <td>
                                    <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_name"/>"
                                                            style="WIDTH: 180px" maxLength=50 name="cust_name">
                                    </td>
                                    <td>客户级别 :</td>
                                    <td id="level">
                                    
                                    </td>
                                </TR>
                                
                                <TR>
                                    
                                    <td>信息来源 :</td>
                                    <td id="source">
                                    
                                    </td>
                                    <td>客户行业:</td>
                                    <td id="industry">
                                    
                                    </td>
                                </TR>
                                
                                <TR>
                                    <td>固定电话 :</td>
                                    <td>
                                    <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_phone"/>"
                                                            style="WIDTH: 180px" maxLength=50 name="cust_phone">
                                    </td>
                                    <td>移动电话 :</td>
                                    <td>
                                    <INPUT class=textbox id=sChannel2 value="<s:property value="#customer.cust_mobile"/>"
                                                            style="WIDTH: 180px" maxLength=50 name="cust_mobile">
                                    </td>
                                </TR>
                                <!-- 文件上传页面的三个要求
                                    1.表单必须是post提交
                                    2.表单提交类型必须为enctype,多段式提交。
                                    3.文件上传使用<input type="file"/>
                                    
                                 -->
                                <TR>
                                    <td>图片上传 :</td>
                                    <td>
                                     <input type="file" name="photo">
                                    </td>
                                    
                                </TR>
                                
                                <tr>
                                    <td rowspan=2>
                                    <INPUT class=button id=sButton2 type=submit
                                                            value="保存 " name=sButton2>
                                    </td>
                                </tr>
                            </TABLE>
                            
                            
                        </TD>
                        <TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg">
                        <IMG src="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
                <TBODY>
                    <TR>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg"
                            border=0></TD>
                        <TD align=middle width="100%"
                            background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD>
                        <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg"
                            border=0></TD>
                    </TR>
                </TBODY>
            </TABLE>
        </FORM>
    </BODY>
    </HTML>
    View Code

    5 list.jsp

    6 struts.xml

  • 相关阅读:
    面向对象基本原则
    策略模式
    简单工厂模式
    高内聚、低耦合
    UML在代码中的展现
    使用commons-csv简单读写CSV文件
    java反射机制
    SrpingDruid数据源加密数据库密码
    markdown学习经验
    Vue.js学习笔记
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8598244.html
Copyright © 2011-2022 走看看