Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

前端 未结 9 1332
小蘑菇
小蘑菇 2020-11-21 05:43

I\'m having trouble with loading CSS and images and creating links to other pages when I have a servlet forward to a JSP. Specifically, when I set my

相关标签:
9条回答
  • 2020-11-21 05:53

    You can try out this one as well as. Because this worked for me and it's simple.

    <style>
        <%@ include file="/css/style.css" %>
    </style>
    
    0 讨论(0)
  • 2020-11-21 05:54

    You must analyse the actual HTML output, for the hint.

    By giving the path like this means "from current location", on the other hand if you start with a / that would mean "from the context".

    0 讨论(0)
  • 2020-11-21 05:54

    If you are using Spring MVC, then you need to declare default action servlet for static contents. Add the following entries in spring-action-servlet.xml. It worked for me.

    NOTE: keep all the static contents outside WEB-INF.

    <!-- Enable annotation-based controllers using @Controller annotations -->
    <bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="0" />
    </bean>
    
    <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="order" value="1" />
    </bean>
    
    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
    0 讨论(0)
  • 2020-11-21 05:59

    Your welcome page is set as That Servlet . So all css , images path should be given relative to that servlet DIR . which is a bad idea ! why do you need the servlet as a home page ? set .jsp as index page and redirect to any page from there ?

    are you trying to populate any fields from db is that why you are using servlet ?

    0 讨论(0)
  • 2020-11-21 06:05

    Below code worked for me.

    instead of use <%@ include file="styles/default.css"%>

    0 讨论(0)
  • 2020-11-21 06:09

    I faced similar issue with Spring MVC application. I used < mvc:resources > tag to resolve this issue.

    Please find the following link having more details.

    http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/

    0 讨论(0)
提交回复
热议问题