Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 - addResourceHandlers not resolving the static resources

My maven spring project directory structure is shown below. I am using Spring-4 annotation based configuration. I configure the resources like below. I tried many ways that are suggested in many Stackoverflow questions and other websites

Spring 4 loading static resources

http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4

But the jsp files could not load the resources, all the static content requests returns 404 error. I tried these things in jsp,

 <link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">  <link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">  <link href="css/bootstrap.css" rel="stylesheet" media="screen"> 

EDIT: I am using servlet 2.5 because as of now I cannot upgrade my project from JBoss 5 to higher versions. JBoss5 do not support servlets 3, and do that matter?

@Configuration @ComponentScan("com.mgage.mvoice") public class MyAppWebConfig extends WebMvcConfigurerAdapter {      public void addResourceHandlers(ResourceHandlerRegistry registry) {           // I tried these many combinations separately.          ResourceHandlerRegistration resourceRegistration = registry             .addResourceHandler("resources/**");         resourceRegistration.addResourceLocations("/resources/**");         registry.addResourceHandler("/css/**").addResourceLocations("/css/**");         registry.addResourceHandler("/img/**").addResourceLocations("/img/**");         registry.addResourceHandler("/js/**").addResourceLocations("/js/**");         registry.addResourceHandler("/resources/**")                 .addResourceLocations("classpath:/resources/");                // do the classpath works with the directory under webapp?      }  } 

Project structure

like image 820
Vijay Veeraraghavan Avatar asked Jul 31 '14 14:07

Vijay Veeraraghavan


1 Answers

this worked,

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

and in the jsp files I referred to the static resources like

<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen"> 
like image 141
Vijay Veeraraghavan Avatar answered Sep 28 '22 00:09

Vijay Veeraraghavan