Skip to content

Commit 47675f3

Browse files
committed
added example for the management of a complex bean, called Railway Station
1 parent d3d3b26 commit 47675f3

File tree

12 files changed

+396
-31
lines changed

12 files changed

+396
-31
lines changed

pom.xml

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
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>
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>
1111

12-
<dependency>
13-
<groupId>org.springframework</groupId>
14-
<artifactId>spring-webmvc</artifactId>
15-
<version>3.1.2.RELEASE</version>
16-
</dependency>
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-webmvc</artifactId>
15+
<version>3.1.2.RELEASE</version>
16+
</dependency>
1717

18-
<dependency>
19-
<groupId>org.apache.velocity</groupId>
20-
<artifactId>velocity</artifactId>
21-
<version>1.7</version>
22-
</dependency>
18+
<dependency>
19+
<groupId>org.apache.velocity</groupId>
20+
<artifactId>velocity</artifactId>
21+
<version>1.7</version>
22+
</dependency>
2323

24-
<dependency>
25-
<groupId>org.codehaus.jackson</groupId>
26-
<artifactId>jackson-mapper-asl</artifactId>
27-
<version>1.9.4</version>
28-
</dependency>
24+
<dependency>
25+
<groupId>org.codehaus.jackson</groupId>
26+
<artifactId>jackson-mapper-asl</artifactId>
27+
<version>1.9.12</version>
28+
</dependency>
2929

