Skip to content

Commit 1646085

Browse files
committed
Modified mvc-velocity to java based configuration
1 parent 493adce commit 1646085

8 files changed

Lines changed: 161 additions & 9 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import java.util.Set;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRegistration;
8+
9+
import org.springframework.web.WebApplicationInitializer;
10+
import org.springframework.web.context.ContextLoaderListener;
11+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.context.support.GenericWebApplicationContext;
13+
import org.springframework.web.servlet.DispatcherServlet;
14+
15+
public class MainWebAppInitializer implements WebApplicationInitializer {
16+
17+
/**
18+
* Register and configure all Servlet container components necessary to
19+
* power the web application.
20+
*/
21+
@Override
22+
public void onStartup(final ServletContext sc) throws ServletException {
23+
24+
// Create the 'root' Spring application context
25+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
26+
root.register(WebConfig.class, SpringConfig.class);
27+
28+
// Manages the lifecycle of the root application context
29+
sc.addListener(new ContextLoaderListener(root));
30+
31+
// Handles requests into the application
32+
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc",
33+
new DispatcherServlet(new GenericWebApplicationContext()));
34+
appServlet.setLoadOnStartup(1);
35+
36+
final Set<String> mappingConflicts = appServlet.addMapping("/");
37+
if (!mappingConflicts.isEmpty()) {
38+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due "
39+
+ "to an existing mapping. This is a known issue under Tomcat versions "
40+
+ "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
41+
}
42+
}
43+
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.baeldung.mvc.velocity.service.TutorialsService;
7+
8+
@Configuration
9+
public class SpringConfig {
10+
11+
@Bean
12+
public TutorialsService tutService(){
13+
return new TutorialsService();
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
12+
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
13+
14+
@Configuration
15+
@EnableWebMvc
16+
@ComponentScan(basePackages={"com.baeldung.mvc.velocity.controller"})
17+
public class WebConfig extends WebMvcConfigurerAdapter {
18+
19+
public WebConfig() {
20+
super();
21+
}
22+
23+
@Override
24+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
25+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
26+
}
27+
28+
@Override
29+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
30+
configurer.enable();
31+
}
32+
33+
@Bean
34+
public ViewResolver viewResolver() {
35+
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
36+
bean.setCache(true);
37+
bean.setPrefix("/WEB-INF/views/");
38+
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
39+
bean.setSuffix(".vm");
40+
return bean;
41+
}
42+
43+
@Bean
44+
public VelocityConfigurer velocityConfig() {
45+
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
46+
velocityConfigurer.setResourceLoaderPath("/");
47+
return velocityConfigurer;
48+
}
49+
}

‎spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
</tr>
1111
#foreach($tut in $tutorials)
1212
<tr>
13-
<td>$tut.tutId</td>
14-
<td>$tut.title</td>
15-
<td>$tut.description</td>
16-
<td>$tut.author</td>
13+
<td id="tutId_$foreach.count">$tut.tutId</td>
14+
<td id="title_$foreach.count">$tut.title</td>
15+
<td id="desc_$foreach.count">$tut.description</td>
16+
<td id="auth_$foreach.count">$tut.author</td>
1717
</tr>
1818
#end
1919
</table>
File renamed without changes.

‎spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
1010
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1111
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
1213

1314
import java.util.Arrays;
1415

@@ -26,10 +27,12 @@
2627

2728
import com.baeldung.mvc.velocity.domain.Tutorial;
2829
import com.baeldung.mvc.velocity.service.ITutorialsService;
29-
import com.baeldung.mvc.velocity.service.TutorialsService;
30+
import com.baeldung.mvc.velocity.spring.config.WebConfig;
31+
import com.baeldung.mvc.velocity.test.config.TestConfig;
3032

3133
@RunWith(SpringJUnit4ClassRunner.class)
32-
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
34+
//@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
35+
@ContextConfiguration(classes = {TestConfig.class, WebConfig.class})
3336
@WebAppConfiguration
3437
public class DataContentControllerTest {
3538

@@ -44,10 +47,9 @@ public class DataContentControllerTest {
4447

4548
@Before
4649
public void setUp() {
47-
tutServiceMock = Mockito.mock(TutorialsService.class);
4850
Mockito.reset(tutServiceMock);
4951

50-
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
52+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).dispatchOptions(true).build();
5153
}
5254

5355
@Test
@@ -77,5 +79,7 @@ public void testModel() throws Exception{
7779
hasProperty("title", is("Android"))
7880
)
7981
)));
82+
83+
mockMvc.perform(get("/")).andExpect(xpath("//table").exists());
8084
}
8185
}

‎spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import com.baeldung.mvc.velocity.controller.MainController;
2323
import com.baeldung.mvc.velocity.domain.Tutorial;
2424
import com.baeldung.mvc.velocity.service.TutorialsService;
25+
import com.baeldung.mvc.velocity.test.config.TestConfig;
2526

2627

2728
@RunWith(SpringJUnit4ClassRunner.class)
2829
@WebAppConfiguration
29-
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
30+
//@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
31+
@ContextConfiguration(classes = {TestConfig.class})
3032
public class NavigationControllerTest {
3133

3234
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.mvc.velocity.test.config;
2+
3+
import org.mockito.Mockito;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
8+
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
9+
10+
import com.baeldung.mvc.velocity.service.ITutorialsService;
11+
12+
@Configuration
13+
public class TestConfig {
14+
15+
16+
@Bean
17+
public ViewResolver viewResolver() {
18+
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
19+
bean.setCache(true);
20+
bean.setPrefix("/WEB-INF/views/");
21+
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
22+
bean.setSuffix(".vm");
23+
return bean;
24+
}
25+
26+
@Bean
27+
public VelocityConfigurer velocityConfig() {
28+
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
29+
velocityConfigurer.setResourceLoaderPath("/");
30+
return velocityConfigurer;
31+
}
32+
33+
@Bean
34+
public ITutorialsService getTutService() {
35+
return Mockito.mock(ITutorialsService.class);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)