mercredi 24 juin 2020

Spring MVC cannot display static resources

I am have been stuck on this issue for a while now, I cannot display css on my jsp page.

I am using annotation based configuration, I have tried almost everything but it seems I cannot display any css at all, my css page essentially changes the background of the page from white to black, obviously I can do this through jsp but this does not solve the underlying root problem.

    public class ApplicationInit extends AbstractContextLoaderInitializer{
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext rootContext
                = new AnnotationConfigWebApplicationContext();
        rootContext.register(DeepAlgorithmConfiguration.class);
        return rootContext;
    }
}

Configuration Class

  @Configuration
@ComponentScan({"com.test.pluto"})
@EnableWebMvc
public class DeepAlgorithmConfiguration  implements WebMvcConfigurer {
    @Bean
    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver; }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("WEB-INF/resources/bootstrap/");

    }
}

Initializer Class

public class DeepAlgorithmInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[]{};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[]{DeepAlgorithmConfiguration.class};
}

@Override
protected String[] getServletMappings() {
    return new String[] {"/"};
}

}

Controller Class

@Controller
public class HelloController {

    @RequestMapping("/add")
    public ModelAndView add(@RequestParam("t1") int k, @RequestParam("t2") int l)
    {

        int answer  = l + k;
        System.out.println(answer);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("display");
        mv.addObject("r1",answer);
        return mv;
}
}

index.jsp page

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <link href="<c:url value="/resources/style.css/" />" rel="stylesheet">
</head>
<body>

<form action="add">
    <input type="text" name="t1" style=""><br>
    <input type="text" name="t2"><br>
    <input type="submit">

</form>
</body>
</html>

File Structure

Web.XML (not sure if needed )

   <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app id = "WebApp1234" version="2.4"

        xmlns = "http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

 <servlet-mapping>
   <servlet-name>HelloWeb</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>



</web-app>

HelloWeb-servlet.xml

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <context:component-scan base-package="com.test.pluto"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>



Aucun commentaire:

Enregistrer un commentaire