📄 itemactions.java
字号:
}
/**
* This is the action called from the Struts framework to search for items in
* the mall matching the specified keyword
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward searchResults(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ActionForward forward = mapping.findForward("searchResults");
try {
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
TreeMap items =
shopBean.findItems(
(String) request.getParameter("keyword"),
(String) request.getSession().getAttribute("shopID"),
request.getLocale().getLanguage());
request.setAttribute("items", items);
} catch(ItemException ex) {
servlet.log("SearchItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("SearchItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
/**
* This is the action called from the Struts framework to get information
* about an item
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward editItemForm(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ActionForward forward = mapping.findForward("editItemForm");
try {
String langID = request.getLocale().getLanguage();
String itemID = (String) request.getParameter("itemID");
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
Item item = shopBean.getItemDetails(itemID, langID);
request.setAttribute("item", new ItemForm(item));
request.setAttribute("image", item.getImage());
SubCategory[] subCategories =
shopBean.getSubCategories(
(String) request.getSession().getAttribute("shopID"),
langID);
request.setAttribute("subcategories", subCategories);
} catch(ItemException ex) {
servlet.log("EditItemFormError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("EditItemFormError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
/**
* This is the action called from the Struts framework to update an item's
* information
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward editItem(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ItemForm item = (ItemForm) form;
ActionForward forward = mapping.findForward("success");
try {
String langID = request.getLocale().getLanguage();
String shopID =
(String) request.getSession().getAttribute("shopID");
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
// Upload the image associated with the item, if specified
FormFile imageFile = item.getImage();
String imageFilePath = imageFile.getFileName();
if(imageFilePath != null && !"".equals(imageFilePath)) {
String uploadDir =
servlet.getServletConfig().getInitParameter("UploadDir");
java.net.URL url =
servlet.getServletContext().getResource(uploadDir);
FileOutputStream out =
new FileOutputStream(url.getFile() + File.separator +
imageFile.getFileName());
out.write(imageFile.getFileData());
} else {
// If no image is specified, use the old image
Item itemDetails =
shopBean.getItemDetails(request.getParameter("itemID"), langID);
imageFilePath = itemDetails.getImage();
}
// Get all user parameters from the request
Enumeration names = request.getParameterNames();
String paramName = null;
Map attributes = new HashMap();
/* Retrieve the list of attribute labels and attribute names from the request
object */
while(names.hasMoreElements()) {
paramName = (String) names.nextElement();
if(
!paramName.equalsIgnoreCase("itemID") &&
!paramName.equalsIgnoreCase("command") &&
!paramName.equalsIgnoreCase("name") &&
!paramName.equalsIgnoreCase("subCatID") &&
!paramName.equalsIgnoreCase("unitPrice") &&
!paramName.equalsIgnoreCase("quantity") &&
!paramName.equalsIgnoreCase("reorderLevel") &&
!paramName.equalsIgnoreCase("reorderQty") &&
!paramName.equalsIgnoreCase("desc")) {
attributes.put(paramName, request.getParameter(paramName));
}
}
shopBean.updateItem(new Item(
request.getParameter("itemID"),
item.getName(), item.getDesc(),
item.getSubCatID(), shopID,
Double.parseDouble(item.getUnitPrice()),
Integer.parseInt(item.getQuantity()),
Integer.parseInt(item.getReorderLevel()),
Integer.parseInt(item.getReorderQty()),
imageFilePath, langID, attributes));
request.setAttribute(
"message",
MessageCache.getMessage(
"message.edititem.success",
getLocale(request)));
} catch(ItemException ex) {
servlet.log("EditItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("EditItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
/**
* This is the action called from the Struts framework to delete an item from
* a shop
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward deleteItem(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ActionForward forward = mapping.findForward("success");
try {
String shopID = (String) request.getSession().getAttribute("shopID");
String itemID = (String) request.getParameter("itemID");
if(itemID != null && shopID != null) {
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
shopBean.deleteItem(itemID, shopID);
request.setAttribute(
"message",
MessageCache.getMessage(
"message.deleteitem.success",
getLocale(request)));
}
} catch(ItemException ex) {
servlet.log("DeleteItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("DeleteItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -