2013-04-07 104 views
3

什麼是雙方的這種正確的配置「並存」:映射的servlet根

http://localhost:8888/index.html 

http://localhost:8888/{some_path_value} 

的第一個項目將返回一個HTML頁面,並且還將containt HREF將要訪問的資源像/public/images/bg.png

現在第二個項目是在同一個根上下文廣告映射,供應頁面的一個(的index.html,PNG,JPG,CSS,JS等)

所以這個問題我一個RESTful API我現在面臨的是,當我配置Rest API servlet映射像th一樣是:

<servlet> 
    <servlet-name>Resteasy</servlet-name> 
    <servlet-class> 
     org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 
    </servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Resteasy</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

的REST API的作品,但它有效地消除了訪問喜歡的index.html,CSS,JS等呈現一個「主頁」的靜態資源。

但是,如果我將映射更改爲諸如/api/*之類的東西,現在可以訪問GWT應用,但Rest API的PATH不再是根。

那麼在我的應用程序中可能會出現什麼問題?我真的需要讓兩者在同一條路徑上共存。我最初的想法是做某種過濾器,但可能有更簡單更合適的解決方案。

更新:

我的應用程序的吉斯模塊

public class MyModule implements Module 
{ 
    public void configure(final Binder binder) 
    { 
     binder.bind(MyResource.class); 
    } 
} 

的web.xml

<context-param> 
    <param-name>resteasy.guice.modules</param-name> 
    <param-value>org.jboss.errai.ui.demo.server.MyModule</param-value> 
</context-param> 

<listener> 
    <listener-class> 
     org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener 
    </listener-class> 
</listener> 

<context-param> 
    <param-name>resteasy.servlet.mapping.prefix</param-name> 
    <param-value>/api</param-value> 
</context-param> 

<servlet> 
    <servlet-name>Resteasy</servlet-name> 
    <servlet-class> 
     org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 
    </servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Resteasy</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

回答

6

所以你的問題是,你是在映射所有根請求去其餘的servlet,因爲它不起作用即

  1. 如果有一種方法可以知道您的其他servlet的某些模式,您可以在web.xml中配置所有這些特定模式。但url-pattern使用一種非常簡單的語法,只允許使用whatever/**.extension,並且您的rest-servlet似乎不符合此要求。

  2. 另一個選擇可能是使用了先進的servlet調度像GuiceServletContextListener(由guice提供),並配置WebModule女巫豐富的正則表達式。修改您的web.xml添加WebModule並配置模塊來處理URL和調度與適當的servlet(從你的web.xml文件中刪除這些小服務程序)

<context-param> 
    <param-name>resteasy.guice.modules&lt;/param-name> 
    <param-value>org.jboss.errai.ui.demo.server.MyModule</param-value> 
    <param-value>org.jboss.errai.ui.demo.server.MyWebModule</param-value> 
    </context-param> 
public class MyWebModule extends ServletModule { 
    @Override 
    protected void configureServlets() { 
     // Note: all servlets and filters must be singletons 
     bind(FactoryServlet.class).in(Singleton.class); 
     // Pass to the HttpServletDispatcher everything but urls ending with static extensions 
     serveRegex(".+(?<!\\.(html|css|png|jpg))") 
      .with(HttpServletDispatcher.class); 
    } 
    }  
  1. 最後一個選項,是編寫您自己的過濾器,您可以在其中檢測路徑是否匹配靜態文件並分發它。
private FilterConfig config; 
    public void init(FilterConfig filterConfig) throws ServletException { 
    config = filterConfig; 
    } 

    public void doFilter(ServletRequest servletRequest, 
     ServletResponse servletResponse, FilterChain filterChain) 
     throws IOException, ServletException { 

    HttpServletRequest req = (HttpServletRequest) servletRequest; 
    HttpServletResponse resp = (HttpServletResponse) servletResponse; 

    File file = new File(config.getServletContext().getRealPath(req.getServletPath())); 
    if (file.canRead()) { 
     // NOTE: you have to set the most appropriate type per file 
     resp.setContentType("text/html"); 

     // This depends on apache commons-io 
     IOUtils.copy(new FileInputStream(file), resp.getOutputStream()); 
    } else { 
     filterChain.doFilter(req, resp); 
    } 
    } 
+0

感謝馬諾洛對於這樣的想法,我實際使用吉斯,也許我可以試試 – xybrek 2013-04-07 11:32:32

+0

當然,如果你有REG-EXP的問題,我可以舉個手。 – 2013-04-07 11:34:50

+0

當然,我已經更新我的問題,我已經到目前爲止的Guice配置 – xybrek 2013-04-07 11:45:43