⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addproductaction.java

📁 商店管理员程序 Java 实现对商品的管理
💻 JAVA
字号:
package cn.com.shopadmin;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;

public final class AddProductAction extends Action{  
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,  
		HttpServletResponse response) throws Exception {
	
   		DynaValidatorForm productForm = (DynaValidatorForm) form;         
		Integer id = (Integer)productForm.get("id");
		Integer sortid = (Integer)productForm.get("sortid");
		String name = (String)productForm.get("name");
		String price = (String)productForm.get("price");
		String saleprice = (String)productForm.get("saleprice");
		String descript = (String)productForm.get("descript");
		String contents = (String)productForm.get("contents");
		FormFile theFile =   (FormFile)productForm.get("theFile");
		/*
		  * 借助Hibernate完成与数据库相关的操作,插入商品记录
		 */
		DbOperate	 db=new DbOperate();
		Product product;
		if (id.intValue()==0) { //新插入商品
			product = new Product();
		}
		else{							//修改商品
			product = db.getProduct(id.intValue());
		}
		Sort sort = db.getSort(sortid.intValue());
		product.setSort(sort);
		product.setName(name);
		product.setPrice(Double.parseDouble(price));
		product.setSaleprice(Double.parseDouble(saleprice));
		product.setDescript(descript);
		product.setContents(contents);
		product.setSalecount(0);
		SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd");
		product.setSaledate( df.format(new Date()));
		/*
		 * 将上传文件保存到指定文件夹
		 */
		if (!theFile.getFileName().equals("")){
				try { 
						InputStream stream = theFile.getInputStream(); //把文件读入
						String filePath = request.getRealPath("/"); //ȡ��ǰϵͳ取当前系统路径
						filePath = filePath.substring(0,filePath.length()-1);
						int i = filePath.lastIndexOf("\\");
						System.out.println(i);
						System.out.println(filePath);
						filePath = filePath.substring(0,i);
						filePath = filePath + "\\ShoppingOnline\\images\\";
						OutputStream bos = new FileOutputStream( filePath +  theFile.getFileName()); //建立一个上传文件的输出流  
						int bytesRead = 0; 
						byte[] buffer = new byte[8192]; 
						while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) { 
							bos.write(buffer, 0, bytesRead); //将文件写入服务器 
						} 
						bos.close(); 
						stream.close(); 
				} catch (Exception e) { 
						System.err.print(e); 
				} 
				product.setImage("images/"+ theFile.getFileName());
		}

		db.saveOrUpdate(product);//保存商品对象
		/*
		 *转至保存成功提示页面
		 */
 		ActionMessages errors = new ActionMessages();
		errors.add(ActionMessages.GLOBAL_MESSAGE,
				new ActionMessage("label.saveOk"));
		if (!errors.isEmpty()) {
				saveErrors(request, errors);
		} 
  		return  mapping.findForward("toWrong");  //保存成功提示页面
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -