GetAllGoodsAction 控制器

package action;
import java.util.ArrayList;
import java.util.List;
import entity.Goods;
import biz.GoodsBiz;
import biz.impl.GoodsBizImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 获取所有商品表的信息 Action
 * */
public class GetAllGoodsAction extends ActionSupport {
	private GoodsBiz goodsBiz=new GoodsBizImpl();
	private List<Goods>  goods=new ArrayList<Goods>();
	public GoodsBiz getGoodsBiz() {
		return goodsBiz;
	}
	public void setGoodsBiz(GoodsBiz goodsBiz) {
		this.goodsBiz = goodsBiz;
	}
	public List<Goods> getGoods() {
		return goods;
	}
	public void setGoods(List<Goods> goods) {
		this.goods = goods;
	}
	@Override
	public String execute() throws Exception {
		
		try {
			goods=goodsBiz.getAll();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
}

 

GetSelectedGoodsAction 控制器

package action;
import java.util.ArrayList;
import java.util.List;
import entity.Goods;
import biz.GoodsBiz;
import biz.impl.GoodsBizImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 查询商品表的信息 Action
 * */
public class GetSelectedGoodsAction extends ActionSupport {
	private GoodsBiz goodsBiz=new GoodsBizImpl();
    private List<Goods>  goods=new ArrayList<Goods>();
    private Integer[] ids;
	public GoodsBiz getGoodsBiz() {
		return goodsBiz;
	}
	public void setGoodsBiz(GoodsBiz goodsBiz) {
		this.goodsBiz = goodsBiz;
	}
	public List<Goods> getGoods() {
		return goods;
	}
	public void setGoods(List<Goods> goods) {
		this.goods = goods;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Override
	public String execute() throws Exception {
		
		try {
			goods=goodsBiz.getGoodsByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}	
}

 

saveOrderAction 控制器

package action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import tool.Tool;
import biz.GoodsBiz;
import biz.impl.GoodsBizImpl;
import com.opensymphony.xwork2.ActionSupport;
import entity.Goods;
import entity.Order;
import entity.OrderDetail;
/**
 * 保存并显示订单 Action
 * */
public class saveOrderAction extends ActionSupport {
	private GoodsBiz goodsBiz=new GoodsBizImpl();
	private Order order;
	private List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
	private Integer[] ids;
	private Integer[] amounts;
	private String name;
	private String address;
	private double total = 0.0;
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public List<OrderDetail> getOrderDetails() {
		return orderDetails;
	}
	public void setOrderDetails(List<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}
	public GoodsBiz getGoodsBiz() {
		return goodsBiz;
	}
	public void setGoodsBiz(GoodsBiz goodsBiz) {
		this.goodsBiz = goodsBiz;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public Integer[] getAmounts() {
		return amounts;
	}
	public void setAmounts(Integer[] amounts) {
		this.amounts = amounts;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String execute() throws Exception {
		try {
			goodsBiz.saveOrder(ids, amounts, name, address);
			order = goodsBiz.getOrderByIds(Tool.oId);
			orderDetails = goodsBiz.getOrderDetailByOdIds(Tool.odIds);
			for (OrderDetail orderDetail : orderDetails) {
				total += orderDetail.getGoods().getPrice()*orderDetail.getAmount();
			}
			Tool.oId=0;
			Tool.odIds.clear();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
}

 

配置 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"struts-2.3.dtd">
<struts>
	<!-- 配置文件修改的时候被重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>
	<!-- 修改系统的主题(页面) -->
	<constant name="struts.ui.theme" value="simple"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<global-results>
			<result name="error">error.jsp</result>
		</global-results>
		<action name="getAllGoods" class="action.GetAllGoodsAction">
			<result name="success">index.jsp</result>
		</action>
		<action name="selectedGoods" class="action.GetSelectedGoodsAction">
			<result name="success">selected.jsp</result>
		</action>
		<action name="saveOrder" class="action.saveOrderAction">
			<result name="success">order.jsp</result>
		</action>
	</package>
</struts>

 

error.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>错误页面</title>
</head>

<body>
	<h2 style="margin: 20px auto;">此路不通</h2>
</body>
</html>

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css/basic.css" />
<link type="text/css" rel="stylesheet" href="css/index.css" />
<title>商品展示</title>
</head>

<body>
	<div>
		<h3>商品展示</h3>
		<s:if test="%{goods.size()==0}"><p>没有商品</p></s:if>
		<s:else>
		<s:form action="selectedGoods" method="get">
		<s:iterator value="goods" >
		<p>
			<s:property value='name'/>    选购  <input type="checkbox" name="ids" value="<s:property value='id'/>" />
		</p>
		<ul>
			<li>厂商:<s:property value='manufacture'/></li>
			<li>价格:<s:property value='price'/></li>
			<li><s:property value='specification'/></li>
		</ul>
		</s:iterator>
		<s:submit value="下单" > </s:submit>
		</s:form>
		</s:else>
	</div>
</body>
</html>

 

效果图:

Struts2+Hibernate Action+struts.xml+jsp_java


 

selected.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css/basic.css" />
<link type="text/css" rel="stylesheet" href="css/selected.css" />
<title>订购商品</title>
</head>

<body>
	<div>
		<s:if test="%{goods.size()==0}">
			<h3>你没有商品</h3>
		</s:if>
		<s:else>
			<s:form action="saveOrder" method="post">
				<ul>
					<li>收货人:<input type="text" name="name" />
					</li>
					<li>收货人地址:<input type="text" name="address" />
					</li>
				</ul>
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td>名称</td>
						<td>价格</td>
						<td>数量</td>
					</tr>
					<s:iterator value="goods">
						<tr>
							<td><input type="hidden" name="ids"
								value="<s:property value='id'/>" /> <s:property value="name" />
							</td>
							<td><s:property value="price" /></td>
							<td><input type="text" name="amounts" value="1" /></td>
						</tr>
					</s:iterator>
				</table>
				<s:submit value="订购"></s:submit>
			</s:form>
		</s:else>
	</div>
</body>
</html>

 

效果图:

Struts2+Hibernate Action+struts.xml+jsp_css_02


 

order.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css/basic.css" />
<link type="text/css" rel="stylesheet" href="css/order.css" />
<title>订购商品</title>
</head>
<body>
	<s:debug></s:debug>
	<div>
		<ul>
			<li>订单号:<s:property value="order.id"/></li>
			<li>收货人:<s:property value="order.name"/></li>
			<li>收货人地址:<s:property value="order.address"/></li>
		</ul>
		<table cellpadding="0" cellspacing="0">
			<tr>
				<td>名称</td>
				<td>价格</td>
				<td>数量</td>
			</tr>
			
			<s:iterator value="orderDetails" >
			<tr>
				<td><s:property value='goods.name'/></td>
				<td><s:property value='goods.price'/></td>
				<td><s:property value='amount'/></td>
			</tr>
			</s:iterator>
			<tr>
				<td colspan="3">总价:<s:property value="total"/></td>
			</tr>
		</table>
	</div>
</body>
</html>

 

效果图:

Struts2+Hibernate Action+struts.xml+jsp_ui_03