0

Problem is common, I've tried a lot of solutions but nothing works for me. I am quite new in Spring so I may not understand some things.

I've got next files structure:

Files structure of project

My link inside page is templated by Thymeleaf like this:

<link rel="stylesheet" th:href="@{css/bootstrap.css}" type="text/css"/>

Also, there is attribute in head of html.

Ok, and my spring-context.xml is:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--to pick up all annotation in the package-->
    <context:component-scan base-package="langquiz"/>

    <mvc:annotation-driven />
    <mvc:resources mapping="/css/**" location="/css/"/>
</beans>

I've tried some manipulations with mapping (like writing location="/templates/css/" or location="css/"), also tried resource handler in configuration class. But nothing helps.

Thank you!

UPD: Changed

<mvc:resources mapping="/css/**" location="/css/"/>

to

<mvc:resources mapping="/css/**" location="/templates/"/>

and also tried

<mvc:resources mapping="/css/**" location="/"/>

but still have no result.

2
  • So you're not using Spring Boot? Commented Mar 26, 2017 at 21:27
  • @chrylis yes, it's Spring-MVC + ThymeLeaf Commented Mar 26, 2017 at 21:59

3 Answers 3

1

Use

<mvc:resources mapping="/css/**" location="/templates/css/"/>

Or adding a resource handler,

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**").addResourceLocations("/templates/css/");
}

}

Sign up to request clarification or add additional context in comments.

Comments

1

Your mapping is incorrect in which location should include your templates directory as well as shown below:

<mvc:resources mapping="/css/**" location="/templates/css/"/>

You can refer here from the spring doc on this:

For example, to serve resource requests with a URL pattern of /resources/ from a public-resources directory within the web application root you would use:

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

2 Comments

what else can I be missing?
Is there ability to check where resources are tried to e taken?
0

Changed to to make it work.

Still wondering if there is a way to see web site not on http://localhost:8080/project-name-snapshot but on http://localhost:8080

2 Comments

Deploy your project as ROOT, that is the easiest
@SAP is there a way to do it with GlassFish?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.