ITeye博客

cxf+spring+struts2的helloWorld

zaife · 2007-11-30 16:42 · cxf · Java · Apache · Spring · Struts · Servlet · Markdown

参照Cxf官方的例子,用Cxf+spring+struts2写了一个helloWorld的例子

首先安装cxf的eclipse插件,文档参考:incubator.apache.org/cxf/setting-up-eclipse.html

Service代码:

HelloWorld

  1. package com.zaife.cxfspring;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface HelloWorld {
  5. String sayHi(String text);
  6. }

HelloWorldImpl

  1. package com.zaife.cxfspring;
  2. import javax.jws.WebService;
  3. @WebService(endpointInterface = "com.zaife.cxfspring.HelloWorld")
  4. public class HelloWorldImpl implements HelloWorld {
  5. public String sayHi(String text) {
  6. return "Hello " + text;
  7. }
  8. }

 

java 代码

  1. package com.zaife.struts2;
  2. import com.opensymphony.xwork2.Action;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import com.zaife.cxfspring.HelloWorld;
  5. public class CallCxfService extends ActionSupport {
  6. private HelloWorld client;
  7. private String message;
  8. public void setClient(HelloWorld client) {
  9. this.client = client;
  10. }
  11. public String execute() {
  12. this.message = this.client.sayHi("dd");
  13. return Action.SUCCESS;
  14. }
  15. public String getMessage() {
  16. return message;
  17. }
  18. public void setMessage(String message) {
  19. this.message = message;
  20. }
  21. }

Spring配置文件

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7. <import resource="classpath:META-INF/cxf/cxf.xml" />
  8. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  9. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  10. <jaxws:endpoint
  11. id="helloWorld"
  12. implementor="com.zaife.cxfspring.HelloWorldImpl"
  13. address="/HelloWorld" />
  14. <bean id="client" class="com.zaife.cxfspring.HelloWorld"
  15. factory-bean="clientFactory" factory-method="create"/>
  16. <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
  17. <property name="serviceClass" value="com.zaife.cxfspring.HelloWorld"/>
  18. <property name="address" value="http://localhost:8080/cxf-spring/test/HelloWorld"/>
  19. bean>
  20. <bean id="callCxfService" class="com.zaife.struts2.CallCxfService">
  21. <property name="client" ref="client" />
  22. bean>
  23. beans>

Struts配置文件

  1. xml version="1.0" encoding="UTF-8" ?>
  2. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">
  4. <struts>
  5. <constant name="struts.objectFactory" value="spring" />
  6. <constant name="struts.devMode" value="true" />
  7. <package name="cxf-spring" extends="struts-default">
  8. <action name="hello" method="execute" class="callCxfService">
  9. <result>/index.jspresult>
  10. action>
  11. package>
  12. struts>

web.xml配置

xml 代码

  1. xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. <servlet>
  8. <display-name>CXF Servletdisplay-name>
  9. <servlet-name>CXFServletservlet-name>
  10. <servlet-class>
  11. org.apache.cxf.transport.servlet.CXFServlet
  12. servlet-class>
  13. <load-on-startup>1load-on-startup>
  14. servlet>
  15. <servlet-mapping>
  16. <servlet-name>CXFServletservlet-name>
  17. <url-pattern>/test/*url-pattern>
  18. servlet-mapping>
  19. <filter>
  20. <filter-name>struts2filter-name>
  21. <filter-class>
  22. org.apache.struts2.dispatcher.FilterDispatcher
  23. filter-class>
  24. filter>
  25. <filter-mapping>
  26. <filter-name>struts2filter-name>
  27. <url-pattern>/*url-pattern>
  28. filter-mapping>
  29. <context-param>
  30. <param-name>contextConfigLocationparam-name>
  31. <param-value>WEB-INF/hello.xmlparam-value>
  32. context-param>
  33. <listener>
  34. <listener-class>
  35. org.springframework.web.context.ContextLoaderListener
  36. listener-class>
  37. listener>
  38. <welcome-file-list>
  39. <welcome-file>index.jspwelcome-file>
  40. welcome-file-list>
  41. web-app>

Jsp页面

  1. <%@ page language="java"  pageEncoding="ISO-8859-1"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. >
  4. <html>
  5. <head>
  6. <title>My JSP 'index.jsp' starting pagetitle>
  7. head>
  8. <body>
  9. <s:property value="message" /><br>
  10. body>
  11. html>

评论(7)

  1. #7 楼 · lycmlove · 2008-09-22 19:08

    Cxf2.08+spring2+struts2下可以跑通。

  2. #6 楼 · samwalt · 2008-07-30 13:23

    最后在浏览器里运行:http://localhost:8080/HelloWorld/hello.action 出现下面的错误: description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Could not send Message. org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

    root cause

    javax.xml.ws.soap.SOAPFaultException: Could not send Message. org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:199) $Proxy33.sayHi(Unknown Source) com.zaife.struts2.CallCxfService.execute(CallCxfService.java:13) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597)

    ...... 这个 org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419

    碰到好几次了,一直都没有解决,是什么问题啊?

  3. #5 楼 · samwalt · 2008-07-30 11:13

    hanqiaorui writes:

    [quote=qianlei007]

    这几个文件,那里去找??[/quote] 这3个文件在cxf-2.1.1.jar里面。你可以找找看。

    还需要cxf的哪些jar包放在WEB-INF/lib路径下?

  4. #4 楼 · hanqiaorui · 2008-07-28 10:48

    qianlei007 writes:

    <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" />   
    

    这几个文件,那里去找??

    这3个文件在cxf-2.1.1.jar里面。你可以找找看。

  5. #3 楼 · qianlei007 · 2008-06-26 16:48

    这几个文件,那里去找??

  6. #2 楼 · zzuizui · 2008-04-08 11:27

    好文章,把CXF应用Struts2+Spring框架的快速上手文档!Eclipse3.3下可以跑通。

  7. #1 楼 · allenBen · 2007-12-20 16:23

    你就不能发完全了啊 学Apache全部发些半吊子例子 靠 com.zaife.struts2.CallCxfService呢?

评论功能维护中,当前仅展示历史评论。