Home > Mobile >  Static resources load failed - Spring MVC
Static resources load failed - Spring MVC

Time:10-01

In my Spring MVC application, I want to serve static resources using a java annotation-based configuration.

I have a config class annotated with @Configuration, @EnableWebMvc, and @ComponentScan and my class is implementing WebMvcConfigurer.

//set up view 

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

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // TODO Auto-generated method stub
    
    registry
    .addResourceHandler("/URLToReachResources/**")
    .addResourceLocations("/WEB-INF/resources/");
    
    
    
    
}

Look at my ServletInitilazier

public class MacsCloneApplicationIntitalizer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // TODO Auto-generated method stub

    //create a dispatcher servlet object
    
    AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
    
    webApplicationContext.register(MacsCloneAppConfig.class);
    
    
    //register dispatcher servlet to context
    DispatcherServlet dispatchServlet = new DispatcherServlet(webApplicationContext);
    
    ServletRegistration.Dynamic myCustomServlet = servletContext.addServlet("myDispatchServlet",
            dispatchServlet);
    
    //configurations
    myCustomServlet.setLoadOnStartup(1);
    myCustomServlet.addMapping("/customercare/*");
}

This is how I have structured my application

enter image description here

Not sure where I'm going wrong, I have used maven-archtype-webapp version 1.0 while creating a project and Spring WebMvc 5.3.22 as web dependency.

This is how my JSP looks like and my resources are not getting loaded.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>MIS Portal</title>
<link href="<c:url value="/URLToReachResources/css/ie.css"/>" rel="stylesheet" type="text/css"/>
<link href="<c:url value="/URLToReachResources/css/um.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="headerimg"><img src="<c:url value="/URLToReachResources/images/topstripo2_11.jpg"/>" alt="O2" /><a href="/home"><img src="<c:url value="/URLToReachResources/images/misportal.jpg"/>" border="0" style="margin-left: 2em" alt="O2 Customer Care"/></a></div>
<!--  <p ><a href="home.do?method=displayHomePage"><bean:message key="home"/></a></p>-->
<br />
<tiles:insert attribute="left-nav" />
<tiles:insert attribute="body-content" />
<br  />
<hr />
<label >Delivered by O2 ISD - Data Delivery - GAS Team</label>
</body>
</html>

Thanks in advance.

CodePudding user response:

I was able to fix the above issue using AbstractAnnotationConfigDispatcherServletInitializer class.

public class MacsCloneApplicationIntitalizer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected Class<?>[] getServletConfigClasses() {
    Class arr[] = { MacsCloneAppConfig.class };
    return arr;
}

@Override
protected String[] getServletMappings() {

    String arr[] = { "/customercare/*" };
    return arr;
}

}

  • Related