Spring MVC - include static files / javascript , css

后端 未结 7 1996
猫巷女王i
猫巷女王i 2021-01-02 06:48

I have created MVC application.

I want to include js or css file into jsp.

My static files ar under:

- webapp
        -js/jquery.js
        -WEB-I         


        
相关标签:
7条回答
  • 2021-01-02 07:19

    add this to you confing and modify the location according to your need.

      <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
    

    see this How to handle static content in Spring MVC?

    0 讨论(0)
  • 2021-01-02 07:23

    Change your DispatcherServlet mapping to e.g:

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    

    Or some other not conflicting url-pattern like *.htm or /controllers/*. Remember that from now on all your controllers will be available only through this pattern.

    Now it is intercepting everything in your web application, including .js files, images, etc.

    The same thing with hibernateFilter - you don't really need an open Hibernate session when fetching .js files, don't you?

    0 讨论(0)
  • 2021-01-02 07:28

    add mvc:resources in your config file(*-servlet.xml), you can find it works

    0 讨论(0)
  • 2021-01-02 07:38

    I agree with your answer. But in the style.css file declare url that relate to path of image.

    --style.css--

    .cwt-object0
    {
        display: block;
        left: 2.62%;
        margin-left: -1px;
        position: absolute;
        top: 43px;
        width: 64px;
        height: 64px;
        background-image: url('/resources/images/object0.png');
        background-position: 0 0;
        background-repeat: no-repeat;
        z-index: 0;
    }
    

    How to use tag <spring:url></spring:url> in style.css file to see in browser IE/Firefox

    --jsp file ---

    <link href="<spring:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" media="screen">
    
    0 讨论(0)
  • 2021-01-02 07:39

    Why not use the simple jsp core ?

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
    <link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" />
    
    0 讨论(0)
  • 2021-01-02 07:39

    I just followed Mkyong Tutorial to place css, js, jquery & image files. Its working for me.

    In servlet-context.xml

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
            up static resources in the ${webappRoot}/resources directory -->
        <resources mapping="/resources/**" location="/resources/assets/" />
    

    In JSP , import tag library

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    

    and add like

    <link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">
    
    0 讨论(0)
提交回复
热议问题