参照Cxf官方的例子,用Cxf+spring+struts2写了一个helloWorld的例子
首先安装cxf的eclipse插件,文档参考:incubator.apache.org/cxf/setting-up-eclipse.html
Service代码:
HelloWorld
- package com.zaife.cxfspring;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- String sayHi(String text);
- }
HelloWorldImpl
- package com.zaife.cxfspring;
- import javax.jws.WebService;
- @WebService(endpointInterface = "com.zaife.cxfspring.HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- public String sayHi(String text) {
- return "Hello " + text;
- }
- }
java 代码
- package com.zaife.struts2;
- import com.opensymphony.xwork2.Action;
- import com.opensymphony.xwork2.ActionSupport;
- import com.zaife.cxfspring.HelloWorld;
- public class CallCxfService extends ActionSupport {
- private HelloWorld client;
- private String message;
- public void setClient(HelloWorld client) {
- this.client = client;
- }
- public String execute() {
- this.message = this.client.sayHi("dd");
- return Action.SUCCESS;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
Spring配置文件
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <jaxws:endpoint
- id="helloWorld"
- implementor="com.zaife.cxfspring.HelloWorldImpl"
- address="/HelloWorld" />
- <bean id="client" class="com.zaife.cxfspring.HelloWorld"
- factory-bean="clientFactory" factory-method="create"/>
- <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass" value="com.zaife.cxfspring.HelloWorld"/>
- <property name="address" value="http://localhost:8080/cxf-spring/test/HelloWorld"/>
- bean>
- <bean id="callCxfService" class="com.zaife.struts2.CallCxfService">
- <property name="client" ref="client" />
- bean>
- beans>
Struts配置文件
- xml version="1.0" encoding="UTF-8" ?>
- "-//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" />
- <constant name="struts.devMode" value="true" />
- <package name="cxf-spring" extends="struts-default">
- <action name="hello" method="execute" class="callCxfService">
- <result>/index.jspresult>
- action>
- package>
- struts>
web.xml配置
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <display-name>CXF Servletdisplay-name>
- <servlet-name>CXFServletservlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- servlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>CXFServletservlet-name>
- <url-pattern>/test/*url-pattern>
- servlet-mapping>
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- filter-class>
- filter>
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>WEB-INF/hello.xmlparam-value>
- context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- listener-class>
- listener>
- <welcome-file-list>
- <welcome-file>index.jspwelcome-file>
- welcome-file-list>
- web-app>
Jsp页面
- <%@ page language="java" pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- >
- <html>
- <head>
- <title>My JSP 'index.jsp' starting pagetitle>
- head>
- <body>
- <s:property value="message" /><br>
- body>
- html>