Skip to content

Commit 8617faf

Browse files
lor6KevinGilmore
authored andcommitted
data rest crud, angular fe (eugenp#1199)
* data rest crud, angular fe * fix formatting, remove extra imports * fix config * change student entity to employee * expose ids * update boot version, removes ids, check null id
1 parent 288d464 commit 8617faf

11 files changed

Lines changed: 395 additions & 5 deletions

File tree

‎spring-rest-angular/pom.xml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>1.4.4.RELEASE</version>
14+
<version>1.5.1.RELEASE</version>
1515
</parent>
1616

1717
<dependencies>
@@ -74,6 +74,15 @@
7474
<artifactId>spring-boot-starter-test</artifactId>
7575
<scope>test</scope>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-starter-data-rest</artifactId>
80+
</dependency>
81+
<dependency>
82+
<groupId>javax.servlet</groupId>
83+
<artifactId>jstl</artifactId>
84+
<version>1.2</version>
85+
</dependency>
7786
</dependencies>
7887

7988
<build>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.web.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
public class EmployeeController {
8+
9+
@RequestMapping(value = "/employeePage")
10+
public String getEmployeePage() {
11+
return "employee";
12+
}
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.web.dao;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.web.entity.Employee;
6+
import org.springframework.data.repository.CrudRepository;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
9+
10+
@RepositoryRestResource(collectionResourceRel = "employee", path = "employees")
11+
public interface EmployeeCRUDRepository extends CrudRepository<Employee, Long> {
12+
List<Employee> findByName(@Param("name") String name);
13+
}

‎spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.baeldung.web.entity.Student;
44
import org.springframework.data.jpa.repository.JpaRepository;
55

6-
public interface StudentRepository extends JpaRepository<Student, Long> {
6+
public interface StudentRepository extends JpaRepository<Student, Long>
7+
{
78

89
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.web.entity;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Employee implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
private long id;
16+
17+
@Column(nullable = false)
18+
private String name;
19+
20+
@Column(nullable = false)
21+
private Integer age;
22+
23+
public Employee() {
24+
}
25+
26+
public Employee(long id, String name, Integer age) {
27+
super();
28+
this.id = id;
29+
this.name = name;
30+
this.age = age;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public Integer getAge() {
42+
return age;
43+
}
44+
45+
public void setAge(Integer age) {
46+
this.age = age;
47+
}
48+
49+
public long getId() {
50+
return id;
51+
}
52+
53+
public void setId(long id) {
54+
this.id = id;
55+
}
56+
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.web.main;
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.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
@ComponentScan("org.baeldung.web.controller")
15+
public class MvcConfig extends WebMvcConfigurerAdapter{
16+
17+
public MvcConfig(){
18+
super();
19+
}
20+
21+
@Override
22+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
23+
configurer.enable();
24+
}
25+
26+
@Bean
27+
public ViewResolver viewResolver() {
28+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
29+
30+
bean.setPrefix("/WEB-INF/pages/");
31+
bean.setSuffix(".html");
32+
33+
return bean;
34+
}
35+
36+
}

‎spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import javax.sql.DataSource;
44

5-
import org.springframework.boot.orm.jpa.EntityScan;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.ComponentScan;
88
import org.springframework.context.annotation.Configuration;
@@ -26,7 +26,7 @@ public JdbcTemplate getJdbcTemplate() {
2626
@Bean
2727
public DataSource dataSource() {
2828
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
29-
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").build();
29+
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").addScript("db/sql/employees.sql").build();
3030
return db;
3131
}
3232

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CREATE TABLE employee (
2+
id INTEGER PRIMARY KEY,
3+
name VARCHAR(30),
4+
age INTEGER
5+
);
6+
7+
INSERT INTO employee (id,name,age)
8+
VALUES (1,'Bryan',20);
9+
INSERT INTO employee (id,name,age)
10+
VALUES (2,'Lisa',30);
11+
INSERT INTO employee (id,name,age)
12+
VALUES (3,'Laura',40);
13+
INSERT INTO employee (id,name,age)
14+
VALUES (4,'Alex',35);
15+
INSERT INTO employee (id,name,age)
16+
VALUES (5,'John',47);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html ng-app="app">
3+
<head>
4+
<meta charset="ISO-8859-1">
5+
<title>Employee CRUD</title>
6+
<script
7+
src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
8+
<script
9+
src="//cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
10+
<script src="view/app.js"></script>
11+
<style>
12+
a {
13+
cursor: pointer;
14+
background-color: lightblue;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<div ng-controller="EmployeeCRUDCtrl">
20+
<table>
21+
<tr>
22+
<td width="100">ID:</td>
23+
<td><input type="text" id="id" ng-model="employee.id" /></td>
24+
</tr>
25+
<tr>
26+
<td width="100">Name:</td>
27+
<td><input type="text" id="name" ng-model="employee.name" /></td>
28+
</tr>
29+
<tr>
30+
<td width="100">Age:</td>
31+
<td><input type="text" id="age" ng-model="employee.age" /></td>
32+
</tr>
33+
</table>
34+
<br /> <br />
35+
<a ng-click="getEmployee(employee.id)">Get employee</a>
36+
<a ng-click="updateEmployee(employee.id,employee.name,employee.age)">Update employee</a>
37+
<a ng-click="addEmployee(employee.id,employee.name,employee.age)">Add employee</a>
38+
<a ng-click="deleteEmployee(employee.id)">Delete employee</a>
39+
40+
<br /> <br />
41+
<p style="color: green">{{message}}</p>
42+
<p style="color: red">{{errorMessage}}</p>
43+
44+
<br />
45+
<br />
46+
<a ng-click="getAllEmployees()">Get all Employees</a><br /> <br />
47+
Name: <input type="text" id="name" ng-model="name" />
48+
<a ng-click="getEmployeesByName()">Get employees by name</a>
49+
<br /> <br />
50+
<div ng-repeat="emp in employees">
51+
{{emp.name}} {{emp.age}}
52+
</div>
53+
</div>
54+
</body>
55+
</html>

0 commit comments

Comments
 (0)