検索機能のサンプル
public class ProductAction extends Action {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String keyword = request.getParameter("keyword");
if(keyword == null) {
keyword = "";
}
ProductDAO dao = new ProductDAO();
List<Product> list = dao.search(keyword);
session.setAttribute("list", list);
return "product.jsp";
}
}
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="menu.jsp" %>
<p>検索キーワードを入力してください。</p>
<form action="Product.action" method="post">
<input type="text" name="keyword">
<input type="submit" value="検索">
</form>
<hr>
<table style="border-collapse:separate;border-spacing:10px;">
<c:forEach var="product" items="${list }">
<tr>
<td>商品${product.id }</td>
<td><img src="image/${product.id }.jpg" height="64"></td>
<td>${product.name }</td>
<td>${product.price }円</td>
<td><a href="CartAdd.action?id=${product.id }">カートに追加</a></td>
</tr>
</c:forEach>
</table>
カート追加機能のサンプル
public class CartAddAction extends Action {
@SuppressWarnings("unchecked")
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
int id = Integer.parseInt(request.getParameter("id"));
List<Item> cart = (List<Item>)session.getAttribute("cart");
if (cart == null) {
cart = new ArrayList<Item>();
session.setAttribute("cart", cart);
}
for (Item i : cart) {
if(i.getProduct().getId() == id) {
i.setCount(i.getCount() + 1);
return "cart.jsp";
}
}
List<Product> list = (List<Product>)session.getAttribute("list");
for (Product p : list) {
if(p.getId() == id) {
Item i = new Item();
i.setProduct(p);
i.setCount(1);
cart.add(i);
}
}
return "cart.jsp";
}
}
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="menu.jsp" %>
<c:choose>
<c:when test = "${cart.size()>0 }">
<p>${cart.size() }種類の商品があります。</p>
<hr>
</c:when>
<c:otherwise>
<p>カートに商品がありません。</p>
</c:otherwise>
</c:choose>
<table style="border-collapse:separate;border-spacing:10px;">
<c:forEach var="item" items="${cart }">
<tr>
<td>商品${item.product.id }</td>
<td><img src="image/${item.product.id }.jpg" height="96"></td>
<td>${item.product.name }</td>
<td>${item.product.price }円</td>
<td>${item.count }個</td>
<td><a href="CartRemove.action?id=${item.product.id }">カートから削除</a></td>
</tr>
</c:forEach>
</table>
購入画面のサンプル
public class PreviewAction extends Action {
@SuppressWarnings("unchecked")
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
if(session.getAttribute("customer") == null) {
return "preview-error-login.jsp";
}
List<Item> cart = (List<Item>)session.getAttribute("cart");
if(cart == null || cart.size() == 0) {
return "preview-error-cart.jsp";
}
return "purchase-in.jsp";
}
}
購入情報の登録処理サンプル
public class PurchaseDAO extends DAO {
public boolean insert (List<Item> cart, String name, String address) throws Exception {
Connection con = getConnection();
con.setAutoCommit(false);
for (Item item : cart) {
PreparedStatement ps = con.prepareStatement("insert into purchase values(null, ?, ?, ?, ?, ?, ?)");
Product p = item.getProduct();
ps.setInt(1, p.getId());
ps.setString(2, p.getName());
ps.setInt(3, p.getPrice());
ps.setInt(4, item.getCount());
ps.setString(5, name);
ps.setString(6, address);
int line = ps.executeUpdate();
ps.close();
if(line != 1) {
con.rollback();
con.setAutoCommit(true);
con.close();
return false;
}
}
con.commit();
con.setAutoCommit(true);
con.close();
return true;
}
}
public class PurchaseAction extends Action {
@SuppressWarnings("unchecked")
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String name = request.getParameter("name");
String address = request.getParameter("address");
if(name.isEmpty() || address.isEmpty()) {
return "purchase-error-empty.jsp";
}
PurchaseDAO dao = new PurchaseDAO();
List<Item> cart = (List<Item>)session.getAttribute("cart");
if(cart == null || !dao.insert(cart, name, address)) {
return "purchase-error-insert.jsp";
}
session.removeAttribute("cart");
TODO
return null;
}
}