workings example for Spring session attributes (@SessionAttribute and @SessionAttributes)
This commit is contained in:
25
Spring-Boot/spring-session-attribute/.gitignore
vendored
Normal file
25
Spring-Boot/spring-session-attribute/.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
59
Spring-Boot/spring-session-attribute/pom.xml
Normal file
59
Spring-Boot/spring-session-attribute/pom.xml
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.javadevjournal</groupId>
|
||||
<artifactId>spring-session-attribute</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-session-attribute</name>
|
||||
<description>Spring Session Attribute sample application</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringSessionAttributeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringSessionAttributeApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.javadevjournal.controller;
|
||||
|
||||
import com.javadevjournal.model.ShoppingCart;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
|
||||
@Controller
|
||||
@SessionAttributes("shoppingCart")
|
||||
public class AddToCartController {
|
||||
|
||||
@PostMapping("/addToCart")
|
||||
public String addToCart(final Model model, @ModelAttribute ShoppingCart shoppingCart,final String productCode){
|
||||
if(shoppingCart!=null){
|
||||
//add product to the shopping cart list
|
||||
shoppingCart.setProduct(productCode);
|
||||
model.addAttribute("cart",shoppingCart);
|
||||
}
|
||||
else{
|
||||
ShoppingCart cart = new ShoppingCart();
|
||||
cart.setCustomerName("Super customer");
|
||||
cart.setProduct(productCode);
|
||||
model.addAttribute("cart",cart);
|
||||
}
|
||||
|
||||
return "redirect:"+"product-detail-page";
|
||||
}
|
||||
|
||||
@GetMapping("/product-detail-page")
|
||||
public String viewPDP(Model model,@ModelAttribute("shoppingCart") ShoppingCart shoppingCart){
|
||||
if(shoppingCart !=null){
|
||||
model.addAttribute("cart",shoppingCart);
|
||||
}
|
||||
else{
|
||||
model.addAttribute("cart",new ShoppingCart());
|
||||
}
|
||||
return "product";
|
||||
}
|
||||
|
||||
@ModelAttribute("shoppingCart")
|
||||
public ShoppingCart shoppingCart() {
|
||||
return new ShoppingCart();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal.controller;
|
||||
|
||||
import com.javadevjournal.model.ShoppingCart;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.SessionAttribute;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
|
||||
@Controller
|
||||
public class CartPageController {
|
||||
|
||||
@GetMapping("/cart")
|
||||
public String cart(@SessionAttribute("shoppingCart") ShoppingCart cart, final Model model){
|
||||
model.addAttribute("cart", cart);
|
||||
return "cart";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.javadevjournal.model;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ShoppingCart {
|
||||
|
||||
private String customerName;
|
||||
private List<String> products;
|
||||
private String product;
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public List<String> getProducts() {
|
||||
return products;
|
||||
}
|
||||
|
||||
public void setProducts(List<String> products) {
|
||||
this.products = products;
|
||||
}
|
||||
|
||||
|
||||
public void setProduct(String product) {
|
||||
if(CollectionUtils.isEmpty(this.getProducts())){
|
||||
List<String> products = new ArrayList<>();
|
||||
products.add(product);
|
||||
this.setProducts(products);
|
||||
}
|
||||
else {
|
||||
this.getProducts().add(product);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ShoppingCart)) return false;
|
||||
ShoppingCart that = (ShoppingCart) o;
|
||||
return Objects.equals(getCustomerName(), that.getCustomerName()) &&
|
||||
Objects.equals(getProducts(), that.getProducts());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
return Objects.hash(getCustomerName(), getProducts());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Shopping Cart Page</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Product Detail</h1>
|
||||
<h1>Product list</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Product Code Code</th>
|
||||
</tr>
|
||||
<tr th:each="prod : ${cart.products}">
|
||||
<td th:text="${prod}">Onions</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Product detail Page</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Product Detail</h1>
|
||||
<form action="#" th:action="@{/addToCart}" method="post">
|
||||
<p>Product Code: <input type="text" name="productCode"/></p>
|
||||
<p>Description: This is really an awesome page </p>
|
||||
<p><input type="submit" value="Add To Cart" />
|
||||
<a href="/cart" name="Cart Page">Go To cart Page</a>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringSessionAttributeApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user