https://web.saravan-js.com/Spring21aMvcCar/car/list
This spring example uses no xml config.
But AppInitializer is required to generate web.xml dynamically.
package com.jeromejaglale.config;
@Configuration @EnableWebMvc @ComponentScan(basePackages = {"com.jeromejaglale.service", "com.jeromejaglale.controller"}) public class AppConfig {
@Bean public ViewResolver jspViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); return resolver; } }
|
package com.jeromejaglale.config;
public class AppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "com.jeromejaglale.config"; private static final String MAPPING_URL = "/";
@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(MAPPING_URL); }
private AnnotationConfigWebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); return context; }
}
|
package com.jeromejaglale.config;
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[0]; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{AppConfig.class}; }
@Override protected String[] getServletMappings() { return new String[]{"/"}; }
}
|
package com.jeromejaglale.controller;
@Controller public class CarController { @Autowired private CarService carService;
@RequestMapping("/car/list") public void carList(Model model) { List<Car> carList = carService.findAll(); model.addAttribute("carList", carList); } }
|
public class Car { private String name; private BigDecimal price;
public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } }
|
package com.jeromejaglale.service;
@Service public class CarService { private List<Car> carList = new LinkedList<Car>();
CarService() { Car car1 = new Car(); car1.setName("Mercedes SL"); car1.setPrice(new BigDecimal(123400)); carList.add(car1);
Car car2 = new Car(); car2.setName("BMW M6 Coupé"); car2.setPrice(new BigDecimal(125000)); carList.add(car2);
Car car3 = new Car(); car3.setName("Audi R8"); car3.setPrice(new BigDecimal(136100)); carList.add(car3); }
public List<Car> findAll() { return carList; } }
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body>
<h1>Cars</h1>
<c:forEach items="${carList}" var="car"> <p> ${car.name}: $${car.price} </p> </c:forEach>
</body> </html>
|
![]() |
@Configuration @ComponentScan(basePackages = "eu.kielczewski.example") class AppConfig { }
@EnableWebMvc @Configuration class WebMvcConfig extends WebMvcConfigurerAdapter { }
@Controller class IndexController {
@SuppressWarnings("SameReturnValue") @RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody public String showIndex() { return "Hello world"; }
}
public class AppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "eu.kielczewski.example.config"; private static final String MAPPING_URL = "/*";
@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(MAPPING_URL); }
private AnnotationConfigWebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); return context; }
}
|