Using Open Source Product Assembly Your web application architecture (4) reprint

xiaoxiao2021-03-06  60

Using Open Source Product Assembly Your web application architecture (4)

Providing a service locator (Providing a service locator)

Since we have set our Services and DAO. We need to display our service to other layers. This is usually encoded in Struts or Swing. A simple method is to return to Spring Context with the service locator. Of course, you can do it by calling beans in Spring directly.

Below is an example of a service locator in a struts actin.

Public Abstract Class baseAction extends action {

Private iorderservice OrderService;

Public void setServlet (ActionServlet

ACTIONSERVLET) {

Super.setServlet (ActionServlet);

ServletContext servletContext =

ActionServlet.getServletContext ();

WebApplicationContext WAC =

WebApplicationContextUtils.

GetRequiredWebapplicationContext

servletContext);

this.orderService = (iorderservice)

Wac.getbean ("ORDERSERVICE");

}

Protected iorderservice getorderService () {

Return ORDERVICE;

}

}

UI layer configuration (UI Layer Configuration)

This example uses Struts framework. Here we have to talk about the Struts section when we are giving the program. Let's start with the configuration information of Action in a struts-config.xml file.

Struts-config.xml file.

TYPE = "com.meagle.action.saveorderDerction"

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 This action is used to persistence of forms submitted over the UI layer. This is a typical action in Struts; pay attention to observing the Exception configuration in this action, which is also configured in the spring profile (ApplicationContext-Hibernate.xml) (in the Business Service Object TransactionAttributes properties). When an exception is thrown at the business layer, we can control them and display it appropriately to the UI layer.

The first exception, ordexception, is triggered when the lasting layer saves the ORDER object failed. This will cause things to roll back and pass abnormally to the Struts by BO.

The second exception, ORDERMINIMUMAMOUNTEXCEPTION is also the same as the first one.

The final step of matching is to combine your display layer and business layer. This has been implemented by the Service Locator (the previous discussion), where the service layer is provided to our business logic and persistence layer as an interface.

SAVENEWORDER Action Calls the execution business method in Struts. The method code is as follows:

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 what

// 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 = new actionMESSAGES ();

Messages.add (

ActionMessages.global_message,

New ActionMessage

"message.Order.saved.successful"));

SaveMessages (Request, Messages);

Return mapping.findforward ("Success");

}

to sum up

This article covers a lot of low-level basic information in technology and architecture, and the main intention of the article is to let you realize how to lay a layering for your application. The hierarchy can "decouple" your code - allowing new components to be added, and your code is easy to maintain. The techniques used here are only focused on the "decidentity". In any case, use such an architecture allows you to replace the current layer with other techniques. For example, you may not use Hibernate to implement persistence. Since you are programming in the DAO, you can use iBatis instead. Alternatively, you may also want to replace the current UI layer with other techniques or frames outside Struts (conversion long, the realization layer should not directly affect your business logic and business service layer). Built your web application with an appropriate frame, in fact, it is not a cumbersome work, more mainly it "decoupled" all layers in your program. postscript:

After reading this article, I just felt very much, so I translated it, of course, I was also prepared to take the egg throwing :).

There is no more technical details in this article, and the detailed steps. If you have never used these frameworks, you can post the instance program, you can post the 9CBS Forum Java Open Source version, I will definitely answer (Ah, this is not an advertisement?),

The article tells how to match the existing open source framework from a framework. There are too many terms I don't know how to express, and there may be a lot of statements. If you affect your reading, please click the original address directly, I am also sorry like you.

About the author: Mark Eagle Senior Software Engineer, Atlanta.

Totodo (zhangli@telecomjs.com) software engineer

reference:

Struts: http://jakarta.apache.org/struts/index.html

Spring: http://www.springframework.org

Hibernate: http://www.hibernate.org

http://www.hibernate.org.cn

About Control Inversion IOC and Dependent Injection: http://www.martinfowler.com/articles/injection.html

转载请注明原文地址:https://www.9cbs.com/read-118198.html

New Post(0)