在 Web 服务领域引入 MVC 模式的功能(5)
2006-05-08 14:19
393 查看
示例
我已经附上了实现此处所讨论的体系结构的一个简单示例。所有的示例代码都包含在了 zip 文件中(请参阅下面的参考资料)。这个例子举例说明了一个简单的新闻 Portal。新闻内容是从数据源(这里称为 DataSource )传送到 JSP 页,同时信息内容也要作为一个 Web 服务发布出来。这个 Portal 也可以从 StockQuote Web 服务中检索最新的股票报价并显示在同一个 JSP 页中。
这个 zip 文件包含了所有的源代码和 DataSource 的 SQL 脚本。它还包含有 Struts Action 和 ActionForm 类、模型服务类以及用来为发布者与订购者显示结果及其源代码的 JSP 页。它同时还包含有 WSDL 文件以及客户端源代码来访问已发布的 NewsContent Web 服务。
这里我不再阐述示例代码的详细信息了;您想要发现它的复杂性,最好的途径就是自己去试验。开始时,我会向您说明应用程序如何将信息内容(存储在 DataSource 中)和股票信息(从 Web 服务处获得)递交给 JSP 页的。
JSP 页被加载时,它就调用 Action 类,这个类在 MVC 设计模式中充当控制器角色。 Action 类通过传递预定义的数据访问对象来调用模型服务类中的 getNews() 方法。(清单 1 中所示的一小段 Action 类代码说明了如何从 Action 类中调用模型服务。)
getNews() 方法实现了所有必需的业务逻辑与数据检索逻辑。一旦从数据源取出了相关数据,模型服务就可以聚集数据访问对象并将它发送回给 Action 类。
清单 1. Action 类的部分代码
/*
* 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");
从模型服务处接收到数据访问对象后, Action 类通过传递转给模型服务的同一个数据访问对象来调用 WSManager 类中的 getStocks() 方法。 WSManager 类中的 getStocks() 方法执行 JAX-RPC 来订购股票 Web 服务。
从股票 Web 服务获得的结果要转变成本地的 schema 并且聚集回到预定义数据访问对象中去。这个对象从它被调用的地方传回到 action 类。 清单 2 阐明了如何使用 WSManager 中的 JAX-RPC 来订购 Web 服务,以及如何将股票 Web 服务的结果转变成本地 schema 并聚集回到数据访问对象中去。
清单 2. 订购 Web 服务及传送响应
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);
}
}
一旦 Action 类调用完模型服务和 WSManager, ActionForm 中的股票值及信息内容域就会被填充。控制就被返回到 JSP 页面,从 ActionForm 获取所有必需的值并打印 UI.
结束语
在本文中,您看到了 Structs 架构是如何与 Web 服务相集成的。您也了解了如何使用 Structs 组件来提供和预订 Web 服务。本文附带的简单应用程序代码将帮助您深入理解所有这些是如何工作的。
使用此处所阐明的体系结构,您可以开发出这样的企业应用程序,它非常健壮,很容易维护,而且能很容易地与早期应用程序集成在一起。我希望您将可以开发出更多的关于 Structs 和 Web 服务的项目,并看到这种体系结构在您自己的项目中是多么的有用。
我已经附上了实现此处所讨论的体系结构的一个简单示例。所有的示例代码都包含在了 zip 文件中(请参阅下面的参考资料)。这个例子举例说明了一个简单的新闻 Portal。新闻内容是从数据源(这里称为 DataSource )传送到 JSP 页,同时信息内容也要作为一个 Web 服务发布出来。这个 Portal 也可以从 StockQuote Web 服务中检索最新的股票报价并显示在同一个 JSP 页中。
这个 zip 文件包含了所有的源代码和 DataSource 的 SQL 脚本。它还包含有 Struts Action 和 ActionForm 类、模型服务类以及用来为发布者与订购者显示结果及其源代码的 JSP 页。它同时还包含有 WSDL 文件以及客户端源代码来访问已发布的 NewsContent Web 服务。
这里我不再阐述示例代码的详细信息了;您想要发现它的复杂性,最好的途径就是自己去试验。开始时,我会向您说明应用程序如何将信息内容(存储在 DataSource 中)和股票信息(从 Web 服务处获得)递交给 JSP 页的。
JSP 页被加载时,它就调用 Action 类,这个类在 MVC 设计模式中充当控制器角色。 Action 类通过传递预定义的数据访问对象来调用模型服务类中的 getNews() 方法。(清单 1 中所示的一小段 Action 类代码说明了如何从 Action 类中调用模型服务。)
getNews() 方法实现了所有必需的业务逻辑与数据检索逻辑。一旦从数据源取出了相关数据,模型服务就可以聚集数据访问对象并将它发送回给 Action 类。
清单 1. Action 类的部分代码
/*
* 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");
从模型服务处接收到数据访问对象后, Action 类通过传递转给模型服务的同一个数据访问对象来调用 WSManager 类中的 getStocks() 方法。 WSManager 类中的 getStocks() 方法执行 JAX-RPC 来订购股票 Web 服务。
从股票 Web 服务获得的结果要转变成本地的 schema 并且聚集回到预定义数据访问对象中去。这个对象从它被调用的地方传回到 action 类。 清单 2 阐明了如何使用 WSManager 中的 JAX-RPC 来订购 Web 服务,以及如何将股票 Web 服务的结果转变成本地 schema 并聚集回到数据访问对象中去。
清单 2. 订购 Web 服务及传送响应
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);
}
}
一旦 Action 类调用完模型服务和 WSManager, ActionForm 中的股票值及信息内容域就会被填充。控制就被返回到 JSP 页面,从 ActionForm 获取所有必需的值并打印 UI.
结束语
在本文中,您看到了 Structs 架构是如何与 Web 服务相集成的。您也了解了如何使用 Structs 组件来提供和预订 Web 服务。本文附带的简单应用程序代码将帮助您深入理解所有这些是如何工作的。
使用此处所阐明的体系结构,您可以开发出这样的企业应用程序,它非常健壮,很容易维护,而且能很容易地与早期应用程序集成在一起。我希望您将可以开发出更多的关于 Structs 和 Web 服务的项目,并看到这种体系结构在您自己的项目中是多么的有用。
相关文章推荐
- 在 Web 服务领域引入 MVC 模式的功能(2)
- 在 Web 服务领域引入 MVC 模式的功能(3)
- 在 Web 服务领域引入 MVC 模式的功能(4)
- 在 Web 服务领域引入 MVC 模式的功能(C#实例)
- 在 Web 服务领域引入 MVC 模式的功能(1)
- 在Web服务领域引入MVC模式的功能
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务(转载)
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务(包括使用 RestTemplate 与 REST 服务进行通信)
- Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- String3 MVC-----使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- Web 服务建模,第 1 部分: XML 模式