该案例为传智之前的一个教学案例,基于基于SSH架构,分前台展示商品及购买、后台对商品信息维护及查询用户订单等功能。
一、涉及软件
Eclipse,JDK1.7,MySQL5.6,jQuery1.8,Spring3.2,Hibernate3,Struts2.3
二、项目介绍
1、框架配置
applicationContext.xml:
<!-- 配置连接池: -->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池: -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate的相关信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置Hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>cn/itcast/shop/user/vo/User.hbm.xml</value>
<value>cn/itcast/shop/category/vo/Category.hbm.xml</value>
<value>cn/itcast/shop/product/vo/Product.hbm.xml</value>
<value>cn/itcast/shop/categorysecond/vo/CategorySecond.hbm.xml</value>
<value>cn/itcast/shop/order/vo/Order.hbm.xml</value>
<value>cn/itcast/shop/order/vo/OrderItem.hbm.xml</value>
<value>cn/itcast/shop/adminuser/vo/AdminUser.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理: -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Action的配置 ===========================-->
<!-- 首页访问的Action -->
<bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
<property name="productService" ref="productService"/>
</bean>
<!-- Service的配置 ===========================-->
<bean id="userService" class="cn.itcast.shop.user.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<!-- Dao的配置 ===========================-->
<bean id="userDao" class="cn.itcast.shop.user.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<package name="shop" extends="struts-default" namespace="/">
<!-- 配置自定义拦截器 -->
<interceptors>
<interceptor name="privilegeInterceptor" class="cn.itcast.shop.interceptor.PrivilegeInterceptor"/>
</interceptors>
<global-results>
<result name="msg">/WEB-INF/jsp/msg.jsp</result>
<result name="login">/admin/index.jsp</result>
</global-results>
<!-- 配置首页访问的Action -->
<action name="index" class="indexAction">
<result name="index">/WEB-INF/jsp/index.jsp</result>
</action>
<!-- 配置用户模块的Action -->
<action name="user_*" class="userAction" method="{1}">
<result name="registPage">/WEB-INF/jsp/regist.jsp</result>
<result name="input">/WEB-INF/jsp/regist.jsp</result>
<result name="loginPage">/WEB-INF/jsp/login.jsp</result>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="loginSuccess" type="redirectAction">index</result>
<result name="quit" type="redirectAction">index</result>
<result name="checkcodeFail">/WEB-INF/jsp/regist.jsp</result>
</action>
</package>
</struts>
jdbc.properties:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///shop?characterEncoding=UTF-8
jdbc.user = root
jdbc.password =root
2、实现原理
项目组织架构:
页面采用html+jQuery开发,以商品一级分类维护功能举例介绍
admin/category/list.jsp
<td class="ta_01" align="center" bgColor="#f5fafe">
<table cellspacing="0" cellpadding="1" rules="all"
bordercolor="gray" border="1" id="DataGrid1"
style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
<tr
style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">
<td align="center" width="18%">
序号
</td>
<td align="center" width="17%">
一级分类名称
</td>
<td width="7%" align="center">
编辑
</td>
<td width="7%" align="center">
删除
</td>
</tr>
<s:iterator var="c" value="cList" status="status">
<tr οnmοuseοver="this.style.backgroundColor = 'white'"
οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="18%">
<s:property value="#status.count"/>
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">
<s:property value="#c.cname"/>
</td>
<td align="center" style="HEIGHT: 22px">
<a href="${ pageContext.request.contextPath }/adminCategory_edit.action?cid=<s:property value="#c.cid"/>">
<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<a href="${ pageContext.request.contextPath }/adminCategory_delete.action?cid=<s:property value="#c.cid"/>">
<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</s:iterator>
</table>
</td>
AdminCategoryAction.class
public class AdminCategoryAction extends ActionSupport implements ModelDriven<Category>{
// 模型驱动使用的对象.
private Category category = new Category();
public Category getModel() {
return category;
}
// 注入一级分类的Service
public CategoryService categoryService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
// 查询所有一级分类
public String findAll(){
// 调用Service查询所有一级分类
List<Category> cList = categoryService.findAll();
// 通过值栈保存一级分类集合:
ActionContext.getContext().getValueStack().set("cList", cList);
return "findAll";
}
// 保存一级分类的方法
public String save(){
// 调用Service完成保存一级分类
categoryService.save(category);
// 进行页面跳转:
return "saveSuccess";
}
// 删除一级分类的方法:
public String delete(){
// 调用Service完成 一级分类的删除
// 级联删除一定先查询在删除:
category = categoryService.findByCid(category.getCid());
categoryService.delete(category);
// 进行页面转向:
return "deleteSuccess";
}
// 编辑一级分类的方法:
public String edit(){
// 接收cid:
// 根据cid进行查询:
category = categoryService.findByCid(category.getCid());
// 完成页面转向:将一级分类数据显示到页面上.
return "editSuccess";
}
// 修改一级分类的方法:
public String update(){
// 使用模型驱动接收前台提交数据:
categoryService.update(category);
// 页面跳转:
return "updateSuccess";
}
}
CategoryService.classpublic class CategoryService {
// 注入CategoryDao
private CategoryDao categoryDao;
public void setCategoryDao(CategoryDao categoryDao) {
this.categoryDao = categoryDao;
}
// 业务层查询所有一级分类的方法
public List<Category> findAll() {
return categoryDao.findAll();
}
// 业务层保存一级分类的操作
public void save(Category category) {
categoryDao.save(category);
}
// 业务层根据一级分类id查询一级分类
public Category findByCid(Integer cid) {
return categoryDao.findByCid(cid);
}
// 业务层删除一级分类
public void delete(Category category) {
categoryDao.delete(category);
}
// 业务层修改一级分类
public void update(Category category) {
categoryDao.update(category);
}
}
CategoryDao.class
public class CategoryDao extends HibernateDaoSupport{
//DAO层的查询所有一级分类的方法
public List<Category> findAll() {
String hql = "from Category";
List<Category> list = this.getHibernateTemplate().find(hql);
return list;
}
// Dao中的保存一级分类的方法
public void save(Category category) {
this.getHibernateTemplate().save(category);
}
// Dao中根据一级分类id查询一级分类
public Category findByCid(Integer cid) {
return this.getHibernateTemplate().get(Category.class, cid);
}
// DAO中删除一级分类
public void delete(Category category) {
this.getHibernateTemplate().delete(category);
}
// Dao中修改一级分类
public void update(Category category) {
this.getHibernateTemplate().update(category);
}
}
Category.classpublic class Category implements Serializable{
private Integer cid;
private String cname;
// 一级分类中存放二级分类的集合:
private Set<CategorySecond> categorySeconds = new HashSet<CategorySecond>();
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Set<CategorySecond> getCategorySeconds() {
return categorySeconds;
}
public void setCategorySeconds(Set<CategorySecond> categorySeconds) {
this.categorySeconds = categorySeconds;
}
}
3、其它功能展示
后台商品信息维护及订单查询
用户信息查看、修改
商品分类信息维护
商品维护
订单查询
前台首页展示
分类展示商品
商品展示
购物车