Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
83d45139d9
25
apache-tika/pom.xml
Normal file
25
apache-tika/pom.xml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<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.baeldung</groupId>
|
||||||
|
<artifactId>apache-tika</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<tika.version>1.17</tika.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tika</groupId>
|
||||||
|
<artifactId>tika-parsers</artifactId>
|
||||||
|
<version>${tika.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.tika;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.apache.tika.Tika;
|
||||||
|
import org.apache.tika.detect.DefaultDetector;
|
||||||
|
import org.apache.tika.detect.Detector;
|
||||||
|
import org.apache.tika.exception.TikaException;
|
||||||
|
import org.apache.tika.metadata.Metadata;
|
||||||
|
import org.apache.tika.mime.MediaType;
|
||||||
|
import org.apache.tika.parser.AutoDetectParser;
|
||||||
|
import org.apache.tika.parser.ParseContext;
|
||||||
|
import org.apache.tika.parser.Parser;
|
||||||
|
import org.apache.tika.sax.BodyContentHandler;
|
||||||
|
import org.xml.sax.ContentHandler;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
public class TikaAnalysis {
|
||||||
|
public static String detectDocTypeUsingDetector(InputStream stream) throws IOException {
|
||||||
|
Detector detector = new DefaultDetector();
|
||||||
|
Metadata metadata = new Metadata();
|
||||||
|
|
||||||
|
MediaType mediaType = detector.detect(stream, metadata);
|
||||||
|
return mediaType.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String detectDocTypeUsingFacade(InputStream stream) throws IOException {
|
||||||
|
Tika tika = new Tika();
|
||||||
|
String mediaType = tika.detect(stream);
|
||||||
|
return mediaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String extractContentUsingParser(InputStream stream) throws IOException, TikaException, SAXException {
|
||||||
|
Parser parser = new AutoDetectParser();
|
||||||
|
ContentHandler handler = new BodyContentHandler();
|
||||||
|
Metadata metadata = new Metadata();
|
||||||
|
ParseContext context = new ParseContext();
|
||||||
|
|
||||||
|
parser.parse(stream, handler, metadata, context);
|
||||||
|
return handler.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String extractContentUsingFacade(InputStream stream) throws IOException, TikaException {
|
||||||
|
Tika tika = new Tika();
|
||||||
|
String content = tika.parseToString(stream);
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Metadata extractMetadatatUsingParser(InputStream stream) throws IOException, SAXException, TikaException {
|
||||||
|
Parser parser = new AutoDetectParser();
|
||||||
|
ContentHandler handler = new BodyContentHandler();
|
||||||
|
Metadata metadata = new Metadata();
|
||||||
|
ParseContext context = new ParseContext();
|
||||||
|
|
||||||
|
parser.parse(stream, handler, metadata, context);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Metadata extractMetadatatUsingFacade(InputStream stream) throws IOException, TikaException {
|
||||||
|
Tika tika = new Tika();
|
||||||
|
Metadata metadata = new Metadata();
|
||||||
|
|
||||||
|
tika.parse(stream, metadata);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package com.baeldung.tika;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.apache.tika.exception.TikaException;
|
||||||
|
import org.apache.tika.metadata.Metadata;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
public class TikaUnitTest {
|
||||||
|
@Test
|
||||||
|
public void whenUsingDetector_thenDocumentTypeIsReturned() throws IOException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.txt");
|
||||||
|
String mediaType = TikaAnalysis.detectDocTypeUsingDetector(stream);
|
||||||
|
|
||||||
|
assertEquals("application/pdf", mediaType);
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingFacade_thenDocumentTypeIsReturned() throws IOException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.txt");
|
||||||
|
String mediaType = TikaAnalysis.detectDocTypeUsingFacade(stream);
|
||||||
|
|
||||||
|
assertEquals("application/pdf", mediaType);
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingParser_thenContentIsReturned() throws IOException, TikaException, SAXException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.docx");
|
||||||
|
String content = TikaAnalysis.extractContentUsingParser(stream);
|
||||||
|
|
||||||
|
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
|
||||||
|
assertThat(content, containsString("detects and extracts metadata and text"));
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingFacade_thenContentIsReturned() throws IOException, TikaException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.docx");
|
||||||
|
String content = TikaAnalysis.extractContentUsingFacade(stream);
|
||||||
|
|
||||||
|
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
|
||||||
|
assertThat(content, containsString("detects and extracts metadata and text"));
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingParser_thenMetadataIsReturned() throws IOException, TikaException, SAXException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.xlsx");
|
||||||
|
Metadata metadata = TikaAnalysis.extractMetadatatUsingParser(stream);
|
||||||
|
|
||||||
|
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
|
||||||
|
assertEquals("Microsoft Office User", metadata.get("Author"));
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingFacade_thenMetadataIsReturned() throws IOException, TikaException {
|
||||||
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.xlsx");
|
||||||
|
Metadata metadata = TikaAnalysis.extractMetadatatUsingFacade(stream);
|
||||||
|
|
||||||
|
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
|
||||||
|
assertEquals("Microsoft Office User", metadata.get("Author"));
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
}
|
BIN
apache-tika/src/test/resources/tika.docx
Normal file
BIN
apache-tika/src/test/resources/tika.docx
Normal file
Binary file not shown.
BIN
apache-tika/src/test/resources/tika.txt
Normal file
BIN
apache-tika/src/test/resources/tika.txt
Normal file
Binary file not shown.
BIN
apache-tika/src/test/resources/tika.xlsx
Normal file
BIN
apache-tika/src/test/resources/tika.xlsx
Normal file
Binary file not shown.
@ -23,9 +23,15 @@ public class SuperHero extends Person {
|
|||||||
* @return the amount of health hero has after attack
|
* @return the amount of health hero has after attack
|
||||||
* @see <a href="http://www.link_to_jira/HERO-402">HERO-402</a>
|
* @see <a href="http://www.link_to_jira/HERO-402">HERO-402</a>
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
|
* @deprecated As of version 1.1, use . . . instead
|
||||||
|
* @version 1.2
|
||||||
|
* @throws IllegalArgumentException if incomingDamage is negative
|
||||||
*/
|
*/
|
||||||
public int successfullyAttacked(int incomingDamage, String damageType) {
|
public int successfullyAttacked(int incomingDamage, String damageType) throws Exception {
|
||||||
// do things
|
// do things
|
||||||
|
if (incomingDamage < 0) {
|
||||||
|
throw new IllegalArgumentException ("Cannot cause negative damage");
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
pom.xml
4
pom.xml
@ -38,6 +38,7 @@
|
|||||||
<module>apache-cxf</module>
|
<module>apache-cxf</module>
|
||||||
<module>apache-fop</module>
|
<module>apache-fop</module>
|
||||||
<module>apache-poi</module>
|
<module>apache-poi</module>
|
||||||
|
<module>apache-tika</module>
|
||||||
<module>apache-thrift</module>
|
<module>apache-thrift</module>
|
||||||
<module>apache-curator</module>
|
<module>apache-curator</module>
|
||||||
<module>apache-zookeeper</module>
|
<module>apache-zookeeper</module>
|
||||||
@ -199,7 +200,8 @@
|
|||||||
<module>spring-ldap</module>
|
<module>spring-ldap</module>
|
||||||
<module>spring-mockito</module>
|
<module>spring-mockito</module>
|
||||||
<module>spring-mvc-email</module>
|
<module>spring-mvc-email</module>
|
||||||
<module>spring-mvc-forms</module>
|
<module>spring-mvc-forms-jsp</module>
|
||||||
|
<module>spring-mvc-forms-thymeleaf</module>
|
||||||
<module>spring-mvc-java</module>
|
<module>spring-mvc-java</module>
|
||||||
<module>spring-mvc-tiles</module>
|
<module>spring-mvc-tiles</module>
|
||||||
<module>spring-mvc-velocity</module>
|
<module>spring-mvc-velocity</module>
|
||||||
|
437
spring-5/pom.xml
437
spring-5/pom.xml
@ -1,215 +1,222 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-5</artifactId>
|
<artifactId>spring-5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>spring-5</name>
|
<name>spring-5</name>
|
||||||
<description>spring 5 sample project about new features</description>
|
<description>spring 5 sample project about new features</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.0.0.RELEASE</version>
|
<version>2.0.0.RELEASE</version>
|
||||||
<relativePath /> <!-- lookup parent from repository -->
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-validation</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.projectreactor</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>reactor-spring</artifactId>
|
||||||
</dependency>
|
<version>${reactor-spring.version}</version>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.projectreactor</groupId>
|
<dependency>
|
||||||
<artifactId>reactor-spring</artifactId>
|
<groupId>javax.json.bind</groupId>
|
||||||
<version>${reactor-spring.version}</version>
|
<artifactId>javax.json.bind-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- Dependencies for Yasson -->
|
||||||
<groupId>javax.json.bind</groupId>
|
<!-- <dependency> -->
|
||||||
<artifactId>javax.json.bind-api</artifactId>
|
<!-- <groupId>org.eclipse</groupId> -->
|
||||||
</dependency>
|
<!-- <artifactId>yasson</artifactId> -->
|
||||||
<dependency>
|
<!-- <version>1.0</version> -->
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<!-- </dependency> -->
|
||||||
<artifactId>geronimo-json_1.1_spec</artifactId>
|
<!-- <dependency> -->
|
||||||
<version>${geronimo-json_1.1_spec.version}</version>
|
<!-- <groupId>org.glassfish</groupId> -->
|
||||||
</dependency>
|
<!-- <artifactId>javax.json</artifactId> -->
|
||||||
<dependency>
|
<!-- <version>1.1.2</version> -->
|
||||||
<groupId>org.apache.johnzon</groupId>
|
<!-- </dependency> -->
|
||||||
<artifactId>johnzon-jsonb</artifactId>
|
<!-- Dependencies for Johnzon -->
|
||||||
<version>${johnzon.version}</version>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<!-- utils -->
|
<artifactId>geronimo-json_1.1_spec</artifactId>
|
||||||
<dependency>
|
<version>${geronimo-json_1.1_spec.version}</version>
|
||||||
<groupId>org.apache.commons</groupId>
|
</dependency>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.apache.johnzon</groupId>
|
||||||
|
<artifactId>johnzon-jsonb</artifactId>
|
||||||
<!-- runtime and test scoped -->
|
</dependency>
|
||||||
|
<!-- utils -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<scope>runtime</scope>
|
</dependency>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<!-- runtime and test scoped -->
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
<dependency>
|
||||||
<scope>runtime</scope>
|
<groupId>org.springframework.boot</groupId>
|
||||||
</dependency>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<dependency>
|
||||||
<artifactId>spring-test</artifactId>
|
<groupId>com.h2database</groupId>
|
||||||
</dependency>
|
<artifactId>h2</artifactId>
|
||||||
<dependency>
|
<scope>runtime</scope>
|
||||||
<groupId>org.springframework.boot</groupId>
|
</dependency>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
|
||||||
<scope>test</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.springframework</groupId>
|
||||||
<dependency>
|
<artifactId>spring-test</artifactId>
|
||||||
<groupId>org.springframework.security</groupId>
|
</dependency>
|
||||||
<artifactId>spring-security-test</artifactId>
|
<dependency>
|
||||||
<scope>test</scope>
|
<groupId>org.springframework.boot</groupId>
|
||||||
</dependency>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<dependency>
|
||||||
<artifactId>commons-collections4</artifactId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<version>4.1</version>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>commons-collections4</artifactId>
|
||||||
</dependency>
|
<version>4.1</version>
|
||||||
<dependency>
|
<scope>test</scope>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
</dependency>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<scope>test</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<dependency>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<groupId>org.junit.platform</groupId>
|
</dependency>
|
||||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
<dependency>
|
||||||
<version>${junit.platform.version}</version>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<scope>test</scope>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
</dependency>
|
<scope>test</scope>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.junit.platform</groupId>
|
<dependency>
|
||||||
<artifactId>junit-platform-runner</artifactId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<version>${junit.platform.version}</version>
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
<scope>test</scope>
|
<version>${junit.platform.version}</version>
|
||||||
</dependency>
|
<scope>test</scope>
|
||||||
<!-- restdocs -->
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.restdocs</groupId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<artifactId>spring-restdocs-mockmvc</artifactId>
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
<scope>test</scope>
|
<version>${junit.platform.version}</version>
|
||||||
</dependency>
|
<scope>test</scope>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.springframework.restdocs</groupId>
|
<!-- restdocs -->
|
||||||
<artifactId>spring-restdocs-webtestclient</artifactId>
|
<dependency>
|
||||||
<scope>test</scope>
|
<groupId>org.springframework.restdocs</groupId>
|
||||||
</dependency>
|
<artifactId>spring-restdocs-mockmvc</artifactId>
|
||||||
<dependency>
|
<scope>test</scope>
|
||||||
<groupId>org.springframework.restdocs</groupId>
|
</dependency>
|
||||||
<artifactId>spring-restdocs-restassured</artifactId>
|
<dependency>
|
||||||
<scope>test</scope>
|
<groupId>org.springframework.restdocs</groupId>
|
||||||
</dependency>
|
<artifactId>spring-restdocs-webtestclient</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.springframework.restdocs</groupId>
|
||||||
<build>
|
<artifactId>spring-restdocs-restassured</artifactId>
|
||||||
<plugins>
|
<scope>test</scope>
|
||||||
<plugin>
|
</dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
<configuration>
|
</dependencies>
|
||||||
<mainClass>com.baeldung.Spring5Application</mainClass>
|
|
||||||
<layout>JAR</layout>
|
<build>
|
||||||
</configuration>
|
<plugins>
|
||||||
</plugin>
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
<plugin>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<configuration>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<mainClass>com.baeldung.Spring5Application</mainClass>
|
||||||
<configuration>
|
<layout>JAR</layout>
|
||||||
<forkCount>3</forkCount>
|
</configuration>
|
||||||
<reuseForks>true</reuseForks>
|
</plugin>
|
||||||
<parallel>methods</parallel>
|
|
||||||
<useUnlimitedThreads>true</useUnlimitedThreads>
|
<plugin>
|
||||||
<excludes>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<configuration>
|
||||||
</excludes>
|
<forkCount>3</forkCount>
|
||||||
</configuration>
|
<reuseForks>true</reuseForks>
|
||||||
</plugin>
|
<parallel>methods</parallel>
|
||||||
<plugin>
|
<useUnlimitedThreads>true</useUnlimitedThreads>
|
||||||
<groupId>org.asciidoctor</groupId>
|
<excludes>
|
||||||
<artifactId>asciidoctor-maven-plugin</artifactId>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
<version>${asciidoctor-plugin.version}</version>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
<executions>
|
</excludes>
|
||||||
<execution>
|
</configuration>
|
||||||
<id>generate-docs</id>
|
</plugin>
|
||||||
<phase>package</phase>
|
<plugin>
|
||||||
<goals>
|
<groupId>org.asciidoctor</groupId>
|
||||||
<goal>process-asciidoc</goal>
|
<artifactId>asciidoctor-maven-plugin</artifactId>
|
||||||
</goals>
|
<version>${asciidoctor-plugin.version}</version>
|
||||||
<configuration>
|
<executions>
|
||||||
<backend>html</backend>
|
<execution>
|
||||||
<doctype>book</doctype>
|
<id>generate-docs</id>
|
||||||
<attributes>
|
<phase>package</phase>
|
||||||
<snippets>${snippetsDirectory}</snippets>
|
<goals>
|
||||||
</attributes>
|
<goal>process-asciidoc</goal>
|
||||||
<sourceDirectory>src/docs/asciidocs</sourceDirectory>
|
</goals>
|
||||||
<outputDirectory>target/generated-docs</outputDirectory>
|
<configuration>
|
||||||
</configuration>
|
<backend>html</backend>
|
||||||
</execution>
|
<doctype>book</doctype>
|
||||||
</executions>
|
<attributes>
|
||||||
</plugin>
|
<snippets>${snippetsDirectory}</snippets>
|
||||||
</plugins>
|
</attributes>
|
||||||
</build>
|
<sourceDirectory>src/docs/asciidocs</sourceDirectory>
|
||||||
|
<outputDirectory>target/generated-docs</outputDirectory>
|
||||||
<properties>
|
</configuration>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
</execution>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
</executions>
|
||||||
<java.version>1.8</java.version>
|
</plugin>
|
||||||
<junit.platform.version>1.0.0</junit.platform.version>
|
</plugins>
|
||||||
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
|
</build>
|
||||||
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
|
|
||||||
<johnzon.version>1.1.3</johnzon.version>
|
<properties>
|
||||||
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
|
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
|
||||||
</project>
|
<spring.version>5.0.2.RELEASE</spring.version>
|
||||||
|
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
|
||||||
|
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
||||||
|
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
|
||||||
|
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<artifactId>spring-mvc-forms</artifactId>
|
<artifactId>spring-mvc-forms-jsp</artifactId>
|
||||||
|
|
||||||
<name>spring-mvc-forms</name>
|
<name>spring-mvc-forms-jsp</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
73
spring-mvc-forms-thymeleaf/pom.xml
Normal file
73
spring-mvc-forms-thymeleaf/pom.xml
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?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.baeldung</groupId>
|
||||||
|
<artifactId>spring-mvc-forms-thymeleaf</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-mvc-forms-thymeleaf</name>
|
||||||
|
<description>spring forms examples using thymeleaf</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.0.RELEASE</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- runtime and test scoped -->
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<forkCount>3</forkCount>
|
||||||
|
<reuseForks>true</reuseForks>
|
||||||
|
<parallel>methods</parallel>
|
||||||
|
<useUnlimitedThreads>true</useUnlimitedThreads>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -1,44 +1,44 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.context.annotation.ScopedProxyMode;
|
import org.springframework.context.annotation.ScopedProxyMode;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.thymeleaf.templatemode.TemplateMode;
|
import org.thymeleaf.templatemode.TemplateMode;
|
||||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||||
|
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@Configuration
|
@Configuration
|
||||||
public class Config implements WebMvcConfigurer {
|
public class Config implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(ViewControllerRegistry registry) {
|
public void addViewControllers(ViewControllerRegistry registry) {
|
||||||
registry.addViewController("/").setViewName("index");
|
registry.addViewController("/").setViewName("index");
|
||||||
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Scope(
|
@Scope(
|
||||||
value = WebApplicationContext.SCOPE_SESSION,
|
value = WebApplicationContext.SCOPE_SESSION,
|
||||||
proxyMode = ScopedProxyMode.TARGET_CLASS)
|
proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||||
public TodoList todos() {
|
public TodoList todos() {
|
||||||
return new TodoList();
|
return new TodoList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ITemplateResolver templateResolver() {
|
public ITemplateResolver templateResolver() {
|
||||||
ClassLoaderTemplateResolver resolver
|
ClassLoaderTemplateResolver resolver
|
||||||
= new ClassLoaderTemplateResolver();
|
= new ClassLoaderTemplateResolver();
|
||||||
resolver.setPrefix("templates/sessionattrs/");
|
resolver.setPrefix("templates/sessionattrs/");
|
||||||
resolver.setSuffix(".html");
|
resolver.setSuffix(".html");
|
||||||
resolver.setTemplateMode(TemplateMode.HTML);
|
resolver.setTemplateMode(TemplateMode.HTML);
|
||||||
resolver.setCharacterEncoding("UTF-8");
|
resolver.setCharacterEncoding("UTF-8");
|
||||||
return resolver;
|
return resolver;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,45 +1,45 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/scopedproxy")
|
@RequestMapping("/scopedproxy")
|
||||||
public class TodoControllerWithScopedProxy {
|
public class TodoControllerWithScopedProxy {
|
||||||
|
|
||||||
private TodoList todos;
|
private TodoList todos;
|
||||||
|
|
||||||
public TodoControllerWithScopedProxy(TodoList todos) {
|
public TodoControllerWithScopedProxy(TodoList todos) {
|
||||||
this.todos = todos;
|
this.todos = todos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/form")
|
@GetMapping("/form")
|
||||||
public String showForm(Model model) {
|
public String showForm(Model model) {
|
||||||
if (!todos.isEmpty()) {
|
if (!todos.isEmpty()) {
|
||||||
model.addAttribute("todo", todos.peekLast());
|
model.addAttribute("todo", todos.peekLast());
|
||||||
} else {
|
} else {
|
||||||
model.addAttribute("todo", new TodoItem());
|
model.addAttribute("todo", new TodoItem());
|
||||||
}
|
}
|
||||||
|
|
||||||
return "scopedproxyform";
|
return "scopedproxyform";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/form")
|
@PostMapping("/form")
|
||||||
public String create(@ModelAttribute TodoItem todo) {
|
public String create(@ModelAttribute TodoItem todo) {
|
||||||
todo.setCreateDate(LocalDateTime.now());
|
todo.setCreateDate(LocalDateTime.now());
|
||||||
todos.add(todo);
|
todos.add(todo);
|
||||||
return "redirect:/scopedproxy/todos.html";
|
return "redirect:/scopedproxy/todos.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/todos.html")
|
@GetMapping("/todos.html")
|
||||||
public String list(Model model) {
|
public String list(Model model) {
|
||||||
model.addAttribute("todos", todos);
|
model.addAttribute("todos", todos);
|
||||||
return "scopedproxytodos";
|
return "scopedproxytodos";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,55 +1,55 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
import org.springframework.web.servlet.view.RedirectView;
|
import org.springframework.web.servlet.view.RedirectView;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/sessionattributes")
|
@RequestMapping("/sessionattributes")
|
||||||
@SessionAttributes("todos")
|
@SessionAttributes("todos")
|
||||||
public class TodoControllerWithSessionAttributes {
|
public class TodoControllerWithSessionAttributes {
|
||||||
|
|
||||||
@GetMapping("/form")
|
@GetMapping("/form")
|
||||||
public String showForm(
|
public String showForm(
|
||||||
Model model,
|
Model model,
|
||||||
@ModelAttribute("todos") TodoList todos) {
|
@ModelAttribute("todos") TodoList todos) {
|
||||||
if (!todos.isEmpty()) {
|
if (!todos.isEmpty()) {
|
||||||
model.addAttribute("todo", todos.peekLast());
|
model.addAttribute("todo", todos.peekLast());
|
||||||
} else {
|
} else {
|
||||||
model.addAttribute("todo", new TodoItem());
|
model.addAttribute("todo", new TodoItem());
|
||||||
}
|
}
|
||||||
return "sessionattributesform";
|
return "sessionattributesform";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/form")
|
@PostMapping("/form")
|
||||||
public RedirectView create(
|
public RedirectView create(
|
||||||
@ModelAttribute TodoItem todo,
|
@ModelAttribute TodoItem todo,
|
||||||
@ModelAttribute("todos") TodoList todos,
|
@ModelAttribute("todos") TodoList todos,
|
||||||
RedirectAttributes attributes) {
|
RedirectAttributes attributes) {
|
||||||
todo.setCreateDate(LocalDateTime.now());
|
todo.setCreateDate(LocalDateTime.now());
|
||||||
todos.add(todo);
|
todos.add(todo);
|
||||||
attributes.addFlashAttribute("todos", todos);
|
attributes.addFlashAttribute("todos", todos);
|
||||||
return new RedirectView("/sessionattributes/todos.html");
|
return new RedirectView("/sessionattributes/todos.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/todos.html")
|
@GetMapping("/todos.html")
|
||||||
public String list(
|
public String list(
|
||||||
Model model,
|
Model model,
|
||||||
@ModelAttribute("todos") TodoList todos) {
|
@ModelAttribute("todos") TodoList todos) {
|
||||||
model.addAttribute("todos", todos);
|
model.addAttribute("todos", todos);
|
||||||
return "sessionattributestodos";
|
return "sessionattributestodos";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("todos")
|
@ModelAttribute("todos")
|
||||||
public TodoList todos() {
|
public TodoList todos() {
|
||||||
return new TodoList();
|
return new TodoList();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,39 +1,39 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public class TodoItem {
|
public class TodoItem {
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
private LocalDateTime createDate;
|
private LocalDateTime createDate;
|
||||||
|
|
||||||
public TodoItem(String description, LocalDateTime createDate) {
|
public TodoItem(String description, LocalDateTime createDate) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.createDate = createDate;
|
this.createDate = createDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TodoItem() {
|
public TodoItem() {
|
||||||
// default no arg constructor
|
// default no arg constructor
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDescription(String description) {
|
public void setDescription(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalDateTime getCreateDate() {
|
public LocalDateTime getCreateDate() {
|
||||||
return createDate;
|
return createDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCreateDate(LocalDateTime createDate) {
|
public void setCreateDate(LocalDateTime createDate) {
|
||||||
this.createDate = createDate;
|
this.createDate = createDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "TodoItem [description=" + description + ", createDate=" + createDate + "]";
|
return "TodoItem [description=" + description + ", createDate=" + createDate + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class TodoList extends ArrayDeque<TodoItem>{
|
public class TodoList extends ArrayDeque<TodoItem>{
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
server.port=8081
|
||||||
|
|
||||||
|
logging.level.root=INFO
|
@ -1,27 +1,27 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Session Scope in Spring MVC</title>
|
<title>Session Attributes in Spring MVC</title>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"
|
||||||
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>
|
<p>
|
||||||
<h3>Session Scope in Spring MVC - Example</h3>
|
<h3>Session Scope in Spring MVC - Example</h3>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<a href="/scopedproxy/form" class="btn btn-sm btn-primary btn-block float-right" role="button">Session Scoped Proxy Example</a></div>
|
<a href="/scopedproxy/form" class="btn btn-sm btn-primary btn-block float-right" role="button">Session Scoped Proxy Example</a></div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<a href="/sessionattributes/form" class="btn btn-sm btn-primary btn-block float-right" role="button">Session Attributes Example</a></div>
|
<a href="/sessionattributes/form" class="btn btn-sm btn-primary btn-block float-right" role="button">Session Attributes Example</a></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,27 +1,27 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Session Scope in Spring MVC</title>
|
<title>Session Attributes in Spring MVC</title>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>
|
<p>
|
||||||
<h3>Scoped Proxy Example</h3>
|
<h3>Scoped Proxy Example</h3>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h5>Enter a TODO</h5>
|
<h5>Enter a TODO</h5>
|
||||||
<form action="#" th:action="@{/scopedproxy/form}" th:object="${todo}" method="post">
|
<form action="#" th:action="@{/scopedproxy/form}" th:object="${todo}" method="post">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="text" th:field="*{description}" class="form-control" placeholder="TODO" required autofocus/>
|
<input type="text" th:field="*{description}" class="form-control" placeholder="TODO" required autofocus/>
|
||||||
<button class="btn btn-sm btn-primary form-control text-center" type="submit" style="max-width: 80px;">Create</button>
|
<button class="btn btn-sm btn-primary form-control text-center" type="submit" style="max-width: 80px;">Create</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,48 +1,48 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Session Scope in Spring MVC</title>
|
<title>Session Attributes in Spring MVC</title>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
|
||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M"
|
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M"
|
||||||
crossorigin="anonymous">
|
crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>
|
<p>
|
||||||
<h3>Scoped Proxy Example</h3>
|
<h3>Scoped Proxy Example</h3>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<br/>
|
<br/>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<a th:href="@{/scopedproxy/form}" class="btn btn-sm btn-primary btn-block" role="button"
|
<a th:href="@{/scopedproxy/form}" class="btn btn-sm btn-primary btn-block" role="button"
|
||||||
style="width: 10em;">Add New</a>
|
style="width: 10em;">Add New</a>
|
||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col card" style="padding: 1em;">
|
<div class="col card" style="padding: 1em;">
|
||||||
<div class="card-block">
|
<div class="card-block">
|
||||||
<h5 class="card-title">TODO List</h5>
|
<h5 class="card-title">TODO List</h5>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<th width="75%">Description</th>
|
<th width="75%">Description</th>
|
||||||
<th width="25%" >Create Date</th>
|
<th width="25%" >Create Date</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr th:each="todo : ${todos}">
|
<tr th:each="todo : ${todos}">
|
||||||
<td th:text="${todo.description}">Description</td>
|
<td th:text="${todo.description}">Description</td>
|
||||||
<td th:text="${#temporals.format(todo.createDate, 'MM-dd-yyyy HH:mm:ss')}">Create Date</td>
|
<td th:text="${#temporals.format(todo.createDate, 'MM-dd-yyyy HH:mm:ss')}">Create Date</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,27 +1,27 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Session Scope in Spring MVC</title>
|
<title>Session Attributes in Spring MVC</title>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>
|
<p>
|
||||||
<h3>Session Attributes Example</h3>
|
<h3>Session Attributes Example</h3>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h5>Enter a TODO</h5>
|
<h5>Enter a TODO</h5>
|
||||||
<form action="#" th:action="@{/sessionattributes/form}" th:object="${todo}" method="post">
|
<form action="#" th:action="@{/sessionattributes/form}" th:object="${todo}" method="post">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="text" th:field="*{description}" class="form-control" placeholder="TODO" required autofocus/>
|
<input type="text" th:field="*{description}" class="form-control" placeholder="TODO" required autofocus/>
|
||||||
<button class="btn btn-sm btn-primary form-control text-center" type="submit" style="max-width: 80px;">Create</button>
|
<button class="btn btn-sm btn-primary form-control text-center" type="submit" style="max-width: 80px;">Create</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,48 +1,48 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Session Scope in Spring MVC</title>
|
<title>Session Attributes in Spring MVC</title>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
|
||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M"
|
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M"
|
||||||
crossorigin="anonymous">
|
crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>
|
<p>
|
||||||
<h3>Session Attributes Example</h3>
|
<h3>Session Attributes Example</h3>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<br/>
|
<br/>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<a th:href="@{/sessionattributes/form}" class="btn btn-sm btn-primary btn-block" role="button"
|
<a th:href="@{/sessionattributes/form}" class="btn btn-sm btn-primary btn-block" role="button"
|
||||||
style="width: 10em;">Add New</a>
|
style="width: 10em;">Add New</a>
|
||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col card" style="padding: 1em;">
|
<div class="col card" style="padding: 1em;">
|
||||||
<div class="card-block">
|
<div class="card-block">
|
||||||
<h5 class="card-title">TODO List</h5>
|
<h5 class="card-title">TODO List</h5>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<th width="75%">Description</th>
|
<th width="75%">Description</th>
|
||||||
<th width="25%" >Create Date</th>
|
<th width="25%" >Create Date</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr th:each="todo : ${todos}">
|
<tr th:each="todo : ${todos}">
|
||||||
<td th:text="${todo.description}">Description</td>
|
<td th:text="${todo.description}">Description</td>
|
||||||
<td th:text="${#temporals.format(todo.createDate, 'MM-dd-yyyy HH:mm:ss')}">Create Date</td>
|
<td th:text="${#temporals.format(todo.createDate, 'MM-dd-yyyy HH:mm:ss')}">Create Date</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class SessionAttrsApplicationIntegrationTest {
|
public class SessionAttrsApplicationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
@ -1,17 +1,17 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import org.springframework.beans.factory.config.CustomScopeConfigurer;
|
import org.springframework.beans.factory.config.CustomScopeConfigurer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.support.SimpleThreadScope;
|
import org.springframework.context.support.SimpleThreadScope;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TestConfig {
|
public class TestConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CustomScopeConfigurer customScopeConfigurer() {
|
public CustomScopeConfigurer customScopeConfigurer() {
|
||||||
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
|
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
|
||||||
configurer.addScope("session", new SimpleThreadScope());
|
configurer.addScope("session", new SimpleThreadScope());
|
||||||
return configurer;
|
return configurer;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,68 +1,68 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
@Import(TestConfig.class)
|
@Import(TestConfig.class)
|
||||||
public class TodoControllerWithScopedProxyIntegrationTest {
|
public class TodoControllerWithScopedProxyTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenFirstRequest_thenContainsAllCategoriesAndUnintializedTodo() throws Exception {
|
public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception {
|
||||||
MvcResult result = mockMvc.perform(get("/scopedproxy/form"))
|
MvcResult result = mockMvc.perform(get("/scopedproxy/form"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists("todo"))
|
.andExpect(model().attributeExists("todo"))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
|
||||||
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
||||||
assertTrue(StringUtils.isEmpty(item.getDescription()));
|
assertTrue(StringUtils.isEmpty(item.getDescription()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception {
|
public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception {
|
||||||
mockMvc.perform(post("/scopedproxy/form")
|
mockMvc.perform(post("/scopedproxy/form")
|
||||||
.param("description", "newtodo"))
|
.param("description", "newtodo"))
|
||||||
.andExpect(status().is3xxRedirection())
|
.andExpect(status().is3xxRedirection())
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
|
||||||
MvcResult result = mockMvc.perform(get("/scopedproxy/form"))
|
MvcResult result = mockMvc.perform(get("/scopedproxy/form"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists("todo"))
|
.andExpect(model().attributeExists("todo"))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
||||||
assertEquals("newtodo", item.getDescription());
|
assertEquals("newtodo", item.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,68 +1,68 @@
|
|||||||
package com.baeldung.sessionattrs;
|
package com.baeldung.sessionattrs;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.servlet.FlashMap;
|
import org.springframework.web.servlet.FlashMap;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
public class TodoControllerWithSessionAttributesIntegrationTest {
|
public class TodoControllerWithSessionAttributesTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception {
|
public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception {
|
||||||
MvcResult result = mockMvc.perform(get("/sessionattributes/form"))
|
MvcResult result = mockMvc.perform(get("/sessionattributes/form"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists("todo"))
|
.andExpect(model().attributeExists("todo"))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
|
||||||
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
||||||
assertTrue(StringUtils.isEmpty(item.getDescription()));
|
assertTrue(StringUtils.isEmpty(item.getDescription()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception {
|
public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception {
|
||||||
FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form")
|
FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form")
|
||||||
.param("description", "newtodo"))
|
.param("description", "newtodo"))
|
||||||
.andExpect(status().is3xxRedirection())
|
.andExpect(status().is3xxRedirection())
|
||||||
.andReturn().getFlashMap();
|
.andReturn().getFlashMap();
|
||||||
|
|
||||||
MvcResult result = mockMvc.perform(get("/sessionattributes/form")
|
MvcResult result = mockMvc.perform(get("/sessionattributes/form")
|
||||||
.sessionAttrs(flashMap))
|
.sessionAttrs(flashMap))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists("todo"))
|
.andExpect(model().attributeExists("todo"))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
|
||||||
assertEquals("newtodo", item.getDescription());
|
assertEquals("newtodo", item.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user