📄 createproductaction.java
字号:
package j2eebbs.admin.action;
import j2eebbs.Constants;
import j2eebbs.DB;
import j2eebbs.Product;
import j2eebbs.admin.form.CreateProductForm;
import j2eebbs.common.FileUpLoad;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
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;
public final class CreateProductAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CreateProductForm productform = (CreateProductForm) form;
String name = productform.getName();
String introduce = productform.getIntroduce();
//String image = productform.getImage();
int price = Integer.parseInt(productform.getPrice());
int type = Integer.parseInt(productform.getType());
int endtime = Integer.parseInt(productform.getEndtime());
FormFile image = productform.getImage();
//保存文件
String path = getServlet().getServletContext().getRealPath("/upload");
String filePath = path + "/" + image.getFileName();
System.out.println("-----"+filePath);
FileUpLoad fileUpload = new FileUpLoad();
fileUpload.saveFile(image,filePath);
request.setAttribute("filePath",filePath);
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource) context
.getAttribute(Constants.DATASOURCE_KEY);
DB db = new DB(dataSource);
String PageForward;
ActionMessages errors = new ActionMessages();
Product product = new Product(name,introduce,filePath,type,endtime,price);
if(product.insert(db))
PageForward = "ToAdminIndex";
else{
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"error.accessDeny"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
PageForward = "ToAdminIndex";
}
db.close();
return mapping.findForward(PageForward);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -