?? addproductaction.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完成與數(shù)據(jù)庫相關(guān)的操作,插入商品記錄
*/
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("/"); //??????取當(dāng)前系統(tǒng)路徑
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); //將文件寫入服務(wù)器
}
bos.close();
stream.close();
} catch (Exception e) {
System.err.print(e);
}
product.setImage("images/"+ theFile.getFileName());
}
db.saveOrUpdate(product);//保存商品對象
/*
*轉(zhuǎn)至保存成功提示頁面
*/
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.saveOk"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong"); //保存成功提示頁面
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -