📄 productaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.tarena.shopcart.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorActionForm;
import com.tarena.shopcart.entity.Category;
import com.tarena.shopcart.entity.Product;
import com.tarena.shopcart.service.IProductService;
import com.tarena.shopcart.serviceImp1.ProductServiceImp1;
/**
* MyEclipse Struts Creation date: 11-01-2008
*
* XDoclet definition:
*
* @struts.action parameter="products" validate="true"
* @struts.action-forward name="success" path="/index.jsp"
*/
public class ProductAction extends MappingDispatchAction {
/*
* Generated Methods
*/
IProductService service = new ProductServiceImp1();
Product product = null;
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward products(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String page = request.getParameter("page");
request.setAttribute("pro", service.getPage(page));
List list =service.getProducts(page);
request.setAttribute("product", list);
return mapping.findForward("toIndex");
}
public ActionForward addProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorActionForm dv = (DynaValidatorActionForm) form;
FormFile file = (FormFile) dv.get("images");
String name = dv.getString("name");
String description = dv.getString("description");
String baseprice = dv.getString("baseprice");
String categoryid = dv.getString("category");
String author = dv.getString("author");
String publish = dv.getString("publish");
String pages = dv.getString("pages");
Product pro=new Product(name,description,Float.parseFloat(baseprice),author,publish,Long.parseLong(pages));
String servletContext = servlet.getServletContext().getRealPath("images/product/");
String plocation=servletContext+"/"+name+".jpg";
String location="images/product/"+name+".jpg";
pro.setImages(location);
System.out.println("images/product="+servletContext);
try {
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(plocation);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
} catch (Exception e) {
System.err.println();
}
Category category=service.findCategoryById(Integer.parseInt(categoryid));
pro.setCategory(category);
if(service.addProduct(pro)){
return mapping.findForward("addsuccess");
}
return mapping.findForward("fail");
}
public ActionForward categoryInit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List list = service.getAllCategory();
request.setAttribute("category", list);
return mapping.findForward("toAddProduct");
}
public ActionForward details(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String pid = request.getParameter("pid");
product = service.findProductById(Integer.parseInt(pid));
request.setAttribute("pro", product);
return mapping.findForward("productDetail");
}
public ActionForward searchProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
List list = service.getProductsLikeName(name);
request.setAttribute("product", list);
return mapping.findForward("toIndex");
}
public ActionForward deleteProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String pid = request.getParameter("pid");
product = service.findProductById(Integer.parseInt(pid));
String context=servlet.getServletContext().getRealPath("/");
String location=context+"/"+product.getImages();
new File(location).delete();
if(service.deleteProduct(Integer.parseInt(pid))){
return mapping.findForward("deletesuccess");
}
return mapping.findForward("fail");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -