read-jpetstore-2-进入jpetstore

进入JpetStore
Ø <a href="shop/index.do">
Ø Struts 中的配置
<action path="/shop/index" type="org.springframework.samples.jpetstore.web.struts.DoNothingAction"
validate="false">
<forward name="success" path="/WEB-INF/jsp/struts/index.jsp"/>
</action>
Ø URL访问:
http://127.0.0.1:8080/jpetstore/shop/index.do

● 所有Action的基类BaseAction
/**
* JPetStore中所有Struts action 的基类。
* 它通过ServletContext查找Spring WebApplicationContext,
* 并且从WebApplicationContext获得PetStoreFacade的实现,使得子类通过protected的
* getter方法获得并调用PetStoreFacade。
*/

public abstract class BaseAction extends Action {
private PetStoreFacade petStore;
public void setServlet(ActionServlet actionServlet) {
super.setServlet(actionServlet);
if (actionServlet != null) {
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.petStore = (PetStoreFacade) wac.getBean("petStore");
}
}
protected PetStoreFacade getPetStore() {
return petStore;
}
}

● PetStoreFacade bean Spring定义
Ø <bean id="petStore" parent="baseTransactionProxy">
<property name="target">
<bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
<property name="accountDao"><ref bean="accountDao"/></property>

<property name="categoryDao"><ref bean="categoryDao"/></property>

<property name="productDao"><ref bean="productDao"/></property>

<property name="itemDao"><ref bean="itemDao"/></property>

<property name="orderDao"><ref bean="orderDao"/></property>

</bean>
</property>
</bean>

Ø在这使用了代理模式,所有的操作委托给PetStoreFacade,又PetStoreFacade再将具体操作委托给具体DAO。

public interface PetStoreFacade {
Account getAccount(String username);
Account getAccount(String username, String password);
void insertAccount(Account account);
void updateAccount(Account account);
List getUsernameList();
List getCategoryList();
Category getCategory(String categoryId);
List getProductListByCategory(String categoryId);
List searchProductList(String keywords);
Product getProduct(String productId);
List getItemListByProduct(String productId);
Item getItem(String itemId);
boolean isItemInStock(String itemId);
void insertOrder(Order order);
Order getOrder(int orderId);
List getOrdersByUsername(String username);
}

Ø PetStoreImpl类

这个类为我们提供了5个DAO对象,它减弱了对具体的持续化API的依赖,以此,虽然Jpetstore使用了iBATIS作为持续化的数据对象控制,但是,也可以使用其他的对象映射工具替代iBATIS,而做这些改动时,不需要改变PetStoreImpl类。

DAOs通过Spring提供的依赖注入功能,使得这些DAOs对Jpetstore可用,在这里使用Setter注入方法,Getter方法时可选的,但你也可以任意的扩展。

PetStoreImpl在JPetStore中只有一个实例对象,用Spring的术语来说,它是一个单例对象。

在Spring中的单例对象不需要像传统的单例设计模式中那样,提供私有的构造函数,静态工厂方法等实现。只需要简单的申明配置就好,其他的交给Spring去做。

PetStoreImpl是一个简单的POJO,它不依赖任何的Spring APIs,即便不在Spring容器中,它也可以正常的使用。

PetStoreImpl类为所有的方法定义了一个默认的事务管理属性。该属性只有在使用Commons Attributes autoproxying时才会被用到,而对于TransactionFactoryProxyBean的使用则要求你提供任何附加的属性定义,只是使用就好了。

public class PetStoreImpl implements PetStoreFacade, OrderService {
//JPetStore涉及到的主要的DAO对象
private AccountDao accountDao;
private CategoryDao categoryDao;
private ProductDao productDao;
private ItemDao itemDao;
private OrderDao orderDao;

public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}

public void setCategoryDao(CategoryDao categoryDao) {
this.categoryDao = categoryDao;
}

public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}

public void setItemDao(ItemDao itemDao) {
this.itemDao = itemDao;
}

public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}

public Account getAccount(String username) {
return this.accountDao.getAccount(username);
}

public Account getAccount(String username, String password) {
return this.accountDao.getAccount(username, password);
}

public void insertAccount(Account account) {
this.accountDao.insertAccount(account);
}

public void updateAccount(Account account) {
this.accountDao.updateAccount(account);
}

public List getUsernameList() {
return this.accountDao.getUsernameList();
}

public List getCategoryList() {
return this.categoryDao.getCategoryList();
}

public Category getCategory(String categoryId) {
return this.categoryDao.getCategory(categoryId);
}

public List getProductListByCategory(String categoryId) {
return this.productDao.getProductListByCategory(categoryId);
}

public List searchProductList(String keywords) {
return this.productDao.searchProductList(keywords);
}

public Product getProduct(String productId) {
return this.productDao.getProduct(productId);
}

public List getItemListByProduct(String productId) {
return this.itemDao.getItemListByProduct(productId);
}

public Item getItem(String itemId) {
return this.itemDao.getItem(itemId);
}

public boolean isItemInStock(String itemId) {
return this.itemDao.isItemInStock(itemId);
}

public void insertOrder(Order order) {
this.orderDao.insertOrder(order);
this.itemDao.updateQuantity(order);
}

public Order getOrder(int orderId) {
return this.orderDao.getOrder(orderId);
}

public List getOrdersByUsername(String username) {
return this.orderDao.getOrdersByUsername(username);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值