1、第一种方法,在配置cxf时,前namespace前缀
如:
<
servlet
>
<
servlet-name
>CXFServlet</
servlet-name
>
<
servlet-class
>
org.apache.cxf.transport.servlet.CXFServlet
</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>CXFServlet</
servlet-name
>
这样配置会拦截掉js等文件,当然struts .action也会被拦截
<
url-pattern
>/webservice</
url-pattern
> -->
</
servlet-mapping
>
拦截器如下:
package mycxf.inteceptor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.transport.servlet.CXFServlet;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyCXFServlet extends CXFServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Map<
String
, String> STATIC_CONTENT_TYPES;
static {
STATIC_CONTENT_TYPES = new HashMap<
String
, String>();
STATIC_CONTENT_TYPES.put("html", "text/html");
STATIC_CONTENT_TYPES.put("txt", "text/plain");
STATIC_CONTENT_TYPES.put("css", "text/css");
STATIC_CONTENT_TYPES.put("js", "text/javascript");
STATIC_CONTENT_TYPES.put("pdf", "application/pdf");
STATIC_CONTENT_TYPES.put("png", "image/png");
// TODO : add more types if needed
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
String url = ((HttpServletRequest)req).getRequestURI();
if (isSpecialFile(url)) {
serveStaticContent((HttpServletRequest) req, (HttpServletResponse) res, ((HttpServletRequest)req).getPathInfo());
} else {
super.service(req, res);
}
}
public boolean isSpecialFile(String url){
for(String key:STATIC_CONTENT_TYPES.keySet()){
if(url.contains("."+key)){
return true;
}
}
return false;
}
public void serveStaticContent(HttpServletRequest request,
HttpServletResponse response, String pathInfo)
throws ServletException {
InputStream is = super.getServletContext()
.getResourceAsStream(pathInfo);
if (is == null) {
throw new ServletException("Static resource " + pathInfo
+ " is not available");
}
try {
int ind = pathInfo.lastIndexOf(".");
if (ind != -1 && ind <
pathInfo.length
()) {
String
type
=
STATIC_CONTENT_TYPES
.get(pathInfo.substring(ind + 1));
if (type != null) {
response.setContentType(type);
}
}
ServletOutputStream
os
=
response
.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (IOException ex) {
throw new ServletException("Static resource " + pathInfo
+ " can not be written to the output stream");
}
}
}
然后在web.xml里配置cxf拦截器:
<servlet>
<
servlet-name
>CXFServlet</
servlet-name
>
<
servlet-class
>
mycxf.inteceptor.MyCXFServlet
</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>CXFServlet</
servlet-name
>
<
url-pattern
>/*</
url-pattern
>
</
servlet-mapping
>