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.

0

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

17 Comments

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
hmm that's interesting, is the JavaScript taking effect also? for the record this is how I load my css file within the <head> tags of my jsps: <link href="${pageContext.request.contextPath}/static/css/main.css" rel="stylesheet" type="text/css"> if both the css and js are not taking effect then try removing the 2 <servlet-mapping> in the web.xml as images is not mapped and seems to be resolving fine without mapping
The Javascript is working fine, just the css. On my application, it's closed properly
can you try with <link href="${pageContext.request.contextPath}/resources/subsources/CSSFolder/stylesheet.css" rel="stylesheet" type="text/css"> ?? because I don't see why the .js and images would successfully be loaded and .css is not
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: docs.spring.io/spring-webflow/docs/current-SNAPSHOT/reference/…. I still think you should remove the ResourceServlet approach from web.xml and stick to <img src="${pageContext.request.contextPath}/resources/subsources/images/Slide3.JPG"/> using just the dispatcher servlet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.