0

My jsp loads some additional file like css and js files like almost every website does with something like this:

<script src="js/dhtmlx/dhtmlx.js"></script>

The problem now is, that Spring wants to dispatch the request and isn't finding a proper handler for it, which is right. I tried the following in my config-class:

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

which is not working, spring still wants to dispatch the request:

org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [...URI/js/dhtmlx/dhtmlx.css] in DispatcherServlet with name 'keza'

How can I tell spring that this is not a request which the framework should dispatch with the dispatcherServlet?

3
  • You have "js/..." not "/js/..." or "/context/js/...". Commented Jul 15, 2015 at 12:50
  • Thank you for the hint! I changed it - but still the same error. Commented Jul 15, 2015 at 12:53
  • Could you show the class declaration in which you override addResourceHandlers, it looks like it was ignored by Spring ... and what exactly is the error when you use src="/js/... ? Commented Jul 15, 2015 at 14:46

2 Answers 2

1

make sure your js folder is available under src\main\webapp\resources

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

9 Comments

WebContent/resources/js/... ? Is that okay? I would change it to that
that's fine now try to access it like @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } also change <script src="js/dhtmlx/dhtmlx.js"></script> to <script src="resources/js/dhtmlx/dhtmlx.js"></script>
can you share your directory structure
Not able to access image using above link. It shows message image is deleted or moved.
|
1

1.Using mvc resources

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

This enables serving any static content whose location can be specified as a Spring Resource

2. Using mvc:default-servlet-handler:

<mvc:default-servlet-handler />

This provides a way to serve out the static content from the root of the web application even though the Dispatcher Servlet is registered at /, the details of how Spring does this is available at the Spring documentation site here - in brief the responsibility is delegated to the containers default servlet.

MVC resources using spring annotations

@Configuration  
@EnableWebMvc  
public class WebAppConfig extends WebMvcConfigurerAdapter {  

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

5 Comments

should I add this in the dispatcher Servlet?
yup, you need to add this to your dispatcher-servlet.xml file.
i dont have a .xml-servlet anymore. That's what my question is about: How to express it in a config-class
I have added class configuration to above answer
That's exactly how i tried it. Doesn't work, plus i need to add @ComponentScan to the config class, otherwise i get multiple exceptions regarding instatiating beans

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.