`
zhoujianghai
  • 浏览: 434040 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

struts2+spring2+Xfire开发webservice

阅读更多
第一次接触webservice,所以想在原有的项目上增加webservice功能,没想到今天相当的郁闷,遇到很多的错误,所以花了差不多一下午的时间去解决,不过最后终于成功了。下面是一个小例子(采用自底向上的开发模式)。写下来供以后参考。以下是主要配置文件。
配置步骤:
《1》、导包
struts2:
         1.         commons-logging-1.0.4.jar
         2.         freemarker-2.3.13.jar
         3.         ognl-2.6.11.jar
         4.         struts2-core-2.1.6.jar
         5.         xwork-2.1.2.jar
         6.         commons-fileupload-1.2.1.jar
         7.         commons-io-1.3.2.jar
         8.         struts2-spring-plugin-2.1.6.jar

spring2:
         1.        log4j-1.2.14.jar
         2.         aspectjweaver.jar
         3.        aspectjrt.jar
         4.         spring.jar

xfire-1.2.6(从网上下载新版本,然后把jar包导入WEB-INF\lib下:xfire-all-1.2.6.jar和lib下的所有包)

《2》、删除一些会冲突的包
把lib下的x-fire中的spring-1.2.6.jar删掉,它会和spring2中的spring.jar包发生冲突。

《3》配置web.xnl
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:applicationContext-*.xml,classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
  </context-param>
  
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>xfireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
     <servlet-name>xfireServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
   org.codehaus.xfire.spring.XFireSpringServlet
  </servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
    <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

《4》配置struts.xml(在该例子中还用不到它,以下配置只是与spring2集成)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="public" extends="struts-default">
  <action name="login" class="loginAction">
   ......
  </action>
</package>
</struts>


《5》配置applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="connectDb" class="jxust.zjh.db.ConnectDb" />
<bean id="educate" class="jxust.zjh.educateImpl" />

<bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
  <property name="serviceBean" ref="educate"/>
  <property name="serviceClass" value="jxust.zjh.Ieducate"/>
  <property name="inHandlers" ref="addressingHandler" />
  </bean>
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
</beans>


《6》配置applicationContext-actions.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="public" abstract="true">
  <property name="connectDb" ref="connectDb" />
</bean>
<bean id="loginAction" class="jxust.zjh.actions.LoginAction" scope="prototype" parent="public">
</bean>
</beans>


《7》修改services.xml(由于用的是spring2.0,所以把xmlns="http://xfire.codehaus.org/config/1.0"放在<service>中)
<?xml version="1.0" encoding="UTF-8"?>
<beans >
<service xmlns="http://xfire.codehaus.org/config/1.0">
  
  <name>educate</name>
  <serviceClass>jxust.zjh.Ieducate</serviceClass>
  <implementationClass>jxust.zjh.educateImpl</implementationClass>
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
</service></beans>


《8》编写一个简单的接口和实现类(用来测试)
Ieducate.java
package jxust.zjh;
public interface Ieducate {
public String queryStudent(String s_card);
}

EducateImpl.java(该实现类依赖了其他类的对象)
package jxust.zjh;
public class EducateImpl implements Ieducate {
public String queryStudent(String s_card) {
  return "该学生学号是:"+s_card;
}
}

这样在浏览器中输入:http://localhost:8080/EducateSystem_server/services/educate?wsdl
就可以看到生成的wsdl文件了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics