1

I've checked around for this problem, but after 4 hours of trying many things, nothing worked for me.

I get a 405 error when trying to access my css file. Here are my Config.java

package com.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.myapp")
@EnableWebMvc
@EnableTransactionManagement
public class Config extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/","/css/","/js/");

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

and directories structure:

META-INF
WEB-INF
resources
    |-css
    |  |-myapp.css
    |-js
       |-myapp.js

Using netbeans as IDE. I've also try to out the resources directory in WEB-INF. Same results.

Edit: my default controller below

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {
    map.put("msg", "Hello Spring 4 Web MVC!");
    return "index";
}

Edit 2: here is my WebinItalizer

public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {        
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        ctx.register(Config.class);  
        ctx.setServletContext(servletContext);    
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}

in this initializer, if I use "/" for servlet.addMapping("/") I can not have access to the css. But if I change it to something else like servlet.addMapping("/app") I can have access to the css. But the problem if I do this is all my url will start by app/ not very cool :/

Rony
  • 101
  • 1
  • 8
  • What the URL of the file that's giving the 405? – Christopher Schneider Feb 10 '17 at 14:53
  • localhost:8084/myapp/resources/css/myapp.css – Rony Feb 10 '17 at 15:08
  • and the resources directory is in the root of the war with all my css and js files – Rony Feb 10 '17 at 15:13
  • `localhost:8084/myapp/resources/css/myapp.css` <-- At least, you are wrong when call static resources. It should like that `http://ocalhost:8084/myapp/css/myapp.css` – Vy Do Feb 10 '17 at 16:32
  • Yes I tried ll of these (/myapp/myapp.css, /myapp/css/myapp.css), and still get the 405 error. But I have this default controller (I've edited my original post) do you think my default controller can prevent me to access my css? – Rony Feb 10 '17 at 17:02
  • I've updated with my WebInitializer. I don't use Spring boot, I use Spring 4. I don't have any xml in my app, the is not other config file beside these. – Rony Feb 10 '17 at 19:47
  • Shouldn't the arguments to `addResourceLocations` read `"/resources/css/","/resources/js/"`? – crizzis Feb 10 '17 at 19:48
  • @crizzis I tried this as well. But I definitely think it's the servlet.addMapping("/") in my WebInitializer blocks all request to the css. I don't know why my resourceHandler cannot resolve the path :/ – Rony Feb 10 '17 at 19:51
  • @ChristopherSchneider if you have a solution, post it anyway I'll do it because I cannot go one without this or I'll have to do a dirty hack. But one question, is this normal with Srping? I've seen many people running to this problem, but I feel like I'm the only one who cannot solve it :/ – Rony Feb 10 '17 at 19:57
  • Spring has a pretty steep learning curve, if that's what you're asking, and it's pretty common to get stuck. It took me several months of working with it almost full time to really become proficient. – Christopher Schneider Feb 10 '17 at 20:38
  • I put a bit of effort into my answer. Did it answer your question? I can also attempt to clarify if something didn't make sense. – Christopher Schneider Feb 12 '17 at 03:37
  • @ChristopherSchneider Sorry for late reply, I was trying new things yesterday in order to make it work. The only thing I found was to add all controllers I have in the WebInitializer, like this servlet.addMapping("/login"); servlet.addMapping("/register"); servlet.addMapping("/test"); I don't know if this is common for Srping. The only problem it now http://localhost:8084/myapp/ leads to a 404. – Rony Feb 12 '17 at 07:55

2 Answers2

5

First, if this is incorrect, sorry. I do a lot of Spring, but it's typically only XML configuration.

Let's start here:

     registry.addResourceHandler("/resources/**")
             .addResourceLocations("/resources/","/css/","/js/");

This is saying "when I request something with the path http://localhost/resources/** look in /resources/, /css/ or /js/.

Well, /css/ and /js/ don't exist. Only /resources/ does.

In this case, you should map this

registry.addResourceHandler("/resources/**")
                 .addResourceLocations("/resources/");

And access this way:

<script src="/resources/js/myapp.js"></script>

Alternately, you could just do this:

registry.addResourceHandler("/js/**")
                 .addResourceLocations("/resources/js/");
registry.addResourceHandler("/css/**")
                 .addResourceLocations("/resources/css/");

and access the static content this way:

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

As for why changing the context root worked, it's due to this:

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

This attempts to route static content requests through the default servlet with the context root /. Since you're creating your DispatcherServlet at /, the default servlet will never be hit since it's set to the lowest priority by default.

If you're using registry.addResourceHandler you do not need to configure the default servlet and vice versa. As you discovered, you never hit it as by default it's the lowest priority.

If you still have issues, even after fixing the ResourceHandler, you can try omitting the resource handler configuration completely, or setting the default servlet to a higher priority (Which I wouldn't recommend).

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • Hi Thanks for your answer. It didn't work, I've uploaded my to github https://github.com/ronyyu/myapp so you can see better if I made a mistake somehere. – Rony Feb 12 '17 at 07:53
  • I followed this tutorial to build my app http://outbottle.com/spring-4-web-mvc-hello-world-using-annotation-configuration-with-netbeans/ – Rony Feb 12 '17 at 08:57
  • Sorry I never responded. Just didn't have the time. From the look of things, I would guess your resources folder is in the wrong location. You've got it in `src/main/webapp/resources`. The standard is generally `src/main/resources`. – Christopher Schneider Mar 08 '17 at 14:33
0

The following solution worked for me. I added resource handlers to my configuration class which extends WebMvcConfigurerAdapter.

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

Then, I imported my bootstrap.min.js file in my jsp as shown below :

<script type="text/javascript" src="<c:url value="/bootstrap/js/bootstrap.min.js"/>" ></script>

Note that bootstrap folder specified in the Resourcehandler resides under WebContent folder.

Edit :

After looking at your code in GitHub, I can clearly see that your resource handler has an extra * in it. It should look like below :

Clearly i don't see that you used <c:url> in your jsp. Do the following things and see if it works :

  1. Remove the extra * from resource handler to look like below :

registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

  1. In your index.jsp file, change the link and script tags to look like this:

<script type="text/javascript" src="<c:url value="/resources/js/js.js"/>" ></script>
<link rel="stylesheet" href="<c:url value="/resources/css/site.css"/>"  type="text/css">

Do not use context path ${cp} for script and link tags anymore. <c:url> is the reason this worked for me.

Ashish
  • 117
  • 1
  • 13
  • I tried this as well. It didn't work for me. You can find my source here: https://github.com/ronyyu/myapp/ – Rony Feb 14 '17 at 21:03
  • Yep! I've already tried to remove the extra '*' but it still doesn't work. I've put the xtra '*' because I was desperate and tried everything. I can not have access to the css still. It's weird, because I followed everything and my config seems right. The only way I can have access to css/js is to put all my controller into the webinitializer like this: servlet.addMapping("/login"); servlet.addMapping("/register"); but the problem is I don't have access to / anymore. – Rony Feb 15 '17 at 06:05