Here, I will briefly introduce the next Struts Architecture and Model - View - Controller (MVC). Struts encourages the Model - View - Controller design paradigm to build up basic applications. Struts view through the ActionForm with Struts controller interaction.

When you want into enterprise applications and Web Services when used together, the simplest way is a single operation with a single enterprise services at a piece of. But that is not the best solution. In this article, Jerome Josephraj'll show you how to stratification based on the framework, the correct model - View - Controller (MVC) design pattern of Web Services applications. To this end, he modified the Struts (a popular open-source MVC framework) to make it applicable to Web services. Through research outlined here sample application programs, you will know how Struts can be combined with the use of Web Services.

The continuous development of the Java programming language and Sun's J2EE specification comply with all kinds of criteria makes software developers be able to create a distributed computing applications, these applications in the past only through the relevant specialized instrument can achieve. In this way, when some of the development groups to select the Java platform implementation of new systems, other groups will be through other technologies to create, enhance and maintain applications, then they have been integrated into various types of distributed applications去. This situation caused interoperability competition. New application with the old application how interactive it? The answer is: Web Services. Web Services are the holy grail of the new program design. They be able to share and coordinate all types of computing resources scattered.

In this article, you will know the purpose of implementation methods. You will see how Web services and the combination of open-source Struts box add up to build the basic application. Before you begin you must know a number of J2EE and Web Services knowledge. Here, I will briefly introduce the next Struts Architecture and Model - View - Controller (MVC).

MVC pattern: the separation of development roles

MVC design pattern is very clear delineation of the programmers and designers the role of boundaries. In other words, from the business logic on the data dismantling. This model is to allow designers to focus on the design of the display part of the application, and developer focused on development-driven application functionality required components.

MVC model has several variations, but they are all based on the same basic structure: the application data model (Model), display layer code (View), and control logic (Controller) is one of the independent existence but is capable of inter-communication components. Model component to describe and deal with application data. View refers to the user interface; it reflects the model data and submit it to the user. Controller is the view on the behavior (for example, press the Submit button) mapped to the model on the operation (for example, retrieve the user details). Model update, the view has also been updated, users can accomplish more acts. MVC pattern to make the code easy to understand but also easier to reuse code; In addition, a lot of projects in view to be updated frequently, MVC model will model and controller with these changes came an independent.

Figure 1. MVC design pattern

Struts: based on solid MVC framework

Struts are basic MVC model to build Web applications on an open source add box. Struts encourages the MVC pattern to build up the majority of applications but also provides shared Web application services.

At Struts application, you can build a model layer, so that business logic and data retrieval logic reuse is very easy. This layer is responsible for running the application business logic, access to relevant data (for example, to run SQL command, or read the flat file).
Struts encourages the Model - View - Controller design paradigm to build up basic applications. Struts provides its own Controller component (ActionController type) and combining with other technologies to provide the model and view. For model (Model-type), Struts to work with any standard data access technologies, including EJB, JDBC, and Object-Relational Bridge. The View (ActionForm category), Struts in JSP environment, as well as other logic-based systems that run well. Figure 2 sets out the application based on Struts logic flow.

Figure 2. Struts Application logic flow

Rough easy Web Services Architecture

Construction of Web Services is the simplest way to a single operation with a single enterprise service combined as shown in Figure 3. In this design method, implementation of business logic implementation service and data retrieval services are mixed together.

Figure 3. Easy rough Web Services Architecture

Such a Web Service can easily be from the existing business components developed. However, it has a lot of shortcomings: the user did not provide a unified certification, providers and subscribers are not tight coupling, there is no reuse of business logic. In short, for a coherent solution for this is not a very good architecture.

MVC model at the basis of Web Services implementation solutions will be even better point. Part in the follow-up, you will see how to use Struts to do this. I will elaborate on WSManager layer to the existing Struts framework, this one demonstrated the use of Web Services Services Ways models.

Struts applications using Web Services

Development work in the future you can expand to build a comparatively well Struts application to support Web Services. Mentioned earlier, Struts architecture clear distinction between the view, controller and model. Model contains all the necessary business logic from data storage to retrieve data warehouse. You can build a simple Web service layer - known as WSManager layer - such a model can provide Web Services Web Services can also be ordered. Architecture of the use of such applications will be the best component-based development with the World Wide Web together, shown in Figure 4.

Figure 4. The use of Web services Struts application

The following aspects have to be discussed in this architecture are different components of the detailed information, in particular, should pay close attention to WSManager layer, because it is this architecture really add some new.

Struts Controller

MVC architecture of the controller part of the main focus on receiving the request of the client (typically running Web browser users), decide what kind of business logic to implement functions to respond to the request, then in charge of the next generation user interface to connect to the appropriate view group pieces up. In Struts, the Controller's main component is a small ActionServlet Class Service procedures.

ActionServlet is responsible for the adoption of XML document will be the request URI is mapped to a specific behavior. This document contains a list of URI requests but also to inform ActionServlet it should be how to allocate each request. This approach has several advantages:

☆ application logic flow of the whole text file at classification.

☆ list in this format more easily view and understanding, especially for a big application procedures.

ActionServlet determines the application's flow. Action categories are much inherited the ActionServlet. Action in each category:

☆ are mapped to separate the process of treatment

☆ through the ActionController with Struts combining Struts JSPs

☆ as a succession of Struts in Action category of a Java implementation class.
Struts in Action category called category WSManager relevant approach to the use of Web Services. WSManager to obtain the required response - or if one has been lifted will be unusual - it back to the Struts controller.

WSManager

WSManager receive JAX-RPC endpoint request. Will WSManager class method calls are mapped to the new client requests. These new arrivals are SOAP client requests information format. WSManager must achieve safety certification, change parameters, the model at the request of the designated service, the requests to the pretreatment parameters. Request parameters contained in the form of Java objects, Java original parameters, XML document, or even piecewise SOAP document (for example, SOAP Element object). These types must be translated into support within the schema (for example, predefined Java Data Access Objects).

Although WSManager can deal directly with the Java object combined with the parameters, but also need to take some additional steps to deal with XML documents. Recommends the following steps:

1. WSManager category should be able to through the XML Schema to verify the validity of XML documents.

2. WSManager category and then to XML documents into an internal support schema.

3. WSManager final document should be decomposed to the extent possible, it is mapped to the domain object go.

WSManager achieve the following tasks are very important:

1. Identity authentication and authorization to use

2. Error Handling

3. Cache.

Can also be generated in response to WSManager; this process by the method call return value of easy structure composed. WSManager to retain this feature, you can cache data so as to avoid repeat visits to model service layer. You can also focus on the management of XML documents in response to aggregation, as well as the conversion, if you want to return to the scheduling of those documents must comply with different schema and internal schema, this point has become particularly important.

WSManager deal with all the SOAP request and put them assigned to the model layer by exposing business logic. If the model are as a Service EJB layer to achieve, then you can at EJB layer through Session Fa? Ade design patterns to achieve. If you use this mode WSManager, you will receive a lot of benefits because WSManager Council:

☆ as the initial point of contact to manage the requests and services

☆ call the security services, including identity verification and to authorize the use, thereby avoiding any duplicate layer of the visit.

☆ appointed business deal (using Struts application from the model used by services)

☆ At WSManager layer cache data to avoid any unnecessary database access.

Posted by: display of Web Services

WSManager category at each implementation of a public method will be published as a Web Service from. In other words, you want to for these types of release of a Web service description. Web Services are described by the Web Services Description Language Services (Web Services Description Language, WSDL) description and quote from it to any XML schema composed. (WSDL services are described in standard language.)

You can center or in the public register of companies at the enterprise registry Published on Web Services Description. Similarly, you can release from the definition of WEB Services XML Schema to the same public or proprietary (UDDI) Registration Center. Java Web services using JAXR client application programming interface to query the company or a public registry on the service description.

If your clients are the exclusive partner, then you do not need to use the registry. However, you can at your application at the Web layer or with the appropriate security protection of well-known location to publish your Web service description (WSDL and XML schemas). For example, the scenario has a resale customer of those applications, he has a particular vendor agreements. Customer application development time has been at manufacturers of static with a combination of Web Services. Only authorized groups can query XML schema or retrieval from the Web Service Description layer to generate client-side code. You should implement effective WSManager layer client authentication and authorization of use.
Order from: the use of Web Services

Applications can be used in a public registry or the enterprise has been maintained by Web Services. WSManager have to resolve the necessary WSDL documents related to the operation and call back a value. Struts controller class call at WSManager related approach to the use of specific Web Services. Data as a predefined data access objects at WSManager and send back and forth between Struts controller. Visit Web services happen all the anomalies will be listed at WSManager out and back to Struts ActionController.

Service requestor agent through the use of Service to search for Web services; find it if you want to use Web Services for the use of this service with the service providers will set up a contract, then can call the service business.

WSManager the use of WSDL documents, the server name, port name, operation name, as well as including the original Java types, Java arrays, Java objects or XML documents, such as all the necessary request parameters with one ordering a Web Service.

If it is published in the UDDI registry target Web Service, then all the Struts-based applications can be used as XMethods (please see reference) such agent services to ordering it. End in the implementation of the requested operation, the supplier of the desired Web Service return value. WSManager return value can be changed with the application makes it expected to match the schema; It can also be requested in the application to amend on the basis of the results. Receive results from WSManager later, Struts ActionController be able to deal with the results and send to the relevant view, or the model can also call the relevant Services to complete the deep treatment.

Error Handling

All errors are at the treatment WSManager layer; This eliminates the unnecessary server overhead. If the model of service is deemed to EJB layer to achieve, then its performance will be very significantly enhanced.

At the role of acting as suppliers, WSManager dished out all this SOAPFaultException such as abnormal. It also can check the new request and dished out all of the abnormal lack of mandatory fields. You can create a class to track and record data warehouse of these errors, then you can reference affectation.

In order to serve as role when, WSManager caught out by the service provider by all SOAP anomaly and will they change the format required by WSManager. You can record the error as dished out by subsequent reference. If necessary, then at the time required can also check out the response value and dished out as abnormal. You can create a category to record these anomalies, as a future reference. WSManager be able to verify the response values and can be used as an exception it thrown out.

Audit

At the role of acting as suppliers, WSManager can record detailed information, the future can be used as audit. Use of such information has a lot of purposes, such as:

☆ receive at the large number of sampling points based on the registered client.

☆ to collect data for marketing purposes.

☆ decide whether the application needs to be updated.

☆ identification and capture of illegal users.
Cache

Web services clients often than the general client - server architecture the client wants to have more; in Web Services architecture, the client do more work, such as cache. Web Services on the proper use of data cache can achieve its maximum performance. When the service request information is read-only or when the time than those in accordance with the information requested by the rate of change was even slower, you should consider to use Web Services caches.

Identity verification and to authorize the use

You can WSManager layer at the implementation of all subscriber authentication. All want to use Web services, customers must go through this kind of authentication logic. You can use the basic user authentication or digital certificates to achieve this purpose.

Struts View

You want to through the use of JSP technology to build applications based on Struts parts view. JSP pages contain static HTML to add dynamic content, which content is based on special acts tag note (at page request) is. JSP environment includes a set of standards of conduct tags. In addition, a custom tag library organized a standard set of tools, developers can use these tools to define their own tags.

Struts framework includes custom tags to expand the Treasury, the Treasury could assist the internationalization of the user interface more comprehensive and very modest interaction with the ActionForm components. View Comparison of thin layer, it does not provide the business logic. Struts view through the ActionForm with Struts controller interaction.

Struts ActionForm

Java Class ActionForms just some of it, it inherits the Struts provided ActionForm class, these classes contains accessor and mutator methods. Class Action or JSP page will call these methods to retrieve or update data from a database.

Model Service

Model of Service are as a group to implement the Java category. Each model will provide a Service Component Services, and the combination of these components also provide a set of common services. ActionController with WSManager category data as predefined data access object to send back and forth. In the treatment process, ActionController or call the correlation model can WSManager service components in the requirements. These components will be the requested data to data access object to model the form of services, service delivery model for all the business logic must then deal with the data from the storage warehouse to retrieve the necessary data. Service component aggregation model related to the predefined data access object, then its back to the ActionServlet or WSManager category. All the wrong information or confirmation notice will give ActionServlet or WSManager layer.

For your application, you should comply with the following design rules:

1. Model of Service and the view should not contain any relevant code (for example, deal with the conversation).

2. All the affairs of just only in Action or to submit WSManager layer.

3. Model of Service can only be the same model of service components in the model of other services or high-level category called Action.

Data storage layer

Data storage layer by all components of data warehouse storage. For example, it may contain relational databases, flat files or even XML documents have.

Example

I have attached the implementation of the system discussed here the structure of a simple example. All the sample code are included in the zip file. This example illustrates a simple press of the Portal. News content from the data source (referred to here as DataSource) sent to the JSP page, while the content of information should be published as a Web Service from. The Portal is also available from StockQuote Web Service to retrieve the latest stock quotes and displays in the same JSP page.

This zip file contains all the source code and the SQL script DataSource. It also contains the Struts Action and ActionForm type, model used for the Services category, as well as publishers and subscribers and its source code shows the results of the JSP page. It also contains a WSDL documents and client source code to access a published NewsContent Web Services.

Here my sample code is no longer on the details of the information; you want to discover the complexity of it, the best way to go its own test. The beginning, I will show you how to application information content (stored in the DataSource) and shares information (obtained from the Web Services) submitted to the JSP page.

JSP page is loaded, it will call the Action category, the category at MVC Controller Designed to serve as role models. Class Action by passing predefined data access object model to call the Service category getNews () method. (One shown in the list of a small section of the Action class code illustrates how to call the model category Action Service.)

getNews () method implementation of all the necessary business logic and data retrieval logic. Once removed from the data source of the relevant data, models can be assembled on the Service Data Access Objects and send it back to the Class Action.
List 1. Action part category code

/ *
* Create a pre-defined Data Access Object
* /
newsSearchResultVOB = new NewsSearchResult ();
/ *
* Call Business layer NewsMs's searchNews method passing the NewsCOD
* And get back a Collection of News
* /
newsList = (Vector) newsMs.getNews (newsSearchResultVOB);
/ *
* Put the NewsList in the request.
*
* /
request.setAttribute (SystemConstants.NEWS_LIST, newsList);
/ / Set the newslist in the form
newsFrm.setNewsDetails (newsList);
/ / Set the NewsForm in the request
request.setAttribute (mapping.getAttribute (), newsFrm);
/ / return actionForward;
return mapping.findForward ( "success");
From the model of service received after the Data Access Objects, Action category by passing to the model of service with a data access object to call WSManager category getStocks () method. WSManager category getStocks () method implementation of JAX-RPC to purchase shares Web Services.
Stock obtained from the results of Web Services is necessary to change the cost to the schema and gathered back to pre-defined data access object go. This object from the place it was called back to action category. 2 sets out a list of how to use WSManager of JAX-RPC to Order Web Service, as well as how to share the results of Web services and schema changes in the cost to gather data access objects to return to go.

List 2. Ordering Web Service and send in response to

public Vector getStocks () throws Exception (
/ / Method level variables
Vector stockValues = new Vector ();
try (
/ / Create service factory
javax.xml.rpc.ServiceFactory factory =
javax.xml.rpc.ServiceFactory.newInstance ();
/ / Define targetNameSpace
String targetNamespace = "http://www.themindelectric.com/"
+ "Wsdl / net.xmethods.services.stockquote.StockQuote /";
/ / Define qname
QName serviceName =
new QName (targetNamespace,
"net.xmethods.services.stockquote.StockQuoteService");
/ / Define portname
QName portName =
new QName (targetNamespace,
"net.xmethods.services.stockquote.StockQuotePort");
/ / Define operation name
QName operationName = new QName ( "urn: xmethods-delayed-quotes",
"getQuote");
/ / Specify wsdl location
java.net.URL wsdlLocation =
new java.net.URL ( "http://services.xmethods.net/soap/urn:xmethods-
delayed-quotes.wsdl ");
/ / Create service
javax.xml.rpc.Service service =
factory.createService (wsdlLocation, serviceName);
/ / Create call
javax.xml.rpc.Call call =
service.createCall (portName, operationName);
/ / Populate an array with list of stock names
Vector populatedStockNames = this.getPopulatedList ();
/ / Loop through the populated list, and for each stock get a result
java.util.Iterator i = this.getPopulatedList (). iterator ();
String stockName = null;
Float result;
com.ddj.wsstruts.valueobject.StockValue stockValue = null;
while (i.hasNext ()) (
stockName = (String) i.next ();
/ / Invoke the remote web service
result = (Float) call.invoke (new Object [] (stockName));
if (category.isDebugEnabled ()) (
category.debug ( "The quote for" + stockName + "is:" + result);
)
/ / Set stock name and stock values in stockValue bean
stockValue = new com.ddj.wsstruts.valueobject.StockValue ();
stockValue.setStockName (stockName);
stockValue.setStockValue (result.toString ());
/ / Add the stockValue bean to stockValues vector
stockValues.add (stockValue);
)
)
Once the call End Class Action Model for Service and WSManager, ActionForm in stock value and the information content of the domain will be filled. Control was returned to the JSP page, from the ActionForm to obtain all the necessary values and print UI.