📄 itemaddaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.shopping.struts.action.item;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
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.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.shopping.dao.UnifyDao;
import com.shopping.factory.DAOFactory;
import com.shopping.struts.form.ItemAddForm;
import com.shopping.vo.Category;
import com.shopping.vo.Item;
public class ItemAddAction extends DispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UnifyDao itemDao = null;
try {
itemDao = (UnifyDao) DAOFactory
.getBean("com.shopping.dao.imp.ItemDaoImp");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
UnifyDao catDao = null;
try {
catDao = (UnifyDao) DAOFactory
.getBean("com.shopping.dao.imp.CatDaoImp");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String fileName = null;
ItemAddForm itemAddForm = (ItemAddForm) form;
FormFile file = itemAddForm.getFile();
if (file.getFileSize() != 0) {
FileOutputStream fos = null;
String path = "/images/item/";
try {
byte[] data = file.getFileData();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nowdate = sdf.format(new Date());
fileName = Long.parseLong(nowdate)
+ new Double(Math.random() * System.currentTimeMillis())
.toString().substring(3, 8) + ".jpg";
ServletContext application = this.getServlet()
.getServletContext();
String realPath = application.getRealPath(path);
fos = new FileOutputStream(realPath + "/" + fileName);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
double price = Double.parseDouble(itemAddForm.getPrice());
String name = itemAddForm.getItemName();
int storage = Integer.parseInt(itemAddForm.getStorage());
String desc = itemAddForm.getCont();
int pid = Integer.parseInt(itemAddForm.getChild());
Item item = new Item();
item.setDescr(desc);
item.setImageUrl("images/item/" + fileName);
item.setName(name);
item.setPdate(new Date());
item.setProStorage(storage);
item.setPrice(price);
item.setCategory((Category) catDao.getTableById(pid));
if (itemDao.insertTable(item)) {
request.setAttribute("info", "success");
return new ActionForward("/admin/itemAdd.jsp");
} else {
request.setAttribute("info", "wrong");
return new ActionForward("/admin/itemAdd.jsp");
}
}
public ActionForward modify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UnifyDao itemDao = null;
try {
itemDao = (UnifyDao) DAOFactory
.getBean("com.shopping.dao.imp.ItemDaoImp");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
UnifyDao catDao = null;
try {
catDao = (UnifyDao) DAOFactory
.getBean("com.shopping.dao.imp.CatDaoImp");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String fileName = null;
ItemAddForm itemAddForm = (ItemAddForm) form;
FormFile file = itemAddForm.getFile();
if (file.getFileSize() != 0) {
FileOutputStream fos = null;
String path = "/images/item/";
try {
byte[] data = file.getFileData();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nowdate = sdf.format(new Date());
fileName = Long.parseLong(nowdate)
+ new Double(Math.random() * System.currentTimeMillis())
.toString().substring(3, 8) + ".jpg";
ServletContext application = this.getServlet()
.getServletContext();
String realPath = application.getRealPath(path);
fos = new FileOutputStream(realPath + "/" + fileName);
fos.write(data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
int id = Integer.parseInt(itemAddForm.getId());
double price = Double.parseDouble(itemAddForm.getPrice());
String name = itemAddForm.getItemName();
int storage = Integer.parseInt(itemAddForm.getStorage());
String desc = itemAddForm.getCont();
int pid = Integer.parseInt(itemAddForm.getChild());
Item item = (Item)itemDao.getTableById(id);
item.setDescr(desc);
if (file.getFileSize() != 0) {
item.setImageUrl("images/item/" + fileName);
}
item.setName(name);
//item.setPdate(new Date());
item.setProStorage(storage);
item.setPrice(price);
item.setCategory((Category) catDao.getTableById(pid));
if (itemDao.updateTable(item)) {
request.setAttribute("info", "success");
return new ActionForward("/admin/item.jsp");
} else {
request.setAttribute("info", "wrong");
return new ActionForward("/admin/item.jsp");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -