简体   繁体   中英

Unable to load CSS in Spring Mavenized project

I have a problem with CSS path in my project. This is the file structure of a Spring Maven project.

http://i.stack.imgur.com/kbzpf.jpg

In jsp page I wrote:

<link rel="stylesheet" href="/Treninky/css/bootstrap.css" />

But it still does not work. I can't find a solution. Could anyone help me?

Resources under WEB-INF directory are not visible outside the application. You should have your /css directory straight under your webapp directory in order for it to be visible.

Something like

src/
   /main
        /webapp
               /Treninky
                        /css/*.css

And then your /css directory would be reachable

add this to your web.xml

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

and it's better to move your css to another directory like src/main/resources or somewhere that not in WEB-INF

I'd suggest you to put your css file under css folder inside src folder:

Treninky >
         src >
              css/bootstrap.css   

Then you can use:

<link rel="stylesheet" href="/Treninky/src/css/bootstrap.css" />

Flelix good advice
Best practice to get path of any folder from the project is: (as per flelix advice structure),

<LINK REL="SHORTCUT ICON" HREF="<%=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()%>/src/css/bootstrap.css">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM