📄 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 + -