Skip to content

Commit 6335e28

Browse files
aravind-r-ranganathanpedja4
authored andcommitted
BAEL-1911 - Spring session with JDBC (eugenp#4689)
1 parent cddf858 commit 6335e28

17 files changed

Lines changed: 232 additions & 29 deletions

File tree

‎spring-session/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
### Relevant Articles:
66
- [Introduction to Spring Session](http://www.baeldung.com/spring-session)
7+
- [Spring Session with JDBC](http://www.baeldung.com)

‎spring-session/pom.xml‎

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
21
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
32
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.baeldung</groupId>
55
<artifactId>spring-session</artifactId>
66
<version>1.0.0-SNAPSHOT</version>
7-
<packaging>jar</packaging>
7+
8+
<name>spring-session</name>
9+
<packaging>pom</packaging>
810

911
<parent>
1012
<artifactId>parent-boot-1</artifactId>
@@ -13,32 +15,9 @@
1315
<relativePath>../parent-boot-1</relativePath>
1416
</parent>
1517

16-
<dependencies>
17-
<dependency>
18-
<groupId>org.springframework.boot</groupId>
19-
<artifactId>spring-boot-starter-data-redis</artifactId>
20-
</dependency>
21-
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-security</artifactId>
24-
</dependency>
25-
<dependency>
26-
<groupId>org.springframework.session</groupId>
27-
<artifactId>spring-session</artifactId>
28-
</dependency>
29-
<dependency>
30-
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-starter-web</artifactId>
32-
</dependency>
33-
<dependency>
34-
<groupId>com.github.kstyrc</groupId>
35-
<artifactId>embedded-redis</artifactId>
36-
<version>${embedded-redis.version}</version>
37-
</dependency>
38-
</dependencies>
39-
40-
<properties>
41-
<embedded-redis.version>0.6</embedded-redis.version>
42-
</properties>
18+
<modules>
19+
<module>spring-session-jdbc</module>
20+
<module>spring-session-redis</module>
21+
</modules>
4322

4423
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This module is for Spring Session with JDBC tutorial.
2+
Jira BAEL-1911
3+
4+
### Relevant Articles:
5+
6+
- [Spring Session with JDBC](http://www.baeldung.com)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-session-jdbc</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>spring-session-jdbc</name>
11+
<description>Spring Session with JDBC tutorial</description>
12+
13+
<parent>
14+
<artifactId>parent-boot-2</artifactId>
15+
<groupId>com.baeldung</groupId>
16+
<version>0.0.1-SNAPSHOT</version>
17+
<relativePath>../../parent-boot-2</relativePath>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.session</groupId>
35+
<artifactId>spring-session-core</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.session</groupId>
49+
<artifactId>spring-session-jdbc</artifactId>
50+
</dependency>
51+
<!--<dependency>-->
52+
<!--<groupId>org.springframework.boot</groupId>-->
53+
<!--<artifactId>spring-boot-starter-security</artifactId>-->
54+
<!--</dependency>-->
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
67+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.springsessionjdbc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringSessionJdbcApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringSessionJdbcApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.springsessionjdbc.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.util.StringUtils;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpSession;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
@Controller
16+
public class SpringSessionJdbcController {
17+
18+
@GetMapping("/")
19+
public String index(Model model, HttpSession session) {
20+
List<String> favoriteColors = getFavColors(session);
21+
model.addAttribute("favoriteColors", favoriteColors);
22+
model.addAttribute("sessionId", session.getId());
23+
return "index";
24+
}
25+
26+
@PostMapping("/saveColor")
27+
public String saveMessage(@RequestParam("color") String color,
28+
HttpServletRequest request) {
29+
List<String> favoriteColors = getFavColors(request.getSession());
30+
if (!StringUtils.isEmpty(color)) {
31+
favoriteColors.add(color);
32+
request.getSession().
33+
setAttribute("favoriteColors", favoriteColors);
34+
}
35+
return "redirect:/";
36+
}
37+
38+
private List<String> getFavColors(HttpSession session) {
39+
List<String> favoriteColors = (List<String>) session.
40+
getAttribute("favoriteColors");
41+
if (favoriteColors == null) {
42+
favoriteColors = new ArrayList<>();
43+
}
44+
return favoriteColors;
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.session.store-type=jdbc
2+
#spring.session.jdbc.initialize-schema=embedded
3+
#spring.session.jdbc.table-name=SPRING_SESSION
4+
#server.servlet.session.timeout=60s
5+
#spring.datasource.url=jdbc:h2:mem:AZ
6+
#spring.security.user.name=admin
7+
#spring.security.user.password=secret
8+
spring.h2.console.enabled=true
9+
spring.h2.console.path=/h2-console
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Spring Session JDBC</title>
6+
</head>
7+
<body>
8+
<div>
9+
<form th:action="@{/saveColor}" method="post">
10+
<input name="color" type="text" placeholder="Enter your favorite color"/>
11+
<input type="submit" value="Save"/>
12+
</form>
13+
</div>
14+
<p>Session ID - <span th:text="${sessionId}"/></p>
15+
<p>My favorite color(s) - <span th:text="${favoriteColors}"/></p>
16+
</body>
17+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.springsessionjdbc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringSessionJdbcApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=========
2+
3+
## Spring Session Examples
4+
5+
### Relevant Articles:
6+
- [Introduction to Spring Session](http://www.baeldung.com/spring-session)

0 commit comments

Comments
 (0)