2

I'm new in spring mvc, and I have some questions:

1) Can I get access to img, css and js inside WEB-INF directory from jsp page?

Folder structure is:

webapp
  - WEB-INF
     - css
     - js
     - img

2) I have the class WebMvcConfig, extending WebMvcConfigurerAdapter:

@Configuration
@EnableWebMvc
@Import({BeanConfig.class, CacheConfig.class})
public class WebMvcConfig extends WebMvcConfigurerAdapter {  
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/").setCachePeriod(31556926);
        registry.addResourceHandler("/img/**").addResourceLocations("/resources/img/").setCachePeriod(31556926);
        registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/").setCachePeriod(31556926);
    }   

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

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ControllerInterceptor());
    }  

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }    
    @Bean
    public ContentNegotiatingViewResolver getContentNegotiatingViewResolver() {
        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        ArrayList<View> defaultViews = new ArrayList<View>();
        MappingJacksonJsonView jsonView = new MappingJacksonJsonView();
        jsonView.setPrefixJson(true);
        defaultViews.add(new MappingJacksonJsonView());
        resolver.setDefaultViews(defaultViews);
        resolver.setOrder(1);
        return resolver;
    }

    @Bean
    public ControllerInterceptor getLoggingInterceptor() {
        return new ControllerInterceptor();
    }
}

I want to get access to folders: /resources/css, /resources/img and /resources/js,

Part of web.xml file:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            config.WebMvcConfig
        </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Folder structure is:

webapp
   - WEB-INF
   - resources
        - css
        - js
        - img

But it does not worked

1 Answer 1

5

Yes, you can do this.

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

And you have this structure

webapp
   - WEB-INF
   - resources
        - css
            - somefile.css
        - js
        - img

You would access your resources at

localhost:8080/context/css/somefile.css

will get the resource from /resources/css/somefile.css. Do the same for all the other resources.

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

3 Comments

Thanks, I have default controller, which disable path to resources.
@AlexandrSlabukh Are you still having problems? You can still fix that if you set the order property for the ResourceHandlerRegistry handler mapping.
How to do it in xml file where properties file is loaded.

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.