0

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.

CODI
  • 147
  • 4
  • 17

1 Answers1

1

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>
smoggers
  • 3,154
  • 6
  • 26
  • 38
  • smoggers, I have a question to ask you. Is the header fine the way it is? The image now loads fine but the css is not showing up. Thanks – CODI Sep 01 '15 at 19:30
  • hmm that's interesting, is the JavaScript taking effect also? for the record this is how I load my css file within the tags of my jsps: if both the css and js are not taking effect then try removing the 2 in the web.xml as images is not mapped and seems to be resolving fine without mapping – smoggers Sep 01 '15 at 20:16
  • The Javascript is working fine, just the css. On my application, it's closed properly – CODI Sep 01 '15 at 20:42
  • can you try with ?? because I don't see why the .js and images would successfully be loaded and .css is not – smoggers Sep 01 '15 at 20:54
  • It's working fine now with your suggestions. Let me just say that I removed the servlet mapping and added like you suggested for it to start working fine. I have no idea why it would not work properly with the servlet-mapping. Any explanation? Thanks – CODI Sep 01 '15 at 22:15
  • I think this is to do with what version of spring you use but I'm not entirely sure. I use spring 3.2.3 and use both pageContext.request.contextPath and the servlet-mapping tags and works well – smoggers Sep 01 '15 at 22:27
  • Ok, I will look into it more. Thanks – CODI Sep 01 '15 at 22:30
  • Hi smoggers, after looking into the last comment, I had to edit my code so that the jsps in the WebContent and in the pages worked together. Could you take a look at the new update I made to the question? Thanks – CODI Sep 06 '15 at 15:19
  • you changed to src="${pageContext.request.contextPath}/static/resources/subsources/images/Slide3.JPG" but you don't have a folder named 'static' judging by your project structure ? – smoggers Sep 06 '15 at 15:23
  • static was created as a path in web.xml. Other jsp files that I included in homepage.jsp picked it up along with the css that went with it. I tried it without "/static/" in and it still did not pick it. – CODI Sep 06 '15 at 15:33
  • 'created as a path in web.xml' sorry I don't understand this statement. the 'pageContext.request' should replicate your project structure, you should be making sure the project structure matches this I'm not sure what you mean by modifying the web.xml – smoggers Sep 06 '15 at 15:41
  • Kindly see this link: [link](http://stackoverflow.com/questions/1483063/spring-mvc-3-and-handling-static-content-am-i-missing-something). I used the comment made by nickdos – CODI Sep 06 '15 at 15:53
  • he says 'The only change I made to JSPs was to add the /static/ path to URLs for CSS, JS and images. E.g. "${pageContext.request.contextPath}/static/css/screen.css".' so he has made this change because his folder structure is WebContent/static/css/screen.css. I've never used org.springframework.js.resource.ResourceServlet that you are using now so I'm not sure how I can help now as you are using this with ResourceServlet /static/*. So fair enough I've helped you out in some ways but not provided a 100% solution – smoggers Sep 06 '15 at 16:00
  • Adding static folder to match "/static/resources/subsources" did not work and also made the header section not to be recognized. – CODI Sep 06 '15 at 16:18
  • Your now using the resource servlet however whereas my projects only use the dispatcher servlet so hopefully someone else who uses the Resource servlet will be able to advise you – smoggers Sep 06 '15 at 16:21
  • 1
    I've just been looking at that answer you linked to again and I'm not sure using ResourceServlet in the web.xml is ideal for your project. if you look at the documentation for org.springframework.js.resource.ResourceServlet it is actually meant to be used for javascript/ajax purposes, see here: http://docs.spring.io/spring-webflow/docs/current-SNAPSHOT/reference/html/spring-js.html. I still think you should remove the ResourceServlet approach from web.xml and stick to using just the dispatcher servlet – smoggers Sep 06 '15 at 17:45
  • I finally got it working. Your insistence on what path to take was what made me look through my code again. I did not use the resourceServlet anymore and also removed resources/subsources everywhere in my code. This actually gave me what I had been battling with for almost 1 week. Thank you so much. – CODI Sep 06 '15 at 18:45