11

我想使用 JSP 页面中的一些静态资源(图像、js、css)作为 Viewable

我不知道如何使用 WebPageContentRegex

如果我使用servlet,我可以执行资源,但我不能访问静态文件
如果我使用过滤器,我可以访问静态文件,但我不能执行资源

你能帮我访问这两个吗?

这是我的文件:

webapp
  |____ resources
  |       |____ css
  |       |____ images
  |       |____ js
  |
  |____ WEB-INF
          |____ jsp

这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>test</display-name>

<servlet>
<servlet-name>REST</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>my packages</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
  <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
  <param-value>/WEB-INF/jsp</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
  <param-value>(/resources/(css|images|js)/.*)|(/WEB-INF/jsp/.*\.jsp)</param-value>
</init-param>
</servlet>
<servlet-mapping>
  <servlet-name>REST</servlet-name>
  <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

</webapp>
4

1 回答 1

2

你能确认你在 jersey 1.x 吗?如果您使用的是 2.x,则应将 com.sun.jersey.config.property.WebPageContentRegex 替换为 jersey.config.servlet.filter.staticContentRegex

请问您也可以简化您的正则表达式,第一次只提供 *.html 吗?(可能是正则表达式的问题!)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>test</display-name>

    <servlet>
        <servlet-name>REST</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>my packages</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/jsp/*.jsp</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/resources/*.html </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

</webapp>
于 2014-06-03T01:44:01.623 回答