1. 程式人生 > >案例52-crm練習新增客戶中加入文件上傳功能(struts2文件上傳)

案例52-crm練習新增客戶中加入文件上傳功能(struts2文件上傳)

練習 mon onclick open type() cor 自動 not 電話

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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html> <head> <TITLE>添加客戶</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(){ loadSelect(
"006","level","cust_level.dict_id"); //cust_level.dict_id是對象驅動的提交方式 loadSelect("002","source","cust_source.dict_id"); loadSelect("001","industry","cust_industry.dict_id"); }); </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"> <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; 添加客戶</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 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 style="WIDTH: 180px" maxLength=50 name="cust_phone"> </td> <td>移動電話 :</td> <td> <INPUT class=textbox id=sChannel2 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

技術分享圖片

技術分享圖片

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<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;
    
    
    //獲取客戶列表
    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

技術分享圖片

技術分享圖片

案例52-crm練習新增客戶中加入文件上傳功能(struts2文件上傳)