如何在web.xml中设置资源的根路径

时间:2013-04-01 23:33:37

标签: java apache tomcat spring-mvc spring-security

我的web.xml中有两个servlet映射我想为我将为下面的servlet引用的资源设置一个根路径。我的目标是不必将整个路径放在我的JSP中。

示例:而不是必须放置(资源/管理员/图像)的图像路径,我希望能够放(/ images)

文件结构如下:

 |_ admin
    |_index.jsp
    |_resources
         |_images
         |_views
              |_dashboard.jsp (file in which I want to use the scoped file paths)

我有一个根作用域作为我的站点的基础(localhost.com)我想要另一个作为我试图定义的管理员级别的作用域(localhost.com/admin)。

我尝试过搜索,但不确定要添加到我的servlet映射中。下面是我的web.xml

<display-name>cr</display-name>
<description>cr</description>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
        classpath:spring-security.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>cr</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/conf/spring-controllers.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>cr</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <servlet-name>cr</servlet-name>
</filter-mapping>

<servlet>
    <servlet-name>admin</servlet-name>
    <jsp-file>/resources/admin/index.jsp</jsp-file>
</servlet>

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

<session-config>
    <session-timeout>15</session-timeout>
</session-config>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1 个答案:

答案 0 :(得分:0)

如果您不希望公开完整的文件路径,请将jsp文件放在WEB-INF文件夹中。虽然您无法直接访问WEB-INF文件夹中的文件,但您可以将请求转发给它。

前锋应该是这样的:

RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/admin/index.jsp");
view.forward(req, resp);