mardi 23 juin 2020

How can I serve static resources such as Javascript, CSS in my Spring MVC application

I am new to the MVC framework, and I was wondering how would I set up, my application such that I can serve static resources to the client(users)

Currently, I have managed to utilize java configuration instead of the traditional, web.xml and NameOfServlet-servlet.xml.

Can someone please walk me through or link me to a good resource, which will explain how I can serve static resources to the user.

I wish to serve some Javascript and CSS resources to make my existing JSP pages look aesthetically pleasing to the user.

Currently my file structure looks like this

File Structure

Index.JSP which I am trying to display my CSS

 <%@ taglib prefix="c" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Programming
  Date: 6/22/2020
  Time: 9:30 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>





<html>
<head>
    <link href="<c:url value="/resources/css/style.css"/>"/>

</head>
<body>

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

</form>

ContextLoaderListener Class

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

Config Class

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

    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

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(Nothing fancy)

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

    }



}



Aucun commentaire:

Enregistrer un commentaire