30-
</dependencies>
31-
<build>
32-
<finalName>AngularSpringApp</finalName>
33-
</build>
30+
31+
</dependencies>
32+
<build>
33+
<finalName>AngularSpringApp</finalName>
34+
</build>
3435
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.xvitcoder.angualrspringapp.beans;
2+
3+
public class RailwayStation {
4+
5+
6+
private Long id;
7+
8+
private String name;
9+
10+
private Train train;
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
20+
public Train getTrain() {
21+
return train;
22+
}
23+
24+
public void setTrain(Train train) {
25+
this.train = train;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
37+
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.xvitcoder.angualrspringapp.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
15+
import com.xvitcoder.angualrspringapp.service.RailwayStationServiceImpl;
16+
17+
@Controller
18+
@RequestMapping("/railwaystations")
19+
public class RailwayStationController {
20+
21+
@Autowired
22+
private RailwayStationServiceImpl railwayStationsService;
23+
24+
@RequestMapping("railwaystationlist.json")
25+
public @ResponseBody List<RailwayStation> getRailwayStationList() {
26+
return railwayStationsService.getAllRailwayStations();
27+
}
28+
29+
@RequestMapping(value = "/add", method = RequestMethod.POST)
30+
public @ResponseBody void addRailwayStation(@RequestBody RailwayStation railwayStation) {
31+
railwayStationsService.addRailwayStation(railwayStation);
32+
}
33+
34+
@RequestMapping(value = "/update", method = RequestMethod.PUT)
35+
public @ResponseBody void updateRailwayStation(@RequestBody RailwayStation railwayStation) {
36+
railwayStationsService.updateRailwayStation(railwayStation);
37+
}
38+
39+
@RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
40+
public @ResponseBody void removeRailwayStation(@PathVariable("id") Long id) {
41+
railwayStationsService.deleteRailwayStationById(id);
42+
}
43+
44+
@RequestMapping(value = "/removeAll", method = RequestMethod.DELETE)
45+
public @ResponseBody void removeAllRailwayStations() {
46+
railwayStationsService.deleteAll();
47+
}
48+
49+
@RequestMapping("/layout")
50+
public String getRailwayStationPartialPage(ModelMap modelMap) {
51+
return "railwaystations/layout";
52+
}
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.xvitcoder.angualrspringapp.service;
2+
3+
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
4+
5+
import java.util.List;
6+
7+
8+
public interface RailwayStationService {
9+
10+
public List<RailwayStation> getAllRailwayStations();
11+
12+
public RailwayStation getRailwayStationById(Long id);
13+
14+
public void addRailwayStation(RailwayStation RailwayStation);
15+
16+
public void deleteRailwayStationById(Long id);
17+
18+
public void deleteAll();
19+
20+
public void updateRailwayStation(RailwayStation RailwayStation);
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.xvitcoder.angualrspringapp.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import com.xvitcoder.angualrspringapp.beans.RailwayStation;
9+
10+
11+
@Service("RailwayStationService")
12+
public class RailwayStationServiceImpl implements RailwayStationService {
13+
14+
private static List<RailwayStation> rsList = new ArrayList<RailwayStation>();
15+
private static Long id = 0L;
16+
17+
public RailwayStation getRailwayStationById(Long id) {
18+
19+
return findRailwayStationById(id);
20+
}
21+
22+
private RailwayStation findRailwayStationById(Long id) {
23+
for (RailwayStation rs : rsList) {
24+
if (rs.getId() == id) {
25+
return rs;
26+
}
27+
}
28+
29+
return null;
30+
}
31+
32+
public List<RailwayStation> getAllRailwayStations() {
33+
return rsList;
34+
}
35+
36+
37+
public void addRailwayStation(RailwayStation railwayStation) {
38+
railwayStation.setId(++ id);
39+
rsList.add(railwayStation);
40+
41+
}
42+
43+
public void deleteRailwayStationById(Long id) {
44+
RailwayStation found = findRailwayStationById(id);
45+
if (found != null) {
46+
rsList.remove(found);
47+
id--;
48+
}
49+
50+
}
51+
52+
public void updateRailwayStation(RailwayStation railwayStation) {
53+
RailwayStation found = findRailwayStationById(railwayStation.getId());
54+
if (found != null) {
55+
rsList.remove(found);
56+
rsList.add(railwayStation);
57+
}
58+
}
59+
60+
@Override
61+
public void deleteAll() {
62+
rsList.clear();
63+
id = 0L;
64+
}
65+
}

src/main/webapp/WEB-INF/html/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ul class="menu">
1313
<li><a href="#/cars">Cars</a></li>
1414
<li><a href="#/trains">Trains</a></li>
15+
<li><a href="#/railwaystations">Railway Station</a></li>
1516
</ul>
1617
<hr class="" />
1718
<div ng-view></div>
@@ -21,6 +22,7 @@
2122
<script src="resources/js/lib/angular/angular.js"></script>
2223
<script src="resources/js/app.js"></script>
2324
<script src="resources/js/services.js"></script>
25+
<script src="resources/js/controllers/RailwayStationController.js"></script>
2426
<script src="resources/js/controllers/CarController.js"></script>
2527
<script src="resources/js/controllers/TrainController.js"></script>
2628
<script src="resources/js/filters.js"></script>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<div class="alert alert-error" ng-show="error">{{errorMessage}}</div>
2+
<form class="form-horizontal">
3+
4+
<div class="control-group">
5+
<label class="control-label" for="rs_name">Railway Station Name</label>
6+
7+
<div class="controls">
8+
<input type="text" id="rs_name" ng-model="rs.name"
9+
placeholder="Railway Name" required min="2">
10+
</div>
11+
</div>
12+
13+
14+
<div class="control-group">
15+
<label class="control-label" for="train_name">Train Name</label>
16+
17+
<div class="controls">
18+
<input type="text" id="train_name" ng-model="rs.train.name"
19+
placeholder="Train Name" required min="2">
20+
</div>
21+
</div>
22+
<div class="control-group">
23+
<label class="control-label" for="train_speed">Speed</label>
24+
25+
<div class="controls">
26+
27+
28+
<input class="span3" type="text" id="train_speed"
29+
ng-model="rs.train.speed" placeholder="Speed" required min="1">
30+
<span class="add-on">km/h</span>
31+
</div>
32+
</div>
33+
<div class="control-group">
34+
<div class="controls">
35+
<label class="checkbox"> <input type="checkbox"
36+
ng-model="rs.train.diesel"> Is Diesel
37+
</label>
38+
</div>
39+
<hr />
40+
<div class="controls">
41+
<button type="button" class="btn btn-primary"
42+
ng-disabled="!rs.name || !rs.train.speed" ng-hide="editMode"
43+
ng-click="addNewRailwayStation(rs)">Add Railway Station</button>
44+
45+
<button type="button" class="btn btn-primary"
46+
ng-disabled="!rs.train.name || !rs.train.speed" ng-show="editMode"
47+
ng-click="updateRailwayStation(rs)">Save Railway Station</button>
48+
<button type="button" class="btn" ng-click="resetRailwayStationForm()">Reset</button>
49+
</div>
50+
</div>
51+
</form>
52+
<hr />
53+
<h3>Railway Stations List</h3>
54+
<div class="alert alert-info" ng-show="railwaystations.length == 0">No Railway Stations found</div>
55+
<table class="table table-bordered table-striped" ng-show="railwaystations.length > 0">
56+
<thead>
57+
<tr>
58+
<th style="text-align: center; width: 25px;">Id</th>
59+
<th style="text-align: center;">Name</th>
60+
<!--
61+
<th style="text-align: center;">Speed</th>
62+
<th style="text-align: center;">Diesel</th>
63+
-->
64+
<th style="text-align: center;">Action</th>
65+
</tr>
66+
</thead>
67+
<tbody>
68+
<tr ng-repeat="item in railwaystations | orderBy:predicate">
69+
<td style="text-align: center;">{{item.id}}</td>
70+
<td>{{item.name}}</td>
71+
72+
73+
<!-- <td>{{train.speed}}</td>
74+
<td style="text-align: center; width: 20px;"><span
75+
ng-show="train.diesel" class="icon-ok"></span></td> -->
76+
77+
<td style="width: 100px; text-align: center;">
78+
<button class="btn btn-mini btn-danger"
79+
ng-click="removeRailwayStation(item.id)">Remove</button>
80+
<button class="btn btn-mini btn-success" ng-click="editRailwayStation(item)">Edit</button>
81+
</td>
82+
</tr>
83+
</tbody>
84+
</table>
85+
<button class="btn btn-danger" ng-show="railwaystations.length > 1"
86+
ng-click="removeAllRailwayStations()">Remove All stations</button>

src/main/webapp/WEB-INF/html/trains/layout.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<div class="controls">
1616

17+
1718
<input class="span3" type="text" id="inputSpeed" ng-model="train.speed" placeholder="Speed" required min="1">
1819
<span class="add-on">km/h</span>
1920
</div>
@@ -60,4 +61,4 @@ <h3>Trains List</h3>
6061
</tr>
6162
</tbody>
6263
</table>
63-
<button class="btn btn-danger" ng-show="trains.length > 1" ng-click="removeAllTrains()">Remove All Cars</button>
64+
<button class="btn btn-danger" ng-show="trains.length > 1" ng-click="removeAllTrains()">Remove All Trains</button>

src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
<servlet>
1818
<servlet-name>DispatcherServlet</servlet-name>
1919
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
20-
<load-on-startup>1</load-on-startup>
2120
<init-param>
2221
<param-name>contextConfigLocation</param-name>
2322
<param-value>/WEB-INF/spring/webapp-config.xml</param-value>
2423
</init-param>
24+
<load-on-startup>1</load-on-startup>
2525
</servlet>
2626

2727
<servlet-mapping>

src/main/webapp/resources/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ App.config(['$routeProvider', function ($routeProvider) {
1515
templateUrl: 'trains/layout',
1616
controller: TrainController
1717
});
18+
19+
$routeProvider.when('/railwaystations', {
20+
templateUrl: 'railwaystations/layout',
21+
controller: RailwayStationController
22+
});
1823

1924
$routeProvider.otherwise({redirectTo: '/cars'});
2025
}]);

0 commit comments

Comments
 (0)