新聞中心
學(xué)習(xí)Struts框架時(shí),經(jīng)常會(huì)遇到SaveNewOrder Action問(wèn)題,這里將介紹SaveNewOrder Action問(wèn)題的解決方法。

SaveNewOrder Action
示例應(yīng)用的用戶接口層使用Struts框架。這兒我們將討論當(dāng)為一個(gè)應(yīng)用分層時(shí)和Struts相關(guān)的部分。讓我們從在struts-config.xml文件里檢查一個(gè)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被用來(lái)持久化一個(gè)用戶從用戶接口層提交的訂單。這是一個(gè)典型的Struts Action;然而,注意這個(gè)action的異常配置。這些Exceptions為我們的業(yè)務(wù)服務(wù)對(duì)象也在Spring 配置文件(applicationContext-hibernate.xml)中配置了(在transactionAttributes屬性里)。當(dāng)這些異常被從業(yè)務(wù)層擲出我們能在我們的用戶接口里恰當(dāng)?shù)奶幚硭鼈?。第一個(gè)異常,OrderException,當(dāng)在持久層里保存訂單對(duì)象失敗時(shí)將被這個(gè)action使用。這將引起事務(wù)回滾和通過(guò)業(yè)務(wù)對(duì)象傳遞把異常傳回給Struts層。OrderMinimumAmountException,在業(yè)務(wù)對(duì)象邏輯里的一個(gè)事務(wù)因?yàn)樘峤坏挠唵芜_(dá)不到最小訂單數(shù)量而失敗也將被處理。然后,事務(wù)將回滾和這個(gè)異常能被用戶接口層恰當(dāng)?shù)奶幚怼?/p>
最后一個(gè)連接步驟是使我們的表現(xiàn)層和我們的業(yè)務(wù)層交互。這已經(jīng)通過(guò)使用前面討論的服務(wù)定位器來(lái)完成了。服務(wù)層充當(dāng)一個(gè)到我們的業(yè)務(wù)邏輯和持久層的接口。這兒是Struts框架中的SaveNewOrder Action可能怎樣使用一個(gè)服務(wù)定位器調(diào)用一個(gè)業(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");
- }
當(dāng)前標(biāo)題:Struts框架SaveNewOrderAction
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/dhgdodd.html


咨詢
建站咨詢
