📄 addbookaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.dongfang.action;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
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.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import com.dongfang.dao.BookDAO;
import com.dongfang.dao.SortDAO;
import com.dongfang.po.Book;
import com.dongfang.po.Sort;
/**
* MyEclipse Struts
* Creation date: 09-10-2007
*
* XDoclet definition:
* @struts.action input="/wrong.jsp" validate="true"
*/
public class AddbookAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
DynaActionForm bookForm = (DynaActionForm)form;
Integer id = (Integer)bookForm.get("id");
Integer sortId = (Integer)bookForm.get("sortId");
String bookname = (String)bookForm.get("bookname");
//System.out.println(bookname);
String price = (String)bookForm.get("price");
String saleprice = (String)bookForm.get("saleprice");
//System.out.println(saleprice);
String descript = (String)bookForm.get("descript");
String contents = (String)bookForm.get("contents");
String writer = (String)bookForm.get("writer");
String publish = (String)bookForm.get("publish");
FormFile theFile = (FormFile)bookForm.get("theFile");
Book book =null;
BookDAO bookdao = new BookDAO();
if(id.intValue()==0)
{
book = new Book();
book.setId(bookdao.getNextId());
}
else
{
book = bookdao.getBookById(id.intValue());
}
SortDAO sortDAO = new SortDAO();
Sort sort = sortDAO.getSortById(sortId.intValue());
book.setSort(sort);
book.setName(bookname);
book.setPrice(Double.parseDouble(price));
book.setSaleprice(Double.parseDouble(saleprice));
book.setDescript(descript);
book.setContents(contents);
book.setWriter(writer);
book.setPublish(publish);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
book.setSaledate(sf.format(new Date()));
if(!theFile.getFileName().equals(""))
{
try {
InputStream is = theFile.getInputStream();
String filePath = this.getServlet().getServletContext().getRealPath("/");
int i = filePath.lastIndexOf("\\");
filePath = filePath.substring(0, i-18);
System.out.println(filePath);
filePath = filePath + "\\BookShopping02\\images\\";
OutputStream os = new FileOutputStream(filePath+theFile.getFileName());
byte [] buffer = new byte[8096];
int temp = 0;
while((temp=is.read(buffer, 0, 8096))!=-1)
{
os.write(buffer, 0, temp);
}
book.setImage("images/"+theFile.getFileName());
is.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(id.intValue()==0)
{
boolean isSaved = bookdao.save(book);
if(!isSaved)
{
ActionMessages errors = new ActionMessages();
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("errors.operate"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong");
}
}
else
{
bookdao.updateBook(book);
}
return mapping.findForward("toAddBook");
}
else
{
ActionMessages errors = new ActionMessages();
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("errors.noImage"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -