叶落知秋

一叶落而知天下秋

统计

留言簿(1)

Java相关

阅读排行榜

评论排行榜

cxf+spring+struts2的helloWorld


 发表时间: 2007年11月30日



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

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

Service代码:

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

 

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

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.   
  8.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  9.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  10.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  11.   
  12.     <jaxws:endpoint    
  13.       id="helloWorld"    
  14.       implementor="com.zaife.cxfspring.HelloWorldImpl"    
  15.       address="/HelloWorld" />  
  16.          
  17.          
  18.   
  19.     <bean id="client" class="com.zaife.cxfspring.HelloWorld"    
  20.       factory-bean="clientFactory" factory-method="create"/>  
  21.        
  22.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  23.       <property name="serviceClass" value="com.zaife.cxfspring.HelloWorld"/>  
  24.       <property name="address" value="http://localhost:8080/cxf-spring/test/HelloWorld"/>  
  25.     bean>  
  26.        
  27.     <bean id="callCxfService" class="com.zaife.struts2.CallCxfService">  
  28.         <property name="client" ref="client" />  
  29.     bean>  
  30.          
  31. 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.   
  8.     <package name="cxf-spring" extends="struts-default">  
  9.   
  10.         <action name="hello" method="execute" class="callCxfService">  
  11.             <result>/index.jspresult>  
  12.                
  13.         action>                
  14.     package>  
  15.   
  16. 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.                        
  8.     <servlet>  
  9.         <display-name>CXF Servletdisplay-name>  
  10.         <servlet-name>CXFServletservlet-name>  
  11.         <servlet-class>  
  12.             org.apache.cxf.transport.servlet.CXFServlet   
  13.         servlet-class>  
  14.         <load-on-startup>1load-on-startup>  
  15.     servlet>  
  16.   
  17.     <servlet-mapping>  
  18.         <servlet-name>CXFServletservlet-name>  
  19.         <url-pattern>/test/*url-pattern>  
  20.     servlet-mapping>     
  21.          
  22.     <filter>  
  23.         <filter-name>struts2filter-name>  
  24.         <filter-class>  
  25.             org.apache.struts2.dispatcher.FilterDispatcher   
  26.         filter-class>  
  27.     filter>  
  28.   
  29.     <filter-mapping>  
  30.         <filter-name>struts2filter-name>  
  31.         <url-pattern>/*url-pattern>  
  32.     filter-mapping>      
  33.     <context-param>  
  34.         <param-name>contextConfigLocationparam-name>  
  35.         <param-value>WEB-INF/hello.xmlparam-value>  
  36.     context-param>  
  37.   
  38.     <listener>  
  39.         <listener-class>  
  40.             org.springframework.web.context.ContextLoaderListener   
  41.         listener-class>  
  42.     listener>  
  43.   
  44.   
  45.   <welcome-file-list>  
  46.     <welcome-file>index.jspwelcome-file>  
  47.   welcome-file-list>  
  48. 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.       
  8.   head>  
  9.      
  10.   <body>  
  11.     <s:property value="message" /><br>  
  12.   body>  
  13. html>  

posted on 2007-11-30 16:43 飞雪连天 阅读(2148) 评论(1)  编辑  收藏

评论

# re: cxf+spring+struts2的helloWorld 2016-08-12 13:21 3333

shenme wanying  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: