📄 manageraction.java
字号:
package cn.com.tarena.ecport.web.action;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
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.actions.MappingDispatchAction;
import org.apache.struts.upload.FormFile;
import cn.com.tarena.ecport.biz.ICategoryBusiness;
import cn.com.tarena.ecport.biz.IProductBusiness;
import cn.com.tarena.ecport.biz.factory.BusinessFactory;
import cn.com.tarena.ecport.pojo.Product;
import cn.com.tarena.ecport.web.form.ManagerForm;
public class ManagerAction extends MappingDispatchAction {
/**
* 管理员登录
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws Exception
*/
public ActionForward doLogin(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
String name = request.getParameter("userid");
String pwd = request.getParameter("password");
if("admin".equals(name) && "123".equals(pwd)){
HttpSession session = request.getSession();
session.setAttribute("managerLogin", "2");
return mapping.findForward("ShopCartManager");
}else{
return mapping.findForward("login");
}
}
/**
* 添加产品
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws Exception
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public ActionForward add(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
ManagerForm managerForm = (ManagerForm)form;
Product product = new Product();
IProductBusiness ipb = (IProductBusiness)BusinessFactory.getBusiness(IProductBusiness.class.getName());
String productName = new String(managerForm.getName().getBytes("ISO-8859-1"),"GBK");
//判断数据库中是否有这个产品,若有的话,给予提示信息
if(!ipb.hasProduct(productName)){
product.setName(productName);
}else{
ActionErrors errors = new ActionErrors();
ActionMessage message = new ActionMessage("manager.error.sameName");
errors.add("sameName",message);
saveErrors(request, errors);
return mapping.findForward("add");
}
product.setAuthor(new String(managerForm.getAuthor().getBytes("ISO-8859-1"),"GBK"));
product.setBaseprice(Double.parseDouble(managerForm.getBaseprice()));
FormFile file = managerForm.getFile();//从ActionForm中取得FormFile对象
//得到保存文件的路径,此处保存再服务器端/images/product目录下
String path = getServlet().getServletContext().getRealPath("/images/product");
String filePath = path + "/" + file.getFileName();
saveFile(file,filePath);
String fileName = file.getFileName();
if(fileName != null){
product.setImages("images/product/"+fileName);
}
product.setPublish(new String(managerForm.getPublish().getBytes("ISO-8859-1"),"GBK"));
product.setDescription(new String(managerForm.getDescription().getBytes("ISO-8859-1"),"GBK"));
String pages = managerForm.getPages();
if(!("".equals(pages) || pages == null)){
product.setPages(Long.parseLong(managerForm.getPages()));
}
ICategoryBusiness icb = (ICategoryBusiness) BusinessFactory.getBusiness(ICategoryBusiness.class.getName());
product.setCategory(icb.getCategoryById(managerForm.getCategoryId()));
ipb.addProduct(product);
request.setAttribute("operation","add");//此处只是设置一个标志
return mapping.findForward("success");
}
/**
* 把上传的图片保存到服务器端
* @param file
* @param filePath
*/
private void saveFile(FormFile file, String filePath) {
InputStream is = null;
OutputStream os = null;
try {
// 在这里把文件名(filePath)保存到数据中
// 在以后显示图片时,只从数据库中读取文件名
is = file.getInputStream();
os = new FileOutputStream(filePath);
int readSize = 0;
byte buffer[] = new byte[1024];
while ((readSize = is.read(buffer, 0, 1024)) != -1) {
os.write(buffer, 0, readSize);
}
} catch (Exception e) {
System.out.println("保存文件异常:" + e);
} finally {
try {
if (is != null){
is.close();
}
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除一个产品
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
@SuppressWarnings("deprecation")
public ActionForward delete(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws IOException{
IProductBusiness ipb = (IProductBusiness)BusinessFactory.getBusiness(IProductBusiness.class.getName());
String productId = request.getParameter("productid");
ipb.deleteProduct(Long.parseLong(productId));
List<Product> list = ipb.findAllProducts();
HttpSession session = request.getSession();
session.setAttribute("list", list);
return mapping.findForward("productList");
}
/**
* 产品列表
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward list(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response){
IProductBusiness ipb = (IProductBusiness)BusinessFactory.getBusiness(IProductBusiness.class.getName());
List<Product> list = ipb.findAllProducts();
request.setAttribute("list", list);
return mapping.findForward("productInfo");
}
/**
* 把数据库中某个产品信息回显
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward modify(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response){
HttpSession session = request.getSession();
IProductBusiness ipb = (IProductBusiness) BusinessFactory.getBusiness(IProductBusiness.class.getName());
String productId = request.getParameter("productid");
Product product = ipb.getProductById(Long.parseLong(productId));
session.setAttribute("product", product);//把product对象放再session 中
return mapping.findForward("modifyProduct");
}
/**
* 修改产品信息并保存入数据库
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws Exception
*/
public ActionForward doModify(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
ManagerForm managerForm = (ManagerForm)form;
IProductBusiness ipb = (IProductBusiness)BusinessFactory.getBusiness(IProductBusiness.class.getName());
String productId = request.getParameter("productid");
Product product = ipb.getProductById(Long.parseLong(productId));
product.setAuthor(new String(managerForm.getAuthor().getBytes("ISO-8859-1"),"GBK"));
FormFile file = managerForm.getFile();
String path = getServlet().getServletContext().getRealPath("/images/product");
String filePath = path + "/" + file.getFileName();
saveFile(file,filePath);
product.setImages("images/product/"+file.getFileName());
product.setBaseprice(Double.parseDouble(managerForm.getBaseprice()));
product.setPublish(new String(managerForm.getPublish().getBytes("ISO-8859-1"),"GBK"));
String pages = managerForm.getPages();
if(!("".equals(pages) || pages == null)){
product.setPages(Long.parseLong(pages));
}else{
//如果用户没有输入page,则默认是0
product.setPages(0L);
}
product.setDescription(new String(managerForm.getDescription().getBytes("ISO-8859-1"),"GBK"));
ICategoryBusiness icb = (ICategoryBusiness) BusinessFactory.getBusiness(ICategoryBusiness.class.getName());
product.setCategory(icb.getCategoryById(managerForm.getCategoryId()));
ipb.updateProduct(product);
HttpSession session = request.getSession();
session.setAttribute("product", product);
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -