简体   繁体   中英

Mapping Resources in dispatcher servlet (Resin Server)

This is my dispacter-servlet.xml file. When i deploy the project on resin pro 4.0.36 it loads my index page and the content BUT FAILS to load css files and images stored under the staticresources folder

<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 />

Please can anyone tell me how to mapp my static resources folder, so that whenever the request is of the patter /resources/* it redirects the request to the static resources folder. the staticresources folder is in MyspringProject/src/main/webapps directory .

I believe you need to map your staticresources directory as a root directory. In Eclipse that would be a "Source Folder" and in IntelliJ a "Resources Root".

Refer to the following help docs depending on your 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

Suppose your project directory structure is like

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

& your .html or .jsp pages are located in the directory as below

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

then you can add link to your resources in index.jsp page with the URL 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>

& to welcome.jsp with URL BaseURL/pages/welcome.jsp as

<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>

Hope this helps you.

I finally found my solution. I had deployed my spring webapp in the webapps folder of the resin directory.

I realised that <mvc:resource /> mapping tag does not work when you have deployed your spring app on resin server instead of tomcat .

Hence i solved this by first creating a war file of my project and then extracted the war file on the desktop and later placed all the contents from the war file in the webapps/root (and not webapps folder) folder of resin directory. and then from my index page i used JSTL TAG to include the external stylesheet.

<%@ 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>

IT WORKS !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM