博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
案例52-crm练习新增客户中加入文件上传功能(struts2文件上传)
阅读量:4361 次
发布时间:2019-06-07

本文共 4224 字,大约阅读时间需要 14 分钟。

1 jsp/customer/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" %>添加客户 
当前位置:客户管理 > 添加客户
客户名称: 客户级别 :
信息来源 : 客户行业:
固定电话 : 移动电话 :
图片上传 :
View Code

2 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
{ private Customer customer = new Customer(); private CustomerService cs; private Integer currentPage; private Integer pageSize; //上传的文件会自动封装到File对象中 //在后台提供一个与前台
组件name相同的属性即可。 private File photo; //在提交的键名后加上固定的后缀FileName,文件的名称会自动封装到属性中。 private String photoFileName; //在提交的键名后加上固定的后缀ContentType,文件的MIME类型值会自动封装到属性中。 private String photoContentType; //获取客户列表 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"; } //保存客户 public String add() throws Exception { 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"; } @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

 

转载于:https://www.cnblogs.com/jepson6669/p/8598198.html

你可能感兴趣的文章
隐藏"站长统计"图标
查看>>
Oracle select 中case 的使用以及使用decode替换case
查看>>
创建一个dynamics 365 CRM online plugin (十二) - Asynchronous Plugins
查看>>
Eclipse 常用快捷键 (动画讲解)
查看>>
233 Matrix(矩阵快速幂+思维)
查看>>
Leetcode-Unique Binary Search Trees II
查看>>
Centos7系统下安装Docker
查看>>
PostgreSQL 序列(SEQUENCE)
查看>>
Missing Number
查看>>
Ionic3 demo TallyBook 实例3
查看>>
laravel服务容器
查看>>
Entity Framework的查询
查看>>
ZH奶酪:Python按行读取文件
查看>>
07-使用循环进行遍历数组(运算符)
查看>>
控件布局通用解决方案
查看>>
scala流程控制语句以及方法和函数
查看>>
MySQL的sql_mode模式
查看>>
windows命令——explorer
查看>>
<转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列...
查看>>
jquery和js cookie的使用解析
查看>>