struts2与WebService整合问题解决

        由于项目需求的需要,要在原来用Struts2的框架之上集成webservice,因为之前单单做webservice的时候没有多大问题,使用Spring 和 Xfire就可以轻松地发布服务,但是,当和Struts2集成的时候问题确出现了。因为原来在web.xml中,struts2过滤了/*,即把根目录下得所有目录都进行了过滤,而webservice的目录我定义为/services/*,这样一来,客户端访问该web服务的时候就被strust2过滤器当成无效链接过滤了。

问题找到了,所以解决办法也很容易找到,有下面两种方式:
方式一:struts2不要过滤根目录下所有的文件目录,可以用/*.action,/*.do,/*.jsp的形式等等。或者在链接action的时候创立一个命名空间如my,然后struts2就只过滤my/*,这样就不会影响service/*的东西了。
方式二:感谢http://blog.csdn.net/lishengbo/article/details/5474044 提供的思路,修改struts2 dispatcher的内容,因为dispatcher里面有个方式dofilter,他得功能是把过滤请求传递给下面一个过滤器,这样就把当前的请求放行了。所以可以在dofilter里面做下处理,如果请求的链接种包含了service.那就放行,代码如下所示:

 

 if(request.getRequestURI().contains("services"))

        {

         

         chain.doFilter(req,res);

        }

解决这个问题只需要明确自己饮用的web.xml的配置,如下

 

 

 <filter>

    <filter-name>struts2</filter-name>

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

  </filter>

或者是:

 

 

<filter>

  	<filter-name>struts2</filter-name>

  	<filter-class>

  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

  	</filter-class>  	

  </filter>


这样我们的问题点就明确了,解决办法如下:

 

我们要重写这个类,可以在src下建立一个org.apache.struts2.dispatcher.ng.filte.StrutsPrepareAndExecuteFilter 类,或者自己指定一个位置,然后在web.xml中修改下对应的struts2的对应类即可。
我采用了方式二解决了问题。因为struts2监听所有的请求比较好,只对一些有要求的请求才放行。FilterDispatcher这个过滤器已经过时了。

 

package org.apache.struts2.dispatcher.ng.filter;



import java.io.IOException;

import java.util.List;

import java.util.regex.Pattern;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import org.apache.struts2.dispatcher.Dispatcher;

import org.apache.struts2.dispatcher.mapper.ActionMapping;

import org.apache.struts2.dispatcher.ng.ExecuteOperations;

import org.apache.struts2.dispatcher.ng.InitOperations;

import org.apache.struts2.dispatcher.ng.PrepareOperations;



public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {

	protected PrepareOperations prepare;

	protected ExecuteOperations execute;

	protected List<Pattern> excludedPatterns;



	public StrutsPrepareAndExecuteFilter() {

		this.excludedPatterns = null;

	}



	public void init(FilterConfig filterConfig) throws ServletException {

		InitOperations init = new InitOperations();

		try {

			FilterHostConfig config = new FilterHostConfig(filterConfig);

			init.initLogging(config);

			Dispatcher dispatcher = init.initDispatcher(config);

			init.initStaticContentLoader(config, dispatcher);



			this.prepare = new PrepareOperations(filterConfig

					.getServletContext(), dispatcher);

			this.execute = new ExecuteOperations(filterConfig

					.getServletContext(), dispatcher);

			this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);



			postInit(dispatcher, filterConfig);

		} finally {

			init.cleanup();

		}

	}



	protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {

	}



	public void doFilter(ServletRequest req, ServletResponse res,

			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;

		try {

			if ("/WebServiceTest2/services/HelloWordWeb".endsWith(request

					.getRequestURI())

					|| "/servlet/".endsWith(request.getRequestURI())) {

				chain.doFilter(req, res);

			} else {

				HttpServletResponse response = (HttpServletResponse) res;



				this.prepare.setEncodingAndLocale(request, response);



				this.prepare.createActionContext(request, response);

				this.prepare.assignDispatcherToThread();

				if ((this.excludedPatterns != null)

						&& (this.prepare.isUrlExcluded(request,

								this.excludedPatterns))) {

					chain.doFilter(request, response);

				} else {

					request = this.prepare.wrapRequest(request);

					ActionMapping mapping = this.prepare.findActionMapping(

							request, response, true);

					if (mapping == null) {

						boolean handled = this.execute

								.executeStaticResourceRequest(request, response);

						if (!(handled))

							chain.doFilter(request, response);

					} else {

						this.execute.executeAction(request, response, mapping);

					}

				}



			}

		} finally {

			this.prepare.cleanupRequest(request);

		}



	}



	public void destroy() {

		this.prepare.cleanupDispatcher();

	}

}

由此我们可以清楚的明白了struts2我们可以操作任何的过滤器和代码,这只是一个简单的例子,更多的需要我们坚持不懈的探索!

 


 

你可能感兴趣的:(webservice)