Struts框架SaveNewOrder Action
學(xué)習(xí)Struts框架時,經(jīng)常會遇到SaveNewOrder Action問題,這里將介紹SaveNewOrder Action問題的解決方法。
SaveNewOrder Action
示例應(yīng)用的用戶接口層使用Struts框架。這兒我們將討論當(dāng)為一個應(yīng)用分層時和Struts相關(guān)的部分。讓我們從在struts-config.xml文件里檢查一個Action配置開始。
- type="com.meagle.action.SaveOrderAction"
- name="OrderForm"
- scope="request"
- validate="true"
- input="/NewOrder.jsp">
- Save New Order
- path="/NewOrder.jsp"
- scope="request"
- type="com.meagle.exception.OrderException"/>
- path="/NewOrder.jsp"
- scope="request"
- type="com.
- meagle.
- exception.
- OrderMinimumAmountException"/>
SaveNewOrder Action被用來持久化一個用戶從用戶接口層提交的訂單。這是一個典型的Struts Action;然而,注意這個action的異常配置。這些Exceptions為我們的業(yè)務(wù)服務(wù)對象也在Spring 配置文件(applicationContext-hibernate.xml)中配置了(在transactionAttributes屬性里)。當(dāng)這些異常被從業(yè)務(wù)層擲出我們能在我們的用戶接口里恰當(dāng)?shù)奶幚硭鼈?。第一個異常,OrderException,當(dāng)在持久層里保存訂單對象失敗時將被這個action使用。這將引起事務(wù)回滾和通過業(yè)務(wù)對象傳遞把異常傳回給Struts層。OrderMinimumAmountException,在業(yè)務(wù)對象邏輯里的一個事務(wù)因為提交的訂單達不到最小訂單數(shù)量而失敗也將被處理。然后,事務(wù)將回滾和這個異常能被用戶接口層恰當(dāng)?shù)奶幚怼?/P>
最后一個連接步驟是使我們的表現(xiàn)層和我們的業(yè)務(wù)層交互。這已經(jīng)通過使用前面討論的服務(wù)定位器來完成了。服務(wù)層充當(dāng)一個到我們的業(yè)務(wù)邏輯和持久層的接口。這兒是Struts框架中的SaveNewOrder Action可能怎樣使用一個服務(wù)定位器調(diào)用一個業(yè)務(wù)方法:
- public ActionForward execute( ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)- throws java.lang.Exception {
- OrderForm oForm = (OrderForm) form;
- // Use the form to build an Order object that
- // can be saved in the persistence layer.
- // See the full source code in the sample app.
- // Obtain the wired business service object
- // from the service locator configuration
- // in BaseAction.
- // Delegate the save to the service layer and
- // further upstream to save the Order object.
- getOrderService().saveNewOrder(order);
- oForm.setOrder(order);
- ActionMessages messages = new ActionMessages();
- messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("message.order.saved.successfully"));- saveMessages(request, messages);
- return mapping.findForward("success");
- }
【編輯推薦】