0

Today i have a problem with Bootstrap in my Spring Boot app.

Spefically, when i start the app and open the browser, i can see my page but with "classic" html; that is, i don't see the page formatted with the css of Bootstrap but with normal html5 (so, Bootstrap is nota loaded).

What i have done?

First, i've download Boostrap framework; then i try to put the 3 folder css, js and fonts under both this path:

src/main/webapp          (first)
src/main/resource/static (then)

In the second case, i tried also to move from /static folder into resource, that is src/main/resource

When i've put this 3 folder in resource and resource/static, i tried 2 configuration:

  1. add in application.properties file this line of code: spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

  2. Use a MvcConfiguration class that extends the WebMvcConfigurerAdapter class and override the method addResourceHandlers this way:

    public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/"); }

Of course, done this, in my jsp file (located under src/main/webapp/WEB-INF/jsp) i'v put the 2 line of code to tell the page where take the css and js file:

<link href="/resources/static/css/bootstrap.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="/resources/static/js/bootstrap.min.js"></script>

and of course, these line change from /resource/static/ with /webapp when i try to put under src/main/webapp the 3 Bootstrap folder. But, after this, i cannot see Bootstrap loaded. What i wrong?

Update: solved. The problem was based on a setting in my applications.properties:

server.port=8080 server.contextPath=/polaris

that is, the change of the context path. A usefull tread on this site is this: Spring 4 - addResourceHandlers not resolving the static resources

Luca Sepe
  • 733
  • 4
  • 19
  • 30
  • have you tried to clear your cache / different browsers between attempts? – yardie May 04 '17 at 15:26
  • Yes, both them; clear the cache was the first thing I thinked. – Luca Sepe May 04 '17 at 15:29
  • Did you try checking with inspect element of browser that your `bootstrap-css` is loading as part of resources? – Sajjad May 04 '17 at 15:37
  • Dont use jsp's anymore, they are outdated relicts of Java technology ;) – Stimpson Cat May 04 '17 at 15:38
  • Stimpson, you are right but my boss want that i use them. Sajjad, no i've not try; and i admit my ignorance. What i must find inspecting the page? And using firefox, wich option i must choose after right click of mouse? Page source? Page analisys? Page Information? – Luca Sepe May 04 '17 at 15:43
  • Try to inspect your developer console in browser. Cntrl + Shift + K in firefox, Cntrl +Shft + I in Chrome. You can see console, network tabs to find out where your browser is loading there resources from. If its a 404 error there then the file may not be there on server. – Praneeth Ramesh May 04 '17 at 17:08
  • Thanks a lot Praneeth. Tomorrow i will try. – Luca Sepe May 04 '17 at 17:51

2 Answers2

0

Using in JSP by link href and give path like static/css/somename.css

Do 4 Step: 1.maven-Clean 2.update project 3.maven install 4.Run As Spring Boot App

Hope it will help. The Path Of Any CSS Should be Like This in Spring Boot

  • Ok, i tried both suggestion of Rajesh and Praaneth and the problem persist but now i know why: as Praanet suppose, i have a 404 error and it's impossible to load file. So, i can continue from this. Tank you very much, both you. – Luca Sepe May 05 '17 at 08:02
0

You can refer following link for spring boot with boostrap template on web application. example_link. You have to change following property values on application.properties file inside the above application for jsp page load. Make sure inside webapp folder having correct name.

  • spring.mvc.view.prefix=/html/
  • spring.mvc.view.suffix=.html
Thusitha Indunil
  • 832
  • 1
  • 8
  • 22