【问题标题】:Mapping Resources in dispatcher servlet (Resin Server)在调度程序 servlet (Resin Server) 中映射资源
【发布时间】:2015-06-12 23:34:06
【问题描述】:

这是我的 dispacter-servlet.xml 文件。当我在resin pro 4.0.36上部署项目时,它会加载我的索引页面和内容,但无法加载存储在staticresources文件夹下的css文件和图像

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.dogears" />


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<import resource="classpath:beans.xml"/>

<mvc:resources mapping="/resources/**" location="/staticresources/" />
<mvc:annotation-driven />

谁能告诉我如何映射我的静态资源文件夹,以便每当请求是模式 /resources/* 时,它将请求重定向到静态资源文件夹。 staticresources 文件夹位于 MyspringProject/src/main/webapps 目录

【问题讨论】:

标签: java maven spring-mvc resin


【解决方案1】:

我相信您需要将您的 staticresources 目录映射为根目录。在 Eclipse 中这将是一个“源文件夹”,而在 IntelliJ 中则是一个“资源根”。

请参阅以下帮助文档,具体取决于您的 IDE:

Eclipse:http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-wizard-source-folder.htm

IntelliJ:https://www.jetbrains.com/webstorm/help/configuring-folders-within-a-content-root.html

【讨论】:

    【解决方案2】:

    假设你的项目目录结构是这样的

    - src
        - main
          - webapp
            - resources
              - css
                - abc.css
              - images
                - xyz.jpg
    

    &您的.html.jsp 页面位于以下目录中

    - webapp
      - index.jsp
      - pages
        - welcome.jsp
    

    然后您可以在 index.jsp 页面中添加指向您的资源的链接,网址为 BaseURL/index.jsp

    <link href="resources/css/abc.css" rel="stylesheet" type="text/css" />  
    
    <body>
        <img src="resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
    </body>
    

    & 到 welcome.jsp 与 URL BaseURL/pages/welcome.jsp 作为

    <link href="../resources/css/abc.css" rel="stylesheet" type="text/css" /> 
    
    <body>
        <img src="../resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
    </body>
    

    希望对你有所帮助。

    【讨论】:

    • 仅 mvc:resource 映射无法识别。所以当我调用资源时,它并不指静态资源文件夹。
    • 我没明白你的意思?你能详细说明一下吗?
    • 意味着即使我已将所有资源请求映射到静态资源文件夹,树脂服务器也不会将请求重定向到静态资源文件夹。
    • 因此它不会加载css和js文件
    【解决方案3】:

    我终于找到了我的解决方案。我已经在resin目录的webapps文件夹中部署了我的spring webapp。

    我意识到&lt;mvc:resource /&gt; 映射标记不起作用,当您将 spring 应用程序部署在 resin 服务器 而不是 tomcat 上时。

    因此我解决了这个问题,首先为我的项目创建一个war文件,然后在桌面上提取war文件,然后将war文件中的所有内容放在webapps/root中(而不是webapps 文件夹)resin 目录的文件夹。然后在我的索引页面中,我使用 JSTL TAG 来包含外部样式表。

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
    <head>
    
    <link rel="stylesheet" type="text/css" href="<c:url  value="/staticresources/externalcss.css"/>">
    
    </head>
    <body>
    <h2 class="text_style">Hello World!</h2>
    </body>
    </html>
    

    它工作!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 2016-08-02
      • 2016-03-12
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多