【问题标题】:how to set root path for resources in web.xml如何在 web.xml 中设置资源的根路径
【发布时间】:2013-03-23 03:09:56
【问题描述】:

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

示例:我不想放置 (resources/admin/images) 的图像路径,而是希望能够放置 (/images)

文件结构如下:

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

我有一个根范围,它是我的站点 (localhost.com) 的基础

我尝试搜索,但不确定要添加到我的 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>

【问题讨论】:

  • 你应该用更多细节来改进你的问题。例如,第二个 servlet 映射在哪里?并添加 java 标签,让更多人看到你的问题。
  • @DiogoSantana 我添加了整个 web.xml。
  • “我的目标是不必将整个路径放在我的 JSP 中”:哪个完整路径?你的意思是?你的问题到底是什么?
  • @DiogoSantana 我添加了一些说明。
  • 您想这样做是因为只有管理员才能访问这些资源?

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


【解决方案1】:

如果您不想暴露完整的文件路径,请将 jsp 文件放在 WEB-INF 文件夹中。虽然不能直接访问WEB-INF文件夹中的文件,但是可以转发请求给它。

前锋应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 2013-02-09
    • 1970-01-01
    • 2014-02-09
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多