Spring MVC Web 引入靜態文件

web.xml配置:

<servlet>  
    <servlet-name>springmvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:springmvc-servlet.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
 </servlet>  

 <servlet-mapping>  
    <servlet-name>springmvc</servlet-name>  
    <url-pattern>/*</url-pattern>  
 </servlet-mapping>  

springmvc-servlet.xml中配置:

    <mvc:annotation-driven />
    //放置js
    <mvc:resources location="/WEB-INF/resources/js/" mapping="/js/**"/>
    //放置css
    <mvc:resources location="/WEB-INF/resources/css/" mapping="/css/**"/>
    //放置圖片
    <mvc:resources location="/WEB-INF/resources/image/" mapping="/image/**"/>

項目目錄:

這裏寫圖片描述

Controller中:

@RequestMapping(value = "/login")
@Controller
public class LoginController {
    @RequestMapping(value = "/getPage")
    public String getPage() {
        return "login";
    }
    }

可以看到目前的路徑是/login/getPage,那麼在jsp文件中就是以/login爲根目錄,所以我們需要利用 .. 返回上一級,以此類推其餘多級路徑情況。

jsp中資源路徑寫法:

    <link href="../css/login.css" rel="stylesheet">
    <link href="../css/weui.min.css" rel="stylesheet">
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章