?? bookinfaction.java
字號:
package action.bookinf;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import actionform.bookinf.BookInfActionForm;
import bean.*;
import org.apache.struts.action.Action;
import java.util.ArrayList;
import java.sql.*;
import db.CustomerOperation;
public class BookInfAction extends Action {
/**
* 覆蓋Action的execute()方法
*
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//創建BookBean對象
BookBean bookbean = new BookBean();
BookBean oldBean = null;
//創建CustomerOperation對象
CustomerOperation md = new CustomerOperation();
//將form進行正確的類型轉換
BookInfActionForm bookInfActionForm = (BookInfActionForm) form;
//從request中取得list屬性
java.util.ArrayList list = (ArrayList) request.getSession()
.getAttribute("list");
if (list == null) {
list = new ArrayList();
}
if (request.getParameter("bookId") == null) {
boolean flag = false;
//從actionform中取得信息充實BookBean對象
String bookName = bookInfActionForm.getBookName();
bookbean.setBookName(bookName);
String bookId = bookInfActionForm.getBookId();
bookbean.setBookId(Integer.parseInt(bookId));
String bookPenster = bookInfActionForm.getBookPenster();
bookbean.setBookPenster(bookPenster);
String bookCompany = bookInfActionForm.getBookCompany();
bookbean.setBookCompany(bookCompany);
String bookPrice = bookInfActionForm.getBookPrice();
bookbean.setBookPrice(Integer.parseInt(bookPrice));
String bookStorage = bookInfActionForm.getBookStorage();
bookbean.setBookStorage(Integer.parseInt(bookStorage));
String bookSynopsis = bookInfActionForm.getBookSynopsis();
bookbean.setBookSynopsis(bookSynopsis);
bookbean.setBookData(new java.util.Date());
bookbean.setBookCount(1);
String bookType = bookInfActionForm.getBookType();
//通過CustomerOperation對象取得bookType
bookType = md.queryType(bookType);
//關閉數據庫的連接
md.Close();
bookbean.setBookAllPrice(bookbean.getBookPrice());
bookbean.setBookType(bookType);
for (int i = 0; i < list.toArray().length; i++) {
oldBean = (bean.BookBean) list.get(i);
if (bookbean.getBookId() == oldBean.getBookId()) {
((bean.BookBean) list.get(i)).setBookCount(oldBean
.getBookCount() + 1);
((BookBean) list.get(i)).setBookAllPrice(oldBean
.getBookAllPrice()
+ oldBean.getBookPrice());
flag = true;
request.getSession().setAttribute(
"zongshu",
(Integer.parseInt((request.getSession()
.getAttribute("zongshu")).toString())) + 1);
request.getSession().setAttribute(
"gongji",
((BookBean) list.get(i)).getBookPrice()
+ Integer
.parseInt((request.getSession()
.getAttribute("gongji")
.toString())));
break; // mapping.findForward("shopcar");
}
}
if (!flag) {
list.add(bookbean);
request.getSession().setAttribute("list", list);
request.getSession().setAttribute(
"zongshu",
(Integer.parseInt((request.getSession()
.getAttribute("zongshu")).toString())) + 1);
request.getSession().setAttribute(
"shangpin",
(Integer.parseInt((request.getSession()
.getAttribute("shangpin")).toString())) + 1);
request.getSession().setAttribute(
"gongji",
bookbean.getBookPrice()
+ Integer.parseInt((request.getSession()
.getAttribute("gongji").toString())));
}
}
if (request.getParameter("bookId") != null) {
boolean flag = false;
bookbean = md.getBookBean(request.getParameter("bookId"));
for (int i = 0; i < list.toArray().length; i++) {
oldBean = (bean.BookBean) list.get(i);
if (bookbean.getBookId() == oldBean.getBookId()) {
((BookBean) list.get(i)).setBookCount(oldBean
.getBookCount() + 1);
((BookBean) list.get(i)).setBookAllPrice(oldBean
.getBookAllPrice()
+ oldBean.getBookPrice());
flag = true;
request.getSession().setAttribute(
"zongshu",
(Integer.parseInt((request.getSession()
.getAttribute("zongshu")).toString())) + 1);
request.getSession().setAttribute(
"gongji",
((BookBean) list.get(i)).getBookPrice()
+ Integer
.parseInt((request.getSession()
.getAttribute("gongji")
.toString())));
break; // return mapping.findForward("shopcar");
}
}
if (!flag) {
list.add(bookbean);
request.getSession().setAttribute("list", list);
request.getSession().setAttribute(
"zongshu",
(Integer.parseInt((request.getSession()
.getAttribute("zongshu")).toString())) + 1);
request.getSession().setAttribute(
"shangpin",
(Integer.parseInt((request.getSession()
.getAttribute("shangpin")).toString())) + 1);
request.getSession().setAttribute(
"gongji",
bookbean.getBookPrice()
+ Integer.parseInt((request.getSession()
.getAttribute("gongji").toString())));
}
}
return mapping.findForward("shopcar");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -