浏览 3197 次
锁定老帖子 主题:struts标签实现图片上传
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2009-10-16
最后修改:2009-10-16
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ page language="java" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib prefix= "fmt" uri= "http://java.sun.com/jstl/fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <%@ taglib prefix="sd" uri="/struts-dojo-tags"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>发布产品信息</title> <link rel="stylesheet" type="text/css" href="../style/main.css"/> <s:head theme="xhtml"/> <sd:head parseContent="true" extraLocales="utf-8" /> </head> <script type="text/javascript"> </script> <body> <!-- <div style="color:red"> <s:fielderror></s:fielderror> </div> --> <s:form action="addProduct" method="post" theme="simple" enctype="multipart/form-data"> <div class="mainbox" style=" height : 848px;"> <div class="morearea"> <a href="<%=request.getContextPath()%>/product/addProduct.action">发布产品信息</a> </div> <h1>发布产品信息</h1> <table border="0" cellspacing="0" cellpadding="0" class="table" bordercolor="#ccc"> <tr> <td width="20%" align="center" bgcolor="#F6F6F6">名称</td> <td width="80%"> <s:textfield id="cfProduct.proTitle" name="cfProduct.proTitle" size="50"></s:textfield> </td> </tr> <tr> <td width="20%" align="center" bgcolor="#F6F6F6">型号</td> <td width="80%"> <s:textfield id="cfProduct.proModel" name="cfProduct.proModel" size="50"></s:textfield> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">规格</td> <td> <s:textfield id="cfProduct.proSpec" name="cfProduct.proSpec" size="50"></s:textfield> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">品牌</td> <td> <s:textfield id="cfProduct.proTrademark" name="cfProduct.proTrademark" size="50"></s:textfield> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">材质</td> <td> <s:textfield id="cfProduct.proMaterial" name="cfProduct.proMaterial" size="50"></s:textfield> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">产地</td> <td> <s:textfield id="cfProduct.proOrigin" name="cfProduct.proOrigin" size="40"></s:textfield> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">类别</td> <td> <s:select headerKey="noSelectClass" headerValue="--请选择类型--" name="cfProduct.proClassId" list="productclassList" id="cfProduct.proClassId" listKey="psId" listValue="psName" key="cfProduct.proClassId" /> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">图片</td> <td> <s:textfield name ="caption" label ="图片名称" /> <s:label value="图片名称"></s:label> <s:file name ="myFile" label ="浏览"/> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">导读</td> <td> <s:textarea id="cfProduct.proNaviContent" name="cfProduct.proNaviContent" cols="80" rows="5"></s:textarea> <font color="#ff0000">*</font> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">内容</td> <td> <s:textarea id="cfProduct.proContent" name="cfProduct.proContent" cols="80" rows="5"></s:textarea> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">有效期</td> <td> 开始时间: <sd:datetimepicker language="utf-8" name="cfProduct.proBeginTime" value="today" displayFormat="yyyy-MM-dd"></sd:datetimepicker> 结束时间: <sd:datetimepicker language="utf-8" name="cfProduct.proEndTime" value="today" displayFormat="yyyy-MM-dd"></sd:datetimepicker> </td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">价格</td> <td> <s:textfield id="cfProduct.proPrice" name="cfProduct.proPrice" size="20"></s:textfield> </td> </tr> </table> <!-- <table border="0" cellspacing="0" cellpadding="0" class="table" bordercolor="#ccc"> <tr> <th colspan="2" align="center" bgcolor="#F6F6F6">联系方式的确认与修改 若有误,您发布的信息将无法通过审核!<a href="account_eidt.html" target="mainFrame">[修改资料]</a></th> </tr> <tr> <td width="20%" align="center" bgcolor="#F6F6F6">姓名</td> <td width="80%"><s:text name="cfCorporation.corUserName"></s:text></td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">公司名称</td> <td><s:text name="cfCorporation.corName"></s:text></td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">经营模式</td> <td><s:text name="cfCorporation.corType"></s:text></td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">固定电话</td> <td><s:text name="cfCorporation.corContactsPhone"></s:text></td> </tr> <tr> <td align="center" bgcolor="#f6f6f6">传真号码</td> <td><s:text name="cfCorporation.corFax"></s:text></td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">移动电话</td> <td><s:text name="cfCorporation.corContactsMobile"></s:text></td> </tr> <tr> <td align="center" bgcolor="#F6F6F6">电子邮件</td> <td><s:text name="cfCorporation.corContactsEmail"></s:text></td> </tr> </table> --> <p class="tc"><s:submit id="confirmButton" type="submit" action="addProduct" value="发布产品"></s:submit></p> </div> </s:form> </body> </html> (2)struts配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="product" extends="struts-default"> <action name="toAddProduct" class="productAction" method="toAddProduct"> <result>/product/product_add.jsp</result> </action> <action name="addProduct" class="productAction" method="addProduct"> <!-- <interceptor-ref name ="fileUpload" > <param name ="allowedTypes" > image/bmp,image/png,image/gif,image/jpeg,image/jpg </param > </interceptor-ref > <interceptor-ref name ="defaultStack" /> --> <result name="success" type="redirect">product/productList.action</result> <result name="input">/product/product_add.jsp</result> </action> <action name="productList" class="productAction" method="listProducts"> <result>/product/product_list.jsp</result> </action> <action name="toUpdateProduct" class="productAction" method="toUpdateProduct"> <result name="success">/product/product_updata.jsp</result> </action> <action name="updateProduct" class="productAction" method="updateProduct"> <!-- <interceptor-ref name ="fileUpload" > <param name ="allowedTypes" > image/bmp,image/png,image/gif,image/jpeg,image/jpg </param > </interceptor-ref > <interceptor-ref name ="defaultStack" /> --> <result name="success" type="redirect">product/productList.action</result> <result name="input">/product/product_updata.jsp</result> </action> <action name="startProduct" class="productAction" method="startProduct"> <result name="success" type="redirect">product/productList.action</result> <result name="input">/product/product_list.jsp</result> </action> <action name="stopProduct" class="productAction" method="stopProduct"> <result name="success" type="redirect">product/productList.action</result> <result name="input">/product/product_list.jsp</result> </action> <action name="toAppointPage" class="productAction" method="toAppointPage"> <result name="success">/product/product_list.jsp</result> </action> <action name="deleteProduct" class="productAction" method="deleteProduct"> <result name="success" type="redirect">product/productList.action</result> <result name="input">/product/product_list.jsp</result> </action> </package> </struts> (3)Action类 /** * <p>ProductAction</p> * * 版权 (c) 2009 * * <p>CFSW</p> * * 文件历史 * 日期 作者 描述 * 2009-10-09 xiangcheng.yue 创建 * */ package product.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import java.util.List; import org.apache.struts2.ServletActionContext; import product.beans.CfProduct; import product.beans.CfProductclass; import product.service.ProductService; import util.IdGenerator; import com.opensymphony.xwork2.ActionSupport; import common.beans.Page; import common.util.ConstantUtil; import common.util.PageUtil; /** * 产品的查看,发布,修改,删除的Action. * * @author xiangcheng.yue * @version 1.0 */ public class ProductAction extends ActionSupport { private static final long serialVersionUID = 1L; private static final int BUFFER_SIZE = 160 * 1024; /** 产品Service。*/ private ProductService productService; /** 产品对象。*/ private CfProduct cfProduct; /** 企业对象。 */ //private CfCorporation cfCorporation; /** 产品对象List。*/ private List<CfProduct> cfProducts; /** 产品Id。*/ private String proId; /** 产品修改页面是否修改产品图片标志,"0"表示没有修改,"1"表示修改。 */ private String updatePicFlag = "0"; /** 产品发布及修改页面是否修改企业信息的标志,"0"表示没有修改,"1"表示修改。 */ //private String updateCorporationFlag = "0"; /** 产品类型List。 */ private List<CfProductclass> productclassList; /** 图片对象,为实现产品图片的上传及显示。 */ //private Picture pic = new Picture(); /** 分页对象,为实现产品List的分页。 */ private Page page = new Page(); /** 跳转页面。 */ private int goPage; /** 为实现产品图片的上传及显示。 */ private File myFile; private String contentType; private String fileName; private String imageFileName; private String caption; private static void copy(File src, File dst) { try { InputStream in = null ; OutputStream out = null ; try { in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE); byte [] buffer = new byte [BUFFER_SIZE]; while (in.read(buffer) > 0 ) { out.write(buffer); } } finally { if ( null != in) { in.close(); } if ( null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } public String getExtention(String fileName) { int pos = fileName.lastIndexOf("."); return fileName.substring(pos); } /** * @since 2009-10-09 * 初始化产品List页面,并按照条件对List进行分页显示。 * @return String SUCCESS 显示产品List页面 */ public String listProducts() { cfProducts = productService.findAll(); page.setEveryPage(ConstantUtil.EVERYPAGE_SIZE); page = PageUtil.createPage(page,cfProducts.size()); goPage = 1; cfProducts = productService.getListByPage(CfProduct.class,page); return SUCCESS; } /** * @since 2009-10-09 * 确定要发布产品信息后,对新发布的产品的进行保存。 * @return String SUCCESS 返回产品List页面 */ public String toAddProduct() { cfProduct = new CfProduct(); productclassList = productService.findSortOfCfProductsort(); return SUCCESS; } /** * @since 2009-10-09 * 确定要发布产品信息后,对新发布的产品的进行保存。 * @return String SUCCESS 返回产品List页面 */ public String addProduct() { cfProduct.setProId(IdGenerator.createId()); cfProduct.setProCreatTime(new Date()); imageFileName = new Date().getTime() + getExtention(fileName); String picURl = ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "\\" + imageFileName; File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "/" + imageFileName); copy(myFile, imageFile); cfProduct.setProPicUrl(picURl); productService.saveProduct(cfProduct); return SUCCESS; } /** * @since 2009-10-09 * 确定要修改产品信息后,初始化要修改的产品对象。 * @return String SUCCESS 跳转到产品修改页面 */ @SuppressWarnings("unchecked") public String toUpdateProduct() { cfProduct = productService.findById(this.getProId()); productclassList = productService.findSortOfCfProductsort(); //cfCorporation = productService.findCfCorporationById(cfProduct.getProCorId()); return SUCCESS; } /** * @since 2009-10-09 * 修改产品信息后,对修改了的产品的信息进行保存。 * @return String SUCCESS 跳转到产品List页面,并对该页面进行刷新。 */ public String updateProduct() { if("0" == updatePicFlag){ productService.saveProduct(cfProduct); return SUCCESS; } productService.updateProduct(cfProduct); return SUCCESS; } /** * @since 2009-10-09 * 确定启用产品时,对产品的是否停用标志位进行修改。 * @return String SUCCESS 刷新List页面。 */ public String startProduct() { cfProduct = productService.findById(this.getProId()); cfProduct.setProIsLock(1); productService.updateProduct(cfProduct); return SUCCESS; } /** * @since 2009-10-09 * 确定停用产品时,对产品的是否停用标志位进行修改。 * @return String SUCCESS 刷新List页面。 */ public String stopProduct() { cfProduct = productService.findById(this.getProId()); cfProduct.setProIsLock(0); productService.updateProduct(cfProduct); return SUCCESS; } /** * @since 2009-10-09 * 点击分页标签时到确定页。 * @return String SUCCESS 指定确定页的数据,并刷新List页面。 */ public String toAppointPage(){ page.setCurrentPage(this.goPage); page = PageUtil.createPage(page); cfProducts = productService.getListByPage(CfProduct.class,page); return SUCCESS; } /** * @since 2009-10-09 * 点击产品List页面,删除链接。 * @return String SUCCESS 把要删除的数据删除标志位设为1,并刷新List页面。 */ public String deleteProduct(){ productService.deleteProduct(this.getProId()); return SUCCESS; } public void setCfProduct(CfProduct cfProduct) { this.cfProduct = cfProduct; } public CfProduct getCfProduct() { return cfProduct; } public List<CfProduct> getCfProducts() { return cfProducts; } public void setCfProducts(List<CfProduct> cfProducts) { this.cfProducts = cfProducts; } public ProductService getProductService() { return productService; } public void setProductService(ProductService productService) { this.productService = productService; } public String getProId() { return proId; } public void setProId(String proId) { this.proId = proId; } public static long getSerialVersionUID() { return serialVersionUID; } public String getUpdatePicFlag() { return updatePicFlag; } public void setUpdatePicFlag(String updatePicFlag) { this.updatePicFlag = updatePicFlag; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } public List<CfProductclass> getProductclassList() { return productclassList; } public void setProductclassList(List<CfProductclass> productclassList) { this.productclassList = productclassList; } public int getGoPage() { return goPage; } public void setGoPage(int goPage) { this.goPage = goPage; } public void setMyFileContentType(String contentType) { this.contentType = contentType; } public void setMyFileFileName(String fileName) { this.fileName = fileName; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getImageFileName() { return imageFileName; } public String getCaption() { return caption; } public void setCaption(String caption) { this.caption = caption; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } } 因为时间关系没怎么仔细的分离代码,说明文字也很少,需要参考的请仔细的查看一下。 谢谢你们,有疑问和建议请直接提。想批评直接说,但不要进行人身攻击,呵呵。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |