JAVA-3528 Move spring-mvc-velocity module
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.mvc.velocity.controller;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
import com.baeldung.mvc.velocity.service.ITutorialsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class MainController {
|
||||
|
||||
@Autowired
|
||||
private ITutorialsService tutService;
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||
public String welcomePage() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
||||
public String listTutorialsPage(Model model) {
|
||||
List<Tutorial> list = tutService.listTutorials();
|
||||
model.addAttribute("tutorials", list);
|
||||
return "list";
|
||||
}
|
||||
|
||||
public ITutorialsService getTutService() {
|
||||
return tutService;
|
||||
}
|
||||
|
||||
public void setTutService(ITutorialsService tutService) {
|
||||
this.tutService = tutService;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.mvc.velocity.domain;
|
||||
|
||||
public class Tutorial {
|
||||
|
||||
private final Integer tutId;
|
||||
private final String title;
|
||||
private final String description;
|
||||
private final String author;
|
||||
|
||||
public Tutorial(Integer tutId, String title, String description, String author) {
|
||||
this.tutId = tutId;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Integer getTutId() {
|
||||
return tutId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.mvc.velocity.service;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ITutorialsService {
|
||||
|
||||
List<Tutorial> listTutorials();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.mvc.velocity.service;
|
||||
|
||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class TutorialsService implements ITutorialsService {
|
||||
|
||||
public List<Tutorial> listTutorials() {
|
||||
return Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.mvc.velocity.spring.config;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
import java.util.Set;
|
||||
|
||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(final ServletContext sc) throws ServletException {
|
||||
|
||||
// Create the 'root' Spring application context
|
||||
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||
root.register(WebConfig.class);
|
||||
|
||||
// Manages the lifecycle of the root application context
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
// Handles requests into the application
|
||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
|
||||
appServlet.setLoadOnStartup(1);
|
||||
|
||||
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
||||
if (!mappingConflicts.isEmpty()) {
|
||||
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.mvc.velocity.spring.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller", "com.baeldung.mvc.velocity.service" })
|
||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
|
||||
bean.setCache(true);
|
||||
bean.setPrefix("/WEB-INF/views/");
|
||||
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
|
||||
bean.setSuffix(".vm");
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfig() {
|
||||
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
|
||||
velocityConfigurer.setResourceLoaderPath("/");
|
||||
return velocityConfigurer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,4 @@
|
||||
<div
|
||||
style="background: #63B175; text-align: center; padding: 5px; margin-top: 10px;">
|
||||
@Copyright baeldung.com
|
||||
</div>
|
||||
@@ -0,0 +1,5 @@
|
||||
<div style="background: #63B175; height: 80px; padding: 5px;">
|
||||
<div style="float: left">
|
||||
<h1>Our tutorials</h1>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,22 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring with Velocity</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
#parse("/WEB-INF/fragments/header.vm")
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<!-- View index.vm is inserted here -->
|
||||
$screen_content
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
#parse("/WEB-INF/fragments/footer.vm")
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
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-4.1.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
|
||||
|
||||
|
||||
<!-- Register the annotated components in the container eg : annotated controllers -->
|
||||
<context:component-scan base-package="com.baeldung.mvc.velocity.*" />
|
||||
|
||||
<!-- Tell the container that we are going to use annotations -->
|
||||
<context:annotation-config />
|
||||
|
||||
|
||||
|
||||
<bean id="velocityConfig"
|
||||
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
|
||||
<property name="resourceLoaderPath">
|
||||
<value>/</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
|
||||
<property name="cache" value="true" />
|
||||
<property name="prefix" value="/WEB-INF/views/" />
|
||||
<property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
|
||||
<property name="suffix" value=".vm" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- Empty -->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,4 @@
|
||||
<h1>Index</h1>
|
||||
|
||||
<h2>Welcome page</h2>
|
||||
This is just the welcome page
|
||||
@@ -0,0 +1,19 @@
|
||||
<h1>Index</h1>
|
||||
|
||||
<h2>Tutorials list</h2>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>Tutorial Id</th>
|
||||
<th>Tutorial Title</th>
|
||||
<th>Tutorial Description</th>
|
||||
<th>Tutorial Author</th>
|
||||
</tr>
|
||||
#foreach($tut in $tutorials)
|
||||
<tr>
|
||||
<td id="tutId_$foreach.count">$tut.tutId</td>
|
||||
<td id="title_$foreach.count">$tut.title</td>
|
||||
<td id="desc_$foreach.count">$tut.description</td>
|
||||
<td id="auth_$foreach.count">$tut.author</td>
|
||||
</tr>
|
||||
#end
|
||||
</table>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
|
||||
|
||||
<display-name>Spring MVC Velocity</display-name>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/spring-context.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
</web-app>
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.mvc.velocity.test;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasProperty;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.mvc.velocity.spring.config.WebConfig;
|
||||
import com.baeldung.mvc.velocity.test.config.TestConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
|
||||
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
|
||||
@WebAppConfiguration
|
||||
public class DataContentControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingList_ThenModelAndContentOK() throws Exception {
|
||||
|
||||
mockMvc.perform(get("/list")).andExpect(status().isOk()).andExpect(view().name("list")).andExpect(model().attribute("tutorials", hasSize(2)))
|
||||
.andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")), hasProperty("title", is("Guava"))))))
|
||||
.andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));
|
||||
|
||||
mockMvc.perform(get("/list")).andExpect(xpath("//table").exists());
|
||||
mockMvc.perform(get("/list")).andExpect(xpath("//td[@id='tutId_1']").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingIndex_thenViewOK() throws Exception {
|
||||
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")).andExpect(model().size(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.mvc.velocity.test.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
||||
|
||||
@Configuration
|
||||
public class TestConfig {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
|
||||
bean.setCache(true);
|
||||
bean.setPrefix("/WEB-INF/views/");
|
||||
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
|
||||
bean.setSuffix(".vm");
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfig() {
|
||||
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
|
||||
velocityConfigurer.setResourceLoaderPath("/");
|
||||
return velocityConfigurer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import com.baeldung.mvc.velocity.spring.config.WebConfig;
|
||||
import com.baeldung.mvc.velocity.test.config.TestConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
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-4.1.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
|
||||
|
||||
|
||||
<!-- Register the annotated components in the container eg : annotated controllers -->
|
||||
<context:component-scan base-package="com.baeldung.mvc.velocity.*" />
|
||||
|
||||
<!-- Tell the container that we are going to use annotations -->
|
||||
<context:annotation-config />
|
||||
|
||||
|
||||
|
||||
<bean id="velocityConfig"
|
||||
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
|
||||
<property name="resourceLoaderPath">
|
||||
<value>/</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
|
||||
<property name="cache" value="true" />
|
||||
<property name="prefix" value="/WEB-INF/views/" />
|
||||
<property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
|
||||
<property name="suffix" value=".vm" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user