CRM综合练习:客户管理-保存客户上传客户资质图片
文件上传回顾
什么是文件上传
文件上传技术
JspSmartUpload(很少用) jspSmartUpload组件是应用JSP进行B/S程序开发过程中经常使用的上传下载组件,它使用简单,方便。现在我又为其加上了下载中文名字的文件的支持,真的是如虎添翼,必将赢得更多开发者的青睐。
FileUpload FileUpload 是 Apache commons下面的一个子项目,用来实现Java环境下面的文件上传功能,与常见的SmartUpload齐名。
Servlet3.0
Struts2框架
底层实现采用FileUpload,对FileUpload进行封装
文件上传要素
文件上传的代码实现
第一步:修改JSP页面(添加)
< TR>
< td> 客户资质 : td>
< td colspan = " 3" >
< input type = " file" name = " upload" />
td>
TR>
修改表单的enctype属性
第二步:修改Action中save方法
Struts2 的文件上传
在Action中提供三个属性,对三个属性提供set方法
字符串类型 上传项名称+FileName;
文件类型 上传项名称
字符串类型 上传项名称+ContentType
private String uploadFileName;
private File upload;
private String uploadContentType;
public void setUploadFileName ( String uploadFileName) {
this . uploadFileName = uploadFileName;
}
public void setUpload ( File upload) {
this . upload = upload;
}
public void setUploadContentType ( String uploadContentType) {
this . uploadContentType = uploadContentType;
}
public String save ( ) throws IOException{
if ( upload != null) {
String path = "C:/upload" ;
String uuidFileName = UploadUtils. getUuidFileName ( uploadFileName) ;
String realPath = UploadUtils. getPath ( uuidFileName) ;
String url = path+ realPath;
File file = new File ( url) ;
if ( ! file. exists ( ) ) {
file. mkdirs ( ) ;
}
File dictFile = new File ( url+ "/" + uuidFileName) ;
FileUtils. copyFile ( upload, dictFile) ;
}
customerService. save ( customer) ;
return NONE;
}
第三步:将文件上传路径存入到数据库中
第四步:设置拦截器(控制文件上传的大小、格式)
CRM综合练习:客户管理-删除客户
客户删除操作
第一步:修改列表页面上的链接地址
第二步:编写Action的delete方法
第三步:编写Service
编写DAO
CRM综合练习:客户管理-修改客户
客户的修改操作
修改列表页面路径
编写Action中的edit方法
在页面中回显数据
< script type= "text/javascript" >
$ ( function ( ) {
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "002" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_source" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_source option[value='${model.baseDictSource.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "006" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_level" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_level option[value='${model.baseDictLevel.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "001" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_industry" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_industry option[value='${model.baseDictIndustry.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
} ) ;
< / script>
修改edit.jsp中的提交路径
编写Action中的update的方法
public String update ( ) throws IOException{
if ( upload != null) {
String cust_image = customer. getCust_image ( ) ;
if ( cust_image != null || ! "" . equals ( cust_image) ) {
File file = new File ( cust_image) ;
file. delete ( ) ;
}
String path = "C:/upload" ;
String uuidFileName = UploadUtils. getUuidFileName ( uploadFileName) ;
String realPath = UploadUtils. getPath ( uuidFileName) ;
String url = path+ realPath;
File file = new File ( url) ;
if ( ! file. exists ( ) ) {
file. mkdirs ( ) ;
}
File dictFile = new File ( url+ "/" + uuidFileName) ;
FileUtils. copyFile ( upload, dictFile) ;
customer. setCust_image ( url+ "/" + uuidFileName) ;
}
customerService. update ( customer) ;
return "updateSuccess" ;
}
编写Service
编写DAO
CRM综合练习:客户管理-条件查询客户
客户管理的条件查询
在列表的页面上准备条件
提供表单元素
异步加载数据
< script type= "text/javascript" src= "${pageContext.request.contextPath }/js/jquery-1.11.3.min.js" > < / script>
< script type= "text/javascript" >
$ ( function ( ) {
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "002" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_source" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "006" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_level" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "001" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_industry" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
} , "json" ) ;
} ) ;
< / script>
改写Action中的findAll方法
public String findAll ( ) {
DetachedCriteria detachedCriteria = DetachedCriteria. forClass ( Customer. class ) ;
if ( customer. getCust_name ( ) != null) {
detachedCriteria. add ( Restrictions. like ( "cust_name" , "%" + customer. getCust_name ( ) + "%" ) ) ;
}
if ( customer. getBaseDictSource ( ) != null) {
if ( customer. getBaseDictSource ( ) . getDict_id ( ) != null && ! "" . equals ( customer. getBaseDictSource ( ) . getDict_id ( ) ) ) {
detachedCriteria. add ( Restrictions. eq ( "baseDictSource.dict_id" , customer. getBaseDictSource ( ) . getDict_id ( ) ) ) ;
}
}
if ( customer. getBaseDictLevel ( ) != null) {
if ( customer. getBaseDictLevel ( ) . getDict_id ( ) != null && ! "" . equals ( customer. getBaseDictLevel ( ) . getDict_id ( ) ) ) {
detachedCriteria. add ( Restrictions. eq ( "baseDictLevel.dict_id" , customer. getBaseDictLevel ( ) . getDict_id ( ) ) ) ;
}
}
if ( customer. getBaseDictIndustry ( ) != null) {
if ( customer. getBaseDictIndustry ( ) . getDict_id ( ) != null && ! "" . equals ( customer. getBaseDictIndustry ( ) . getDict_id ( ) ) ) {
detachedCriteria. add ( Restrictions. eq ( "baseDictIndustry.dict_id" , customer. getBaseDictIndustry ( ) . getDict_id ( ) ) ) ;
}
}
PageBean< Customer> pageBean = customerService. findByPage ( detachedCriteria, currPage, pageSize) ;
ActionContext. getContext ( ) . getValueStack ( ) . push ( pageBean) ;
return "findAll" ;
}
在条件上回显数据
$ ( function ( ) {
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "002" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_source" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_source option[value='${model.baseDictSource.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "006" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_level" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_level option[value='${model.baseDictLevel.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
$. post ( "${pageContext.request.contextPath }/baseDict_findByTypeCode.action" , { "dict_type_code" : "001" } , function ( data) {
$ ( data) . each ( function ( i, n) {
$ ( "#cust_industry" ) . append ( "" + n. dict_item_name+ "" ) ;
} ) ;
$ ( "#cust_industry option[value='${model.baseDictIndustry.dict_id}']" ) . prop ( "selected" , "selected" ) ;
} , "json" ) ;
} ) ;
CRM综合练习:联系人管理-查询列表的显示
联系人准备工作
创建表
CREATE TABLE ` cst_linkman` (
` lkm_id` bigint ( 32 ) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)' ,
` lkm_name` varchar ( 16 ) DEFAULT NULL COMMENT '联系人姓名' ,
` lkm_cust_id` bigint ( 32 ) NOT NULL COMMENT '客户id' ,
` lkm_gender` char ( 1 ) DEFAULT NULL COMMENT '联系人性别' ,
` lkm_phone` varchar ( 16 ) DEFAULT NULL COMMENT '联系人办公电话' ,
` lkm_mobile` varchar ( 16 ) DEFAULT NULL COMMENT '联系人手机' ,
` lkm_email` varchar ( 64 ) DEFAULT NULL COMMENT '联系人邮箱' ,
` lkm_qq` varchar ( 16 ) DEFAULT NULL COMMENT '联系人qq' ,
` lkm_position` varchar ( 16 ) DEFAULT NULL COMMENT '联系人职位' ,
` lkm_memo` varchar ( 512 ) DEFAULT NULL COMMENT '联系人备注' ,
PRIMARY KEY ( ` lkm_id` ) ,
KEY ` FK_cst_linkman_lkm_cust_id` ( ` lkm_cust_id` ) ,
CONSTRAINT ` FK_cst_linkman_lkm_cust_id` FOREIGN KEY ( ` lkm_cust_id` ) REFERENCES ` cst_customer` ( ` cust_id` ) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8;
创建实体和映射
联系人的实体
联系人映射
< hibernate-mapping>
< class name = " com.lele.crm.domain.LinkMan" table = " cst_linkman" >
< id name = " lkm_id" column = " lkm_id" >
< generator class = " native" />
id>
< property name = " lkm_name" column = " lkm_name" />
< property name = " lkm_gender" column = " lkm_gender" />
< property name = " lkm_phone" column = " lkm_phone" />
< property name = " lkm_mobile" column = " lkm_mobile" />
< property name = " lkm_email" column = " lkm_email" />
< property name = " lkm_qq" column = " lkm_qq" />
< property name = " lkm_position" column = " lkm_position" />
< property name = " lkm_memo" column = " lkm_memo" />
< many-to-one name = " customer" class = " com.lele.crm.domain.Customer" column = " lkm_cust_id" />
class>
hibernate-mapping>
修改客户的实体
修改客户的映射
创建相关的类
package com. lele. crm. web. action;
import com. lele. crm. domain. LinkMan;
import com. lele. crm. service. LinkManService;
import com. opensymphony. xwork2. ActionSupport;
import com. opensymphony. xwork2. ModelDriven;
public class LinkManAction extends ActionSupport implements ModelDriven < LinkMan> {
private LinkMan linkMan = new LinkMan ( ) ;
@Override
public LinkMan getModel ( ) {
return linkMan;
}
private LinkManService linkManService;
public void setLinkManService ( LinkManService linkManService) {
this . linkManService = linkManService;
}
}
package com. lele. crm. service. impl;
import com. lele. crm. dao. LinkManDao;
import com. lele. crm. service. LinkManService;
public class LinkManServiceImpl implements LinkManService {
private LinkManDao linkManDao;
public void setLinkManDao ( LinkManDao linkManDao) {
this . linkManDao = linkManDao;
}
}
package com. lele. crm. dao. impl;
import org. springframework. orm. hibernate5. support. HibernateDaoSupport;
import com. lele. crm. dao. LinkManDao;
public class LinkManDaoImpl extends HibernateDaoSupport implements LinkManDao {
}
完成相关配置
查询联系人的列表
修改menu.jsp的链接
编写Action
package com. lele. crm. web. action;
import org. hibernate. criterion. DetachedCriteria;
import com. lele. crm. domain. LinkMan;
import com. lele. crm. domain. PageBean;
import com. lele. crm. service. LinkManService;
import com. opensymphony. xwork2. ActionContext;
import com. opensymphony. xwork2. ActionSupport;
import com. opensymphony. xwork2. ModelDriven;
public class LinkManAction extends ActionSupport implements ModelDriven < LinkMan> {
private LinkMan linkMan = new LinkMan ( ) ;
@Override
public LinkMan getModel ( ) {
return linkMan;
}
private LinkManService linkManService;
public void setLinkManService ( LinkManService linkManService) {
this . linkManService = linkManService;
}
private Integer currPage = 1 ;
private Integer pageSize = 3 ;
public void setCurrPage ( Integer currPage) {
if ( currPage == null) {
currPage = 1 ;
}
this . currPage = currPage;
}
public void setPageSize ( Integer pageSize) {
if ( pageSize == null) {
pageSize = 3 ;
}
this . pageSize = pageSize;
}
public String findAll ( ) {
DetachedCriteria detachedCriteria = DetachedCriteria. forClass ( LinkMan. class ) ;
PageBean< LinkMan> pageBean = linkManService. findAll ( detachedCriteria, currPage, pageSize) ;
ActionContext. getContext ( ) . getValueStack ( ) . push ( pageBean) ;
return "findAll" ;
}
}
编写Service
@Override
public PageBean< LinkMan> findAll ( DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
PageBean< LinkMan> pageBean = new PageBean < LinkMan> ( ) ;
pageBean. setCurrPage ( currPage) ;
pageBean. setPageSize ( pageSize) ;
Integer totalCount = linkManDao. findCount ( detachedCriteria) ;
pageBean. setTotalCount ( totalCount) ;
double tc = totalCount;
Double num = Math. ceil ( tc / pageSize) ;
pageBean. setTotalPage ( num. intValue ( ) ) ;
Integer begin = ( currPage - 1 ) * pageSize;
List< LinkMan> list = linkManDao. findByPage ( detachedCriteria, begin, pageSize) ;
pageBean. setList ( list) ;
return pageBean;
}
编写DAO
在页面上回显数据