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

标签 java apache tomcat spring-mvc spring-security

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

示例:我希望能够放置 (/images) 而不是必须放置 (resources/admin/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>

最佳答案

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

转发应该是这样的:

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

关于java - 如何在 web.xml 中设置资源的根路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753684/

相关文章:

java - 合并两个 Eclipse 插件

java - 如何解决 "add all in list cannot be applied to"

java - 类/监听器之间的通信

php - 如何在 PHP 中解析 A​​pache 的错误日志?

java - 根据 Tomcat 中的请求检查用户

tomcat - logback 的默认输出目录

java - Nexus 3 Rest api 检查组件是否存在

php - Laravel POST 输入有限制吗?

apache - mod_ldap Apache错误处理服务器错误500

tomcat - 使用 tomcat-maven-plugin 部署预构建的 WAR