url-pattern that excludes subdirectoriesy

需求其实就是把eclipse osgi导出的应用嵌入到原有的ssh开发的程序中。

 

	<filter> 
	  <filter-name>struts-cleanup</filter-name> 
	  <filter-class> 
	   org.apache.struts2.dispatcher.ActionContextCleanUp 
	  </filter-class> 
	</filter>
	<filter-mapping> 
	  <filter-name>struts-cleanup</filter-name> 
	  <url-pattern>/*</url-pattern> 
	</filter-mapping> 

	<filter>
	  <filter-name>struts</filter-name>
	  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>		
	</filter>
	<filter-mapping>
	  <filter-name>struts</filter-name>
	  <url-pattern>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>
	  <filter-name>struts</filter-name>
	  <url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
	  <filter-name>struts</filter-name>
	  <url-pattern>*.htm</url-pattern>
	</filter-mapping>
	<filter-mapping>
	  <filter-name>struts</filter-name>
	  <url-pattern>/struts/*</url-pattern>
	</filter-mapping>

 

 

 把eclipse osgi应用嵌入需要在web.xml中添加:

	<servlet id="bridge">
		<servlet-name>equinoxbridgeservlet</servlet-name>
		<servlet-class>org.eclipse.equinox.servletbridge.BridgeServlet</servlet-class>
		<init-param>
			<param-name>commandline</param-name>
			<param-value>-console</param-value>			
		</init-param>		
		<init-param>
			<param-name>enableFrameworkControls</param-name>
			<param-value>true</param-value>			
		</init-param>
		<init-param>
			<param-name>extendedFrameworkExports</param-name>
			<param-value></param-value>			
		</init-param>	
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>equinoxbridgeservlet</servlet-name>
		<url-pattern>
			/osgi/*
		</url-pattern>
	</servlet-mapping>

 

 由于struts的filter过滤了htm,导致osgi的htm文件被struts"劫"取了~~

 

经过一番挣扎,解决方法如下:

在struts过滤器中增加排除参数。

 

  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  	<init-param>
  		<param-name>struts.action.excludePattern</param-name>
  		<param-value>/osgi/.*</param-value>
  	</init-param>
  </filter>

 

为啥怎么弄,解释如下:

1、 读取init-param初始化参数

 

    protected List<Pattern> 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);

            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

            postInit(dispatcher, filterConfig);
        } finally {
            init.cleanup();
        }

    }

 

public List<Pattern> buildExcludedPatternsList( Dispatcher dispatcher ) {
   return buildExcludedPatternsList(
                 dispatcher.getContainer().getInstance(
                                  String.class,
                                  StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN));
}

 

2、 使用

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
                        throws IOException, ServletException {
        //父类向子类转:强转为http请求、响应
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

    try {
    //。。。@@
    if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
        chain.doFilter(request, response);
    } else {
        request = prepare.wrapRequest(request);
        ActionMapping mapping = prepare.findActionMapping(request, response, true);
        if (mapping == null) {
            boolean handled = execute.executeStaticResourceRequest(request, response);
            if (!handled) {
                chain.doFilter(request, response);
            }
         } else {
            execute.executeAction(request, response, mapping);
         }
     }
     } finally {
     prepare.cleanupRequest(request);
     }
    }
 

 

参考资源:

http://ari.iteye.com/blog/829843

http://www.docjar.org/html/api/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareAndExecuteFilter.java.html

 

servlet-mapping

http://selectshy.iteye.com/blog/1293458

 

Struts2的工作原理

http://space.itpub.net/13750068/viewspace-493899

 

Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>?

http://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping

 

Filter mapping url-pattern that excludes subdirectories

http://stackoverflow.com/questions/3466897/filter-mapping-url-pattern-that-excludes-subdirectories

 

// First cast ServletRequest to HttpServletRequest.
HttpServletRequest hsr = (HttpServletRequest) request;

// Check if requested resource is not in /test folder.
if (!hsr.getServletPath().startsWith("/test/")) {
    // Not in /test folder. Do your thing here.
}
 

 

http://www.blogjava.net/liuspring/archive/2008/09/01/226073.html

http://www.blogjava.net/liuspring/archive/2008/09/01/226073.html 写道
web.xml中<url-pattern>的3种写法

① 完全匹配

<url-pattern>/test/list.do</url-pattern>

② 目录匹配

<url-pattern>/test/*</url-pattern>

③ 扩展名匹配

<url-pattern>*.do</url-pattern>

servlet-mapping的重要规则:

☆ 容器会首先查找完全匹配,如果找不到,再查找目录匹配,如果也找不到,就查找扩展名匹配。

☆ 如果一个请求匹配多个“目录匹配”,容器会选择最长的匹配。

 

urlrewrite

http://tuckey.org/urlrewrite/

http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值