客户管理---保存客户上传客户资质图片
1、修改表单页面
- 提供文件传项
- 修改表单的enctype属性
2、修改Action中的save方法
- Struts的文件上传
- 在Action中提供三个属性,对三个属性提供set方法
- 字符串类型 上传项名称+fileName
- 文件类型 上传项名称
- 字符串类型 上传项名称+ConentType
- 在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;
}
- 修改save方法
public String save() throws IOException {
if(upload!=null) {
//文件上传
//设置文件上传路径
String path = "D:/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;
}
3、将文件上传路径存入到数据库中
- 修改实体
- 修改映射
- 修改文件上传的代码
public String save() throws IOException {
if(upload!=null) {
//文件上传
//设置文件上传路径
String path = "D:/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.save(customer);
return "saveSuccess";
}
4、设置拦截器(控制文件上传大小、格式)
<action name="customer_*" class="com.ithou.crm.web.action.CustomerAction" method="{1}">
<result name="saveUI">/jsp/customer/add.jsp</result>
<result name="findAll">/jsp/customer/list.jsp</result>
<result name="saveSuccess" type="redirectAction">customer_findAll.action</result>
<result name="input">/jsp/customer/add.jsp</result>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2097152</param>
<param name="fileUpload.allowedExtensions">.jpg</param>
</interceptor-ref>
</action>
客户管理---删除客户
1、修改列表页面上的链接地址
2、编写Action的delete方法
public String delete() {
customer = customerService.findById(customer.getCust_id());
//删除图片
String cust_image = customer.getCust_image();
if(cust_image != null && !"".equals(cust_image)) {
File file = new File(cust_image);
if(file.exists()) {
file.delete();
}
}
customerService.delete(customer);
return "deleteSuccess";
}
3、编写Service
@Override
public void delete(Customer customer) {
customerDao.delete(customer);
}
@Override
public Customer findById(long cust_id) {
return customerDao.findById(cust_id);
}
4、编写Dao
@Override
public void delete(Customer customer) {
this.getHibernateTemplate().delete(customer);
}
@Override
public Customer findById(Long cust_id) {
return this.getHibernateTemplate().get(Customer.class, cust_id);
}
客户管理---修改客户
1、修改客户列表页面路径
2、编写Action中的edit方法
public String edit() {
customer = customerService.findById(customer.getCust_id());
/*将customer传递到页面:两种方式
一种:手动压栈;二种:因为模型驱动的对象,默认在栈顶
如果使用第一种方式:回显数据:<s:property value="cust_name"/> <s:name="cust_name" value="">
如果使用第二种方式:回显数据:<s:property value="%{model.cust_name}"/>*/
//ActionContext.getContext().getValueStack().push(customer);
return "editSuccess";
}
3、在页面中回显数据
- 下拉列表的回显
<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("<option value='"+n.dict_id+"'>"+ n.dict_item_name+ "</option>");
});
$("#cust_source option=[value='${model.baseDictSource.dict_id}']").prop("selected","selected");
}, "json");
});
$(function() {
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code" : "006"}, function(data) {
$(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");
});
$(function() {
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code" : "001"}, function(data) {
$(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");
});
</script>
4、修改edit.jsp中的提交路径
5、编写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);
if(file.exists()) {
file.delete();
}
}
String path = "D:/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";
}
6、编写Service
@Override
public void update(Customer customer) {
customerDao.update(customer);
}
7、编写Dao
@Override
public void update(Customer customer) {
this.getHibernateTemplate().update(customer);
}
客户管理---条件查询客户
1、列表页面准备
- 提供表单元素
- 异步加载数据
<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("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
});
$(function(){
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"001"},function(data){
$(data).each(function(i,n){
$("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
});
$(function() {
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action", { "dict_type_code" : "006" }, function(data) {
$(data).each(function(i, n){
$("#cust_level").append( "<option value='"+n.dict_id+"'>"+ n.dict_item_name+"</option>");
});
}, "json");
});
</script>
2、改写Action中的findAll方法
public String findAll() {
// 接收参数:分页参数
// 封装离线条件查询对象,使用DetachedCriteria对象(条件查询--带分页)
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
if(customer.getCust_name()!=null && !"".equals(customer.getCust_name())) {
detachedCriteria.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
}
if(customer.getBaseDictSource()!=null ) {
if(customer.getBaseDictSource().getDict_id()!=null && !"".equals(customer.getBaseDictSource().getDict_id())) {
System.out.println(customer.getBaseDictSource().getDict_item_name());
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);
//将pageBean的数据存入到值栈
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
3、在条件上回显数据
<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("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
$("#cust_source option[value='${model.baseDictSource.dict_id}']").prop("selected","selected");
},"json");
});
$(function(){
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"001"},function(data){
$(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");
});
$(function() {
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action", { "dict_type_code" : "006" }, function(data) {
$(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");
});
</script>