CRM综合练习:客户管理-保存客户上传客户资质图片
文件上传回顾
什么是文件上传
- 将本地文件通过流的形式写到服务器上。
文件上传技术
- JspSmartUpload(很少用)
jspSmartUpload组件是应用JSP进行B/S程序开发过程中经常使用的上传下载组件,它使用简单,方便。现在我又为其加上了下载中文名字的文件的支持,真的是如虎添翼,必将赢得更多开发者的青睐。 - FileUpload
FileUpload 是 Apache commons下面的一个子项目,用来实现Java环境下面的文件上传功能,与常见的SmartUpload齐名。 - Servlet3.0
- 文件上传
- 注解开发
- 异步请求
- Struts2框架
- 底层实现采用FileUpload,对FileUpload进行封装
文件上传要素
- 表单的提交方式必须是POST
- 表单中需要提供
<input type="file" name="upload">
而且这个文件项必须要有name属性和值。 - 表单的enctype属性必须是
multipart/form-data
。
文件上传的代码实现
第一步:修改JSP页面(添加)
- 提供文件上传项
<TR>
<td>客户资质 :</td>
<td colspan="3">
<input type="file" name="upload"/>
</td>
</TR>
- 修改表单的enctype属性
第二步:修改Action中save方法
- Struts2 的文件上传
- 在Action中提供三个属性,对三个属性提供set方法
- 字符串类型 上传项名称+FileName;
- 文件类型 上传项名称
- 字符串类型 上传项名称+ContentType
- 在Action中提供三个属性,对三个属性提供set方法
/**
* 文件上传提供的三个属性:
*/
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;
}
- CustomerAction.java
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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
//使用EL表达式获取值栈的数据
$("#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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
//使用EL表达式获取值栈的数据
$("#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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
//使用EL表达式获取值栈的数据
$("#cust_industry option[value='${model.baseDictIndustry.dict_id}']").prop("selected","selected");
},"json");
});
</script>
修改edit.jsp中的提交路径
编写Action中的update的方法
/**
* 修改客户的方法:update
* @throws IOException
*/
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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"006"},function(data){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"001"},function(data){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
});
</script>
改写Action中的findAll方法
/**
* 分页查询客户的方法:findAll
*/
public String findAll(){
//接收参数:分页参数
//最好使用DetachedCriteria对象(条件查询--带分页)
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
//设置条件:(在web层来设置条件)
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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
$("#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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
$("#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){
//遍历json的数据:
$(data).each(function(i,n){
$("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
$("#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;
创建实体和映射
- 联系人的实体
- 联系人映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<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>
- 修改客户的实体
- 修改客户的映射
创建相关的类
- 创建Action
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;
/**
* 联系人的Action类
* @author lele
*
*/
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
//模型驱动使用的对象
private LinkMan linkMan = new LinkMan();
@Override
public LinkMan getModel() {
return linkMan;
}
//注入Service
private LinkManService linkManService;
public void setLinkManService(LinkManService linkManService) {
this.linkManService = linkManService;
}
}
- 创建Service
package com.lele.crm.service.impl;
import com.lele.crm.dao.LinkManDao;
import com.lele.crm.service.LinkManService;
/**
* 联系人的Service的实现类
* @author lele
*
*/
public class LinkManServiceImpl implements LinkManService {
private LinkManDao linkManDao;
public void setLinkManDao(LinkManDao linkManDao) {
this.linkManDao = linkManDao;
}
}
- 创建DAO
package com.lele.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.lele.crm.dao.LinkManDao;
/**
* 联系人DAO的实现类
* @author lele
*
*/
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;
/**
* 联系人的Action类
* @author lele
*
*/
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
//模型驱动使用的对象
private LinkMan linkMan = new LinkMan();
@Override
public LinkMan getModel() {
return linkMan;
}
//注入Service
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;
}
/**
* 查询联系人列表的Action
*/
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
在页面上回显数据