FROM: http://shade8109.blog.163.com/blog/static/130110604200911294257292/?fromdm&fromSearch&isFromSearchEngine=yes
1. 开发环境:
JDK 1.5, Tomcat 5.5.25, MyEclipse 6.0 其实也没什么特别之处.
2. 建立工程, 配好环境:
在MyEclipse下建立Web Project,取名为:wss, (意为:Webservice, Spring, Struts)
对wss增加:Web Service Capabilities, 记得选择XFire的Core Libraries
对wss增加:Struts Capabilities
3.修改web.xml,使之响应不同的请求以及XFire与Spring的结合
http://java.sun.com/dtd/web-app_2_3.dtd">
4.在/WEB-INF/下编写applicationContext.xml 和 xfire-servlet.xml两文件,此二文件是必须的,前一个是Spring的,后一个是XFire的,其为被XFire自动加载.
applicationContext.xml 的内容:
http://www.springframework.org/dtd/spring-beans.dtd">
5.xfire-servlet.xml 的内容:
http://www.springframework.org/dtd/spring-beans.dtd">
此时,我们就已经构建出了一个Web Service,我们在IE中输入:http://localhost:8888/wss/HelloWorldService.ws?wsdl
就能看到WSDL.
6.通过Java application 测试:编写JAVA简单测试类:
首先,在wss/src下建个客户端调用Web Service的Spring配置文件:client.xml,内容为:
http://www.springframework.org/dtd/spring-beans.dtd">
到此我们可以通过client.xml获得HelloWorld
package org.ymcn.test.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ymcn.ws.HelloWorld;
public class HelloWorld2 {
HelloWorld helloWorld = null;
public static void main(String[] args) {
HelloWorld2 test = new HelloWorld2();
test.testClient();
}
public void testClient() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
helloWorld = (HelloWorld)ctx.getBean("HelloWorldService");
System.out.println(helloWorld.sayHello("老牛啊"));
}
}
运行此程序,如输出:你好啊,老牛啊 说明正确.
7.在表示层(Struts)通过JSP调用Web Service:
JSP文件:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="html" uri="html" %>
<%@ taglib prefix="bean" uri="bean" %>
<%@ taglib prefix="logic" uri="logic" %>
8,struts-config.xml文件:
http://struts.apache.org/dtds/struts-config_1_2.dtd">
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);
}
}
好的,所有工作做完了