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


@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

forward is MvcConfig.java. As you can see, i add resourcehandler for static resources.

private void addDispatcherServlet(ServletContext servletContext)
{
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.getEnvironment().addActiveProfile("production");
    applicationContext.register(MvcConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
    dispatcher.setInitParameter("dispatchOptionsRequest", "true");
} 
private void addUtf8CharacterEncodingFilter(ServletContext servletContext)
{
    FilterRegistration.Dynamic filter = servletContext.addFilter("CHARACTER_ENCODING_FILTER", CharacterEncodingFilter.class);
    filter.setInitParameter("encoding", "UTF-8");
    filter.setInitParameter("forceEncoding", "true");
    filter.addMappingForUrlPatterns(null, false, "/*");
}

And there is Initializer.java

There is my resource hierachy.

src
  -main
    --java
    --resources
    --webapp
      ---WEB-INF
         ----resources
             -----css
                  ------ signin.css
         ----views

In index.jsp,I called signin.css like this.

<link href="/resources/css/signin.css"  rel="stylesheet">

Then, i can found these error message.

WARN [2017-03-07 14:37:49] ({http-bio-8080-exec-14} DefaultHandlerExceptionResolver.java[handleHttpRequestMethodNotSupported]:215) - Request method 'GET' not supported
WARN [2017-03-07 14:37:49] ({http-bio-8080-exec-15} DefaultHandlerExceptionResolver.java[handleHttpRequestMethodNotSupported]:215) - Request method 'GET' not supported

In chrome browser, also has 405 error. [405-Error screen shot][1][1]: https://i.sstatic.net/vmDlk.png

How can i fix it?

2
  • FYI, if you're using Boot, you can put those resources under src/main/resources/public (or static) and you don't have to configure any of the resolvers yourself. Commented Mar 7, 2017 at 5:48
  • I'm not using Boot,,, i create MVC project for Spring legacy. Then i changed servlet's config to Java code. Commented Mar 7, 2017 at 6:18

2 Answers 2

1

can you change

<link href="/resources/css/signin.css"  rel="stylesheet">

to

<link href="${pageContext.request.contextPath}/resources/css/signin.css"  rel="stylesheet">
Sign up to request clarification or add additional context in comments.

2 Comments

Have you added this in servlet-context.xml : <resources mapping="/resources/**" location="/resources/" />
Yeah.... i did... As you can see, addResourceHandlers is method which is the same function in servlet-context.xml <resources> tag.
0

try this

registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

Comments

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.