Skip to content

Commit 956dc43

Browse files
author
Vitalie Mudrenco
committed
- Upgraded to spring 4.2 version
- Migrated the project from XML configuration to Java Config - Removed web.xml - Changed the view renderer from Velocity to Thymeleaf - Changed the context path to use the same path as project name
1 parent 83a0f0e commit 956dc43

36 files changed

+250
-164
lines changed

Diff for: .gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
target/
1+
target
22
src/main/resources/rebel.xml
3+
.idea
4+
spring-mvc-angularjs.iml
5+
*/**/.DS_Store

Diff for: README.md

100644100755
+13-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
spring-mvc-angularjs
22
====================
33

4-
A simple application to demonstrate how to configure angularjs with Spring MVC
4+
A simple application to demonstrate how to configure Angularjs with Spring MVC
55

6-
The application demonstrates how to configure Spring MVC with AngularJS and Velocity.
6+
The application demonstrates how to configure Spring MVC with AngularJS and Thymeleaf.
77

8-
It uses the AngularJS-Seed with some modifications to allow Spring MVC load the HTML. Also integrated Velocity in case it is needed to pass some values from the spring mvc controller into the partials or index.html file.
8+
It uses the AngularJS-Seed with some modifications to allow Spring MVC load the HTML. Also integrated Thymeleaf in case it is needed to pass some values from the spring mvc controller into the partials or index.html file.
99

1010
I also included the bootstrap library for a better UI.
1111

12-
RUN:
12+
Run Local (Embedded Tomcat):
13+
----------------
14+
- execute: *mvn tomcat7:run*
15+
- Then open the URL: http://localhost:8080/spring-mvc-angularjs
1316

14-
mvn tomcat:run
17+
Run inside tomcat container
18+
-----------------------------
19+
Package it with maven
1520

16-
Then open the URL: http://localhost:8080/AngularSpringApp
21+
- execute: *mvn clean package*
22+
- copy the result war file into your tomcat/webapp folder
23+
- Then open the URL: http://localhost:8080/spring-mvc-angularjs

Diff for: pom.xml

100644100755
+66-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,68 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.xvitcoder.angularspringapp</groupId>
5-
<artifactId>AngularSpringApp</artifactId>
6-
<packaging>war</packaging>
7-
<version>1.0-SNAPSHOT</version>
8-
<name>AngularSpringApp Maven Webapp</name>
9-
<url>http://maven.apache.org</url>
10-
<dependencies>
11-
12-
<dependency>
13-
<groupId>org.springframework</groupId>
14-
<artifactId>spring-webmvc</artifactId>
15-
<version>3.1.2.RELEASE</version>
16-
</dependency>
17-
18-
<dependency>
19-
<groupId>org.apache.velocity</groupId>
20-
<artifactId>velocity</artifactId>
21-
<version>1.7</version>
22-
</dependency>
23-
24-
<dependency>
25-
<groupId>org.codehaus.jackson</groupId>
26-
<artifactId>jackson-mapper-asl</artifactId>
27-
<version>1.9.12</version>
28-
</dependency>
29-
30-
31-
</dependencies>
32-
<build>
33-
<finalName>AngularSpringApp</finalName>
34-
</build>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.xvitcoder.springmvcangularjs</groupId>
5+
<artifactId>spring-mvc-angularjs</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.1</version>
8+
<name>AngularSpringApp Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-webmvc</artifactId>
15+
<version>4.2.4.RELEASE</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
<version>3.0.1</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.thymeleaf</groupId>
27+
<artifactId>thymeleaf-spring4</artifactId>
28+
<version>2.1.4.RELEASE</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>com.fasterxml.jackson.core</groupId>
33+
<artifactId>jackson-core</artifactId>
34+
<version>2.1.0</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.fasterxml.jackson.core</groupId>
39+
<artifactId>jackson-databind</artifactId>
40+
<version>2.1.0</version>
41+
</dependency>
42+
43+
</dependencies>
44+
<build>
45+
<finalName>spring-mvc-angularjs</finalName>
46+
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-war-plugin</artifactId>
51+
<version>2.5</version>
52+
<configuration>
53+
<failOnMissingWebXml>false</failOnMissingWebXml>
54+
</configuration>
55+
</plugin>
56+
57+
<plugin>
58+
<groupId>org.apache.tomcat.maven</groupId>
59+
<artifactId>tomcat7-maven-plugin</artifactId>
60+
<version>2.2</version>
61+
<configuration>
62+
<port>8080</port>
63+
<path>/spring-mvc-angularjs</path>
64+
</configuration>
65+
</plugin>
66+
</plugins>
67+
</build>
3568
</project>

Diff for: src/main/java/com/xvitcoder/angualrspringapp/controller/IndexController.java

-28
This file was deleted.

Diff for: src/main/java/com/xvitcoder/angualrspringapp/beans/RailwayStation.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/beans/RailwayStation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xvitcoder.angualrspringapp.beans;
1+
package com.xvitcoder.springmvcangularjs.beans;
22

33
public class RailwayStation {
44

Diff for: src/main/java/com/xvitcoder/angualrspringapp/beans/Train.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/beans/Train.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xvitcoder.angualrspringapp.beans;
1+
package com.xvitcoder.springmvcangularjs.beans;
22

33
/**
44
* Created with IntelliJ IDEA.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.xvitcoder.springmvcangularjs.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
5+
/**
6+
* Created by xvitcoder on 12/24/15.
7+
*/
8+
@Configuration
9+
public class AppConfig {
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.xvitcoder.springmvcangularjs.config;
2+
3+
import org.springframework.web.WebApplicationInitializer;
4+
import org.springframework.web.context.ContextLoaderListener;
5+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
import org.springframework.web.servlet.DispatcherServlet;
7+
8+
import javax.servlet.ServletContext;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.ServletRegistration;
11+
12+
/**
13+
* Created by xvitcoder on 12/24/15.
14+
*/
15+
public class AppInitializer implements WebApplicationInitializer {
16+
17+
public void onStartup(ServletContext servletContext) throws ServletException {
18+
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
19+
rootContext.register(AppConfig.class);
20+
21+
servletContext.addListener(new ContextLoaderListener(rootContext));
22+
23+
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
24+
dispatcherContext.register(WebMvcConfig.class);
25+
26+
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
27+
dispatcher.setLoadOnStartup(1);
28+
dispatcher.addMapping("/");
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.xvitcoder.springmvcangularjs.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.thymeleaf.spring4.SpringTemplateEngine;
6+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
7+
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
8+
9+
/**
10+
* Created by xvitcoder on 12/24/15.
11+
*/
12+
@Configuration
13+
public class ThymeleafConfig {
14+
15+
@Bean
16+
public ServletContextTemplateResolver templateResolver() {
17+
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
18+
resolver.setPrefix("/WEB-INF/html/");
19+
resolver.setSuffix(".html");
20+
resolver.setTemplateMode("HTML5");
21+
resolver.setCacheable(false);
22+
return resolver;
23+
}
24+
25+
@Bean
26+
public SpringTemplateEngine templateEngine() {
27+
SpringTemplateEngine engine = new SpringTemplateEngine();
28+
engine.setTemplateResolver(templateResolver());
29+
return engine;
30+
}
31+
32+
@Bean
33+
public ThymeleafViewResolver thymeleafViewResolver() {
34+
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
35+
resolver.setTemplateEngine(templateEngine());
36+
return resolver;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.xvitcoder.springmvcangularjs.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.http.converter.HttpMessageConverter;
7+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
8+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Created by xvitcoder on 12/24/15.
17+
*/
18+
@Configuration
19+
@EnableWebMvc
20+
@ComponentScan(basePackages = "com.xvitcoder.springmvcangularjs")
21+
@Import({ThymeleafConfig.class})
22+
public class WebMvcConfig extends WebMvcConfigurerAdapter {
23+
24+
@Override
25+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
26+
configurer.enable();
27+
}
28+
29+
@Override
30+
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
31+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
32+
}
33+
34+
@Override
35+
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
36+
converters.add(new MappingJackson2HttpMessageConverter());
37+
super.configureMessageConverters(converters);
38+
}
39+
}

Diff for: src/main/java/com/xvitcoder/angualrspringapp/controller/CarController.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/controller/CarController.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package com.xvitcoder.angualrspringapp.controller;
1+
package com.xvitcoder.springmvcangularjs.controller;
22

3-
import com.xvitcoder.angualrspringapp.service.CarService;
3+
import com.xvitcoder.springmvcangularjs.service.CarService;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.stereotype.Controller;
6-
import org.springframework.ui.ModelMap;
76
import org.springframework.web.bind.annotation.PathVariable;
87
import org.springframework.web.bind.annotation.RequestMapping;
98
import org.springframework.web.bind.annotation.RequestMethod;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.xvitcoder.springmvcangularjs.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: xvitcoder
9+
* Date: 12/20/12
10+
* Time: 5:27 PM
11+
*/
12+
@Controller
13+
@RequestMapping("/")
14+
public class IndexController {
15+
16+
@RequestMapping
17+
public String getIndexPage() {
18+
return "index";
19+
}
20+
}

Diff for: src/main/java/com/xvitcoder/angualrspringapp/controller/RailwayStationController.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/controller/RailwayStationController.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xvitcoder.angualrspringapp.controller;
1+
package com.xvitcoder.springmvcangularjs.controller;
22

33
import java.util.List;
44

@@ -11,8 +11,8 @@
1111
import org.springframework.web.bind.annotation.RequestMethod;
1212
import org.springframework.web.bind.annotation.ResponseBody;
1313

14-
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
15-
import com.xvitcoder.angualrspringapp.service.RailwayStationServiceImpl;
14+
import com.xvitcoder.springmvcangularjs.beans.RailwayStation;
15+
import com.xvitcoder.springmvcangularjs.service.RailwayStationServiceImpl;
1616

1717
@Controller
1818
@RequestMapping("/railwaystations")

Diff for: src/main/java/com/xvitcoder/angualrspringapp/controller/TrainController.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/controller/TrainController.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package com.xvitcoder.angualrspringapp.controller;
1+
package com.xvitcoder.springmvcangularjs.controller;
22

3-
import com.xvitcoder.angualrspringapp.beans.Train;
4-
import com.xvitcoder.angualrspringapp.service.TrainService;
3+
import com.xvitcoder.springmvcangularjs.beans.Train;
4+
import com.xvitcoder.springmvcangularjs.service.TrainService;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.ModelMap;

Diff for: src/main/java/com/xvitcoder/angualrspringapp/service/CarService.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/service/CarService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xvitcoder.angualrspringapp.service;
1+
package com.xvitcoder.springmvcangularjs.service;
22

33
import java.util.List;
44

Diff for: src/main/java/com/xvitcoder/angualrspringapp/service/CarServiceImpl.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/service/CarServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xvitcoder.angualrspringapp.service;
1+
package com.xvitcoder.springmvcangularjs.service;
22

33
import org.springframework.stereotype.Service;
44

Diff for: src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationService.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/service/RailwayStationService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.xvitcoder.angualrspringapp.service;
1+
package com.xvitcoder.springmvcangularjs.service;
22

3-
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
3+
import com.xvitcoder.springmvcangularjs.beans.RailwayStation;
44

55
import java.util.List;
66

Diff for: src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationServiceImpl.java renamed to src/main/java/com/xvitcoder/springmvcangularjs/service/RailwayStationServiceImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package com.xvitcoder.angualrspringapp.service;
1+
package com.xvitcoder.springmvcangularjs.service;
22

33
import java.util.ArrayList;
44
import java.util.List;
55

66
import org.springframework.stereotype.Service;
77

8-
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
8+
import com.xvitcoder.springmvcangularjs.beans.RailwayStation;
99

1010

1111
@Service("RailwayStationService")

0 commit comments

Comments
 (0)