0

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 class="right_align"><a href="home.do?method=displayHomePage"><bean:message key="home"/></a></p>-->
<br />
<tiles:insert attribute="left-nav" />
<tiles:insert attribute="body-content" />
<br class="clear" />
<hr />
<label class="footer">Delivered by O2 ISD - Data Delivery - GAS Team</label>
</body>
</html>

Thanks in advance.

  • Please add the JSP as source rather than screenshot. Also, you should test the image references directly via browser URL and see what is returned, and what is logged on the server. 404, presumably. – dbreaux Sep 13 '22 at 12:33
  • @dbreaux Hey thanks for your comment. Yes, I have tested the image references directly getting 404. I have edited my question please provide your input. – santhosh_athreya Sep 13 '22 at 12:47
  • What's the full URL you use when you try the image resource directly? Something like localhost:port/customercare/URLToReachResources/... ? And what does the server/app log show? It might give helpful path information about where it's looking. – dbreaux Sep 13 '22 at 12:54

1 Answers1

0

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;
}

}