CODI
CODI

Reputation: 173

Spring not recognizing image and css

Previous issue:

I created a webpage named homepage.jsp without the Spring application. This page is supposed to be the regular website home page where you can click to different sections like: services, contact and news_blog. The homepage.jsp is in WebContent. It worked perfectly when the application was created without the spring framework.

Now, because I incorporated spring since I would like to add a blog to the new_blog section, the homepage.jsp can no longer see the image, css and javascript.

I have searched the internet and tried several things but no progress yet. This is what I have so far and I would appreciate your help please. Thanks in advance.

Application Structure

I edited the Header from:

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="resources/subsources/CSSFolder/stylesheet.css" /> 
<script src="resources/subsources/JavaScriptFolder/script.js"></script>
<script src="resources/subsources/JavaScriptFolder/crawler.js"></script>

to this:

<link href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link href="${pageContext.request.contextPath}/static/resources/subsources/CSSFolder/stylesheet.css" rel="stylesheet" type="text/css">
<script src="${pageContext.request.contextPath}/static/resources/subsources/JavaScriptFolder/script.js"></script>
<script src="${pageContext.request.contextPath}/static/resources/subsources/JavaScriptFolder/crawler.js" 

I changed Part of the body from:

<div class="homepic">
   <img src="resources/subsources/images/Slide3.JPG" />
</div>

to this:

<div class="homepic">
   <img id="homepic" src="${pageContext.request.contextPath}/static/resources/subsources/images/Slide3.JPG" >
</div>

I added the following to my web.xml:

<servlet>
    <display-name>mvc-dispatcher</display-name>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet>
      <servlet-name>ResourceServlet</servlet-name>
      <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>

    <servlet-mapping>
    <servlet-name>ResourceServlet</servlet-name>
    <url-pattern>/static/*</url-pattern>
  </servlet-mapping>

New Problem: The image is not showing up at all but everything else is fine.

Upvotes: 0

Views: 637

Answers (1)

smoggers
smoggers

Reputation: 3192

In your spring-config.xml add this:

<mvc:resources location="/resources/subsources/" mapping="/resources/**"></mvc:resources>

and then in your web.xml have this:

<servlet>
    <display-name>dispatcher</display-name>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring-config.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

Upvotes: 1

Related Questions