首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

施用XFire+Spring+Struts构建Web Service及测试

2012-10-11 
使用XFire+Spring+Struts构建Web Service及测试!DOCTYPE web-app PUBLIC -//Sun Microsystems, Inc.//DT

使用XFire+Spring+Struts构建Web Service及测试

<!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>
<display-name>YMCN-XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>

?

?

<filter>
??? <filter-name>encodingFilter</filter-name>
??? <filter-class>org.ymcn.filter.CharacterEncoding</filter-class>
</filter>


<filter-mapping>
??? <filter-name>encodingFilter</filter-name>
??? <url-pattern>

?

4.在/WEB-INF/下编写applicationContext.xml 和 xfire-servlet.xml两文件,此二文件是必须的,前一个是Spring的,后一个是XFire的,其为被XFire自动加载.

applicationContext.xml 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>

?

5.xfire-servlet.xml 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

<!-- 使用XFire导出器 -->
<bean id="baseWebService" lazy-init="false" abstract="true">
?? <!-- 引用xfire.xml中定义的工厂 -->
?? <property name="serviceFactory" ref="xfire.serviceFactory"/>
?? <!-- 引用xfire.xml中的xfire实例 -->
?? <property name="xfire" ref="xfire"/>
</bean>

<!-- 定义所有访问 WEB服务的url -->
<bean parent="baseWebService">
?? <!-- 业务服务bean -->
?? <property name="serviceBean" ref="HelloWorld"/>
?? <!-- 业务服务bean的窄接口类 -->
?? <property name="serviceClass" value="org.ymcn.ws.HelloWorld"/>
? </bean>

</beans>

?

此时,我们就已经构建出了一个Web Service,我们在IE中输入:http://localhost:8888/wss/HelloWorldService.ws?wsdl

就能看到WSDL.

?

6.通过Java application 测试:编写JAVA简单测试类:

首先,在wss/src下建个客户端调用Web Service的Spring配置文件:client.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="HelloWorldService" pageEncoding="utf-8"%>
<%@ taglib prefix="html" uri="html" %>
<%@ taglib prefix="bean" uri="bean" %>
<%@ taglib prefix="logic" uri="logic" %>
<html>
<head>
<title>测试WebService - HelloWorld</title>
</head>

<body>

<logic:present name="NAME" scope="request">
<font color="red"><bean:write name="NAME" scope="request"/></font>
</logic:present>

<br>
<form action="/wss/ws/HelloWorld.ymcn" method="post">
用户名:<input type="text" name="name" style="width:500px">
<br><br>
<input type="submit" name="Submit" value=" 提交 "/>
</form>

</body>
</html>

?

8,struts-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />

<action-mappings>
<action path="/ws/HelloWorld" type="org.ymcn.struts.action.WSHelloWorldAction" scope="request" validate="false">
????? <forward name="hello-ok" path="/ws/helloWorld.jsp"/>
??? </action>
</action-mappings>

<message-resources parameter="org.ymcn.struts.i18n.i18n" />
</struts-config>

?

9.JSP请求Action类:WSHelloWorld

package org.ymcn.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ymcn.ws.HelloWorld;

public class WSHelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
??? HttpServletRequest request, HttpServletResponse response) throws Exception {
?
?? String name = (String)request.getParameter("name");
?
?? ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
???? HelloWorld helloWorld = (HelloWorld)ctx.getBean("HelloWorldService");
???? String result = helloWorld.sayHello(name);
???
?
???? if(result == null) {
???? result = "对不起, 调用WEB服务失败, 请重试......";
???? }
???
???? request.setAttribute("NAME", result);
???
?? return mapping.findForward("hello-ok");
}
}

?

到些,所有的工作已完成,部署WEB工程,启动Tomcat

?

10.在IE中输入:http://localhost:8888/wss/ws/helloWorld.jsp

?

?

点击提交:

?

?

?

恭喜你,你的工作得到了回报~~~~~~~~~~~~~~~~~~~~~~~~~~

最后是过滤器类了,我用的全是UTF-8,内容如下:

?

package org.ymcn.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncoding implements Filter
{
??? protected String encoding;
??? protected FilterConfig filterConfig;

??? public void init(FilterConfig filterConfig) throws ServletException
??? {
??????? this.filterConfig = filterConfig;
??????? this.encoding = "utf-8";
??? }

??? public void destroy( )
??? {
??????? this.encoding = null;
??????? this.filterConfig = null;
??? }

??? public void doFilter(ServletRequest request, ServletResponse response,
?????? FilterChain chain) throws IOException, ServletException
??? {
??? request.setCharacterEncoding(encoding);
??????? chain.doFilter(request, response);
??? }

??? protected String selectEncoding(ServletRequest request)
??? {
??????? return (this.encoding);
??? }
}

好的,所有工作做完了

热点排行