`

spring与Ajax的整合

阅读更多
作者: qukmei  

保留原作的出处:http://www.iblog.com/index.php?op=ViewArticle&articleId=28581&blogId=1499

  eclipse resin axis1.4 spring

  spring在整合axis上还是很不错的。

1.   环境配置

1、 基本设置,略
2、 在eclipse中配置引入相应的Spring框架、axis包,略。
因为axis还有一些可选包,所以可以把一些可选包都引进来,虽然网上下载的axis1.4只有核心包,但通过happyaxis.jsp,可以把相关可选包都找到。

2.   场景描述

在本例中,我们要完成的是通过Web Service调用到Spring工程中的getMessage的方法,传入人名,然后返回相应的Say Hello to somebody的字符串,并将调用后的字符串打印到前台Application界面中。

3.   代码开发

在MyEclipse中建立一个新的J2EE的Web Project。
编写IHelloWorld接口文件,代码如下:
package com.test.www;
/**
* Spring工程中要使用的接口文件
*/
public interface IHelloWorld {
  public String getMessage(String name);
}


编写HelloWorldImpl实现文件,代码如下:
package com.test.www.impl;
import com.test.www.IHelloWorld;
/**
* Spring工程中要使用的接口文件
*/
public class HelloWorldImpl implements IHelloWorld{
  private String helloStr; //Spring中需要注入的字符串
/**
* 实现接口中的方法,得到Say Hello to <somebody>的字符串
*/
  public String getMessage(String name){
      return helloStr+":"+name;
  }
  public String getHelloStr() {
      return helloStr;
  }
  public void setHelloStr(String helloStr) {
      this.helloStr = helloStr;
  }
}


编写Web-INF下的Web工程文件Web.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <!—Spring框架需要引入的配置文件及相关类 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <servlet>
      <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
<!—axis需要引入的Servlet -->
  <servlet>
      <servlet-name>axis</servlet-name>
      <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>axis</servlet-name>
      <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
<!—axis的Web Service的Web发布路径 -->
</web-app>

简要说明:这个配置文件里,可能还会有许多,不要怕,只需要把关于axis的copy到你自己的web.xml就可以了,不会冲突的,当然也可以稍加修改的,比如:/services/*.jws,这样看起来标准一些,其实这都是可以调试修改的。

编写Web-INF下的Spring工程文件xxx-services.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="helloWorld"
      class="com.test.www.impl.HelloWorldImpl">
      <property name="helloStr">
        <value>Say Hello to :</value>
      </property>
  </bean>
</beans>

原文在这里有一些小错,不过是可以看出来的,我把正确的贴上来了,上面这段要放到services的配置下,记得要把包名和类名写对。

以上就完成了整个Spring的代码部分的编写,好像到了这个地方还一直没有介绍Web Service的部分(除了如何配置axis)。其实在Spring中对Web Service进行封装很简单,仅仅需要继承org.springframework.remoting.jaxrpc.ServletEndpointSupport类,实现里面的一些方法,包装一次,将其发布出来就可以勒。
编写包装成Web Service的JaxRpcHelloWorld类,代码如下:
package com.test.www.webservice;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.test.www.IHelloWorld;
public class JaxRpcHelloWorld extends ServletEndpointSupport implements IHelloWorld{
  private IHelloWorld helloWorld;
 
protected void onInit() throws ServiceException {
  //在Spring容器中获取Bean的实例
    helloWorld = (IHelloWorld) getApplicationContext().getBean(
          "helloWorld");
  }
 
public String getMessage(String name) throws RemoteException {
  //执行Bean中的相同的方法
    return helloWorld.getMessage(name);
  }
}


最后编写server-config.wsdd文件,发布Web Service,具体代码如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
  <service name="HelloWorld" provider="java:RPC">
    <parameter name="className"
          value="com.test.www.webservice.JaxRpcHelloWorld"/>
    <parameter name="allowedMethods" value="*"/>
  </service>
  <transport name="http">
    <requestFlow>
        <handler type="URLMapper"/>
    </requestFlow>
  </transport>
</deployment>


所有的工作全部完成,接下来只需要启动resin来验证你的Web Service是否已经发布成功,启动resin后,在你的浏览器中输入:http://localhost:8080/<YourProject>/services/HelloWorld?wsdl,如果能发现HelloWorld(wdsl)等信息,恭喜你,你的所有的工作都已经完成。

4.   测试调用

编写一个叫TestWebServiceClient类,代码如下:
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.ServiceFactory;
public class testWebServiceClient {
  /**
    * @param args
    */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      try {
        String wsdlUrl = "http://localhost:8080/services/HelloWorld?wsdl";
        String nameSpaceUri = "http://localhost:8080/services/HelloWorld";
        // 创建调用对象
        Service service = new Service();
        Call call = null;
        call = (Call) service.createCall();
        // 调用sayHello
        System.out.println(">>>getMessage");
        call.setOperationName(new QName(nameSpaceUri, "getMessage"));
        call.setTargetEndpointAddress(new java.net.url(http://www.zhmy.com/wsdlUrl));
        String ret = (String) call.invoke(new Object[] { "ABC" });
        System.out.println("return value is " + ret);
      } catch (Exception e) {
        e.printStackTrace();
      }
  }
}


执行出来的结果,可想而知,会在控制台上输出一排字,内容如下:
Say Hello to:ABC


5:总结分析

因为用的是resin,所以要一定要配相关发布路径,要不然会报i18n的错的。

tomcat下更好弄。

这样就把resin+spring+axis结合起来了,与自己目前相关应用结合,就可以做实际业务处理了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics