将外部资源添加到GWT开发模式服务器(Jetty)

时间:2012-01-24 19:58:08

标签: gwt servlets jetty

我有一个需要访问一些静态资源的GWT。我不想将这些资源放在实际的/ src / main / webapp目录中(注意:使用Maven和相应的布局),因为它们不是应用程序的一部分,而是数据的一部分。

我的问题不是如何在部署时让应用程序访问这些资源;通过Tomcat配置应该很容易做到。相反,问题是如何在开发期间让我的应用程序访问某些测试数据。我在GWT应用树之外的目录中有一些测试静态文件,并且想要配置GWT的dev模式Jetty服务器来访问这些资源。也就是说,我希望能够说mvn gwt:run(或者从Eclipse以dev模式运行或调试应用程序),并让服务器提供这些静态资源。

我知道(并使用)开发模式-noserver选项。但是,由于我正在修改服务器端代码以及客户端代码,因此每次服务器代码更改时重新部署服务器都是不切实际的。

到目前为止,我一直在尝试在WEB-INF目录中创建一个jetty-web.xml文件,并添加一个新的上下文,以便服务器为我的资源提供服务。这是我失败的jetty-web.xml尝试:

<Configure id="parentHandler" class="org.mortbay.jetty.handler.ContextHandler">
    <Set name="contextPath">/static_resources</Set>
    <Set name="resourceBase">/real/filesystem/path/to/static/resources/</Set>
    <Set name="handler">
    <New class="org.mortbay.jetty.handler.ResourceHandler">
        <Set name="cacheControl">max-age=3600,public</Set>
    </New>
    </Set>
</Configure>

如果我把它放在jetty-web.xml中,那么当我启动开发模式时,Jetty会提供我的外部资源,但不会为我的GWT应用程序提供服务。

另一方面,如果我这样做......

<Configure id="parentHandler" class="org.mortbay.jetty.handler.ContextHandler">
    <New id="newHandler" class="org.mortbay.jetty.handler.ContextHandler" >
        <Set name="contextPath">/static_resources</Set>
        <Set name="resourceBase">/real/filesystem/path/to/static/resources/</Set>
        <Set name="handler">
            <New class="org.mortbay.jetty.handler.ResourceHandler">
                <Set name="cacheControl">max-age=3600,public</Set>
            </New>
        </Set>
    </New>
    <Get id="server" name="server">
        <Call name="setHandler">
            <Arg><Ref id="newHandler" /></Arg>
        </Call>
    </Get>
    <Ref id="newHandler">
        <Set name="server"><Ref id="server" /></Set>
    </Ref>
</Configure>

... Jetty既不提供我的静态内容也不提供我的GWT应用程序。

有什么建议吗?我做错了什么?

(注意:GWT开发模式使用Jetty的版本6 [或6.something],并以编程方式设置它,所以据我所知,没有读入其他Jetty配置文件。)

非常感谢!

2 个答案:

答案 0 :(得分:0)

我喜欢使用的方法是编写一个简单的Servlet,它可以传输任何文件,类似于:http://www.exampledepot.com/egs/javax.servlet/GetImage.html,并将它在我的web.xml中映射到某些URL模式。

然后我可以轻松地通过我的一个配置文件使静态文件的基本目录可配置。我也可以做任意的事情,比如将几个目录混合在一起,或从数据库中获取文件内容......

我更喜欢这种方法,因为它适用于任何环境中的所有servlet容器。

答案 1 :(得分:-1)

我的工作:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
        <New id="sampleDS" class="org.mortbay.jetty.plus.naming.Resource">            
            <Arg>java:jboss/datasources/sampleDS</Arg>
            <Arg>
                 <New class="oracle.jdbc.pool.OracleConnectionPoolDataSource">
                 <Set name="URL">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</Set>                 
                 <Set name="User">user</Set>
                 <Set name="Password">pass</Set>
                 </New>
            </Arg>
        </New>
</Configure>
  1. 将其置于WEB-INF
  2. 下载[jetty-naming-6.1.12.jar][1][jetty-plus-6.1.12.jar][2]并将其放在`WEB-INF \ lib'
  3. 重新启动eclipse并再次运行应该这样做。