Feature/BAEL-5575 (#14350)

* BAEL-5575

First commit of the example for the article:
https://drafts.baeldung.com/wp-admin/post.php?post=153774&action=edit

* BAEL-5575

Adjusted pom.xml

* BAEL-5575

Formatted source code

* BAEL-5575

Removed unused import

* BAEL-5575

Renamed UT to follow: https://docs.google.com/document/d/1iIeSPuYFG-jEtB8UbcxQnn2AI3OR2LWlnisNo2NUCL8/edit

* BAEL-5575

Modified pom.xml to define versions in  properties  as stated in:
https://docs.google.com/document/d/1iIeSPuYFG-jEtB8UbcxQnn2AI3OR2LWlnisNo2NUCL8/edit

* BAEL-5575

Changes checking the commands:
* mvn clean install
* mvn clean install -Dgib.enabled=false

* BAEL-5575

Set GB as the base language

* BAEL-5575

Removed explicit references to jakarta in pom.xml
Reduced code to instanciate Thymeleaf engine

* BAEL-5575

Adjusting parent artifactId
This commit is contained in:
victorsempere 2023-08-03 14:45:32 +02:00 committed by GitHub
parent de5e3f519d
commit d278ca2066
3 changed files with 120 additions and 175 deletions

View File

@ -1,95 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring-thymeleaf-attributes.module</groupId> <groupId>com.baeldung.spring-thymeleaf-attributes.module</groupId>
<artifactId>accessing-session-attributes</artifactId> <artifactId>accessing-session-attributes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung.spring-thymeleaf-attributes</groupId>
<artifactId>spring-thymeleaf-attributes</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <relativePath>../pom.xml</relativePath>
</parent>
<parent> <dependencies>
<groupId>com.baeldung.spring-thymeleaf-attributes</groupId> <dependency>
<artifactId>spring-thymeleaf-attributes</artifactId> <groupId>org.springframework.boot</groupId>
<version>0.0.1-SNAPSHOT</version> <artifactId>spring-boot-starter-web</artifactId>
<relativePath>../pom.xml</relativePath> </dependency>
</parent> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring.boot.starter.thymeleaf}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.engine.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies> <build>
<dependency> <plugins>
<groupId>jakarta.servlet</groupId> <plugin>
<artifactId>jakarta.servlet-api</artifactId> <groupId>org.springframework.boot</groupId>
<scope>provided</scope> <artifactId>spring-boot-maven-plugin</artifactId>
</dependency> <configuration>
<dependency> <jvmArguments>
<groupId>org.springframework.boot</groupId> -Dfile.encoding="UTF-8" -Xdebug
<artifactId>spring-boot-starter-web</artifactId> -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
</dependency> </jvmArguments>
<dependency> </configuration>
<groupId>org.thymeleaf</groupId> </plugin>
<artifactId>thymeleaf-spring6</artifactId> </plugins>
<version>${thymeleaf.spring6.version}</version> </build>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.engine.version}</version>
<scope>test</scope>
</dependency>
</dependencies> <properties>
<start-class>com.baeldung.accesing_session_attributes.SpringWebApplicationInitializer</start-class>
<build> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resources> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<resource> <file.encoding>UTF-8</file.encoding>
<directory>src/main/resources</directory> <downloadSources>true</downloadSources>
</resource> <downloadJavadocs>true</downloadJavadocs>
</resources> <junit.jupiter.engine.version>5.9.3</junit.jupiter.engine.version>
<mockito.version>5.3.1</mockito.version>
<plugins> <thymeleaf.spring6.version>3.1.1.RELEASE</thymeleaf.spring6.version>
<plugin> <spring.boot.starter.thymeleaf>3.1.1</spring.boot.starter.thymeleaf>
<groupId>org.springframework.boot</groupId> </properties>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Dfile.encoding="UTF-8" -Xdebug
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.accesing_session_attributes.SpringWebApplicationInitializer</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<file.encoding>UTF-8</file.encoding>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<junit.jupiter.engine.version>5.9.3</junit.jupiter.engine.version>
<mockito.version>5.3.1</mockito.version>
<thymeleaf.spring6.version>3.1.1.RELEASE</thymeleaf.spring6.version>
</properties>
</project> </project>

View File

@ -1,34 +1,26 @@
package com.baeldung.accesing_session_attributes.web; package com.baeldung.accesing_session_attributes.web;
import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
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.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
public class SpringWebConfig implements WebMvcConfigurer, ApplicationContextAware { public class SpringWebConfig implements WebMvcConfigurer { // , ApplicationContextAware {
private WebApplicationContext webApplicationContext; @Autowired
public SpringWebConfig(SpringResourceTemplateResolver templateResolver) {
public SpringWebConfig() {
super(); super();
}
@Override templateResolver.setPrefix("/WEB-INF/templates/");
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { templateResolver.setSuffix(".html");
this.webApplicationContext = (WebApplicationContext) applicationContext; templateResolver.setTemplateMode(TemplateMode.HTML);
} }
/* /*
@ -51,36 +43,4 @@ public class SpringWebConfig implements WebMvcConfigurer, ApplicationContextAwar
messageSource.addBasenames("name-analysis"); messageSource.addBasenames("name-analysis");
return messageSource; return messageSource;
} }
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.webApplicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(true);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setMessageSource(webApplicationContext);
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
@Bean
public JakartaServletWebApplication jakartaServletWebApplication() {
return JakartaServletWebApplication.buildApplication(webApplicationContext.getServletContext());
}
} }

View File

@ -20,6 +20,7 @@ import com.baeldung.accesing_session_attributes.business.entities.NameAnalysisEn
import com.baeldung.accesing_session_attributes.web.beans.SessionNameRequest; import com.baeldung.accesing_session_attributes.web.beans.SessionNameRequest;
import com.baeldung.accesing_session_attributes.web.factories.SessionNameRequestFactory; import com.baeldung.accesing_session_attributes.web.factories.SessionNameRequestFactory;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -27,19 +28,15 @@ import jakarta.servlet.http.HttpServletResponse;
public class NameAnalysisController { public class NameAnalysisController {
private NameAnalysisService nameAnalysisService; private NameAnalysisService nameAnalysisService;
private HttpServletRequest request;
private SessionNameRequestFactory sessionNameRequestFactory; private SessionNameRequestFactory sessionNameRequestFactory;
private HttpServletResponse response; private JakartaServletWebApplication webApp;
private JakartaServletWebApplication jakartaServletWebApplication;
@Autowired @Autowired
public NameAnalysisController(JakartaServletWebApplication jakartaServletWebApplication, HttpServletRequest request, HttpServletResponse response, NameAnalysisService nameAnalysisService, SessionNameRequestFactory sessionNameRequestFactory) { public NameAnalysisController(NameAnalysisService nameAnalysisService, SessionNameRequestFactory sessionNameRequestFactory, ServletContext servletContext) {
super(); super();
this.jakartaServletWebApplication = jakartaServletWebApplication;
this.request = request;
this.response = response;
this.nameAnalysisService = nameAnalysisService; this.nameAnalysisService = nameAnalysisService;
this.sessionNameRequestFactory = sessionNameRequestFactory; this.sessionNameRequestFactory = sessionNameRequestFactory;
this.webApp = JakartaServletWebApplication.buildApplication(servletContext);
} }
@ModelAttribute("nameRequest") @ModelAttribute("nameRequest")
@ -53,22 +50,25 @@ public class NameAnalysisController {
} }
@RequestMapping(value = "/name-analysis", params = { "search" }) @RequestMapping(value = "/name-analysis", params = { "search" })
public String performNameAnalysis(final NameRequest nameRequest, final BindingResult bindingResult) { public String performNameAnalysis(final NameRequest nameRequest, final BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) {
performNameRequest(nameRequest); IWebSession webSession = getIWebSession(request, response);
performNameRequest(nameRequest, webSession);
return "name-analysis"; return "name-analysis";
} }
@RequestMapping(value = "/name-analysis/clear") @RequestMapping(value = "/name-analysis/clear")
public String clearNameAnalysis() { public String clearNameAnalysis(HttpServletRequest request, HttpServletResponse response) {
clearAnalysis(); IWebSession webSession = getIWebSession(request, response);
clearAnalysis(webSession);
return "redirect:/name-analysis"; return "redirect:/name-analysis";
} }
@RequestMapping(value = "/name-analysis/remove-history-request", params = { "id" }) @RequestMapping(value = "/name-analysis/remove-history-request", params = { "id" })
public String removeRequest() { public String removeRequest(HttpServletRequest request, HttpServletResponse response) {
try { try {
IWebSession webSession = getIWebSession(request, response);
final Integer rowId = Integer.valueOf(request.getParameter("id")); final Integer rowId = Integer.valueOf(request.getParameter("id"));
removeRequest(rowId); removeRequest(rowId, webSession);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -76,53 +76,49 @@ public class NameAnalysisController {
return "redirect:/name-analysis"; return "redirect:/name-analysis";
} }
private void removeRequest(Integer rowId) { private void removeRequest(Integer rowId, IWebSession webSession) {
IWebSession session = getIWebSession(); if (rowId != null) {
Object requests = session.getAttributeValue("requests"); List<SessionNameRequest> requests = getRequestsFromSession(webSession);
if (rowId != null && requests != null && (requests instanceof List)) { if (requests != null) {
((List<SessionNameRequest>) requests).remove(rowId.intValue()); requests.remove(rowId.intValue());
}
} }
} }
private void performNameRequest(final NameRequest nameRequest) { private void performNameRequest(final NameRequest nameRequest, IWebSession webSession) {
try { try {
CompletableFuture<NameAnalysisEntity> nameAnalysis = this.nameAnalysisService.searchForName(nameRequest); CompletableFuture<NameAnalysisEntity> nameAnalysis = this.nameAnalysisService.searchForName(nameRequest);
NameAnalysisEntity nameAnalysisEntity = nameAnalysis.get(30, TimeUnit.SECONDS); NameAnalysisEntity nameAnalysisEntity = nameAnalysis.get(30, TimeUnit.SECONDS);
sessionRegisterRequest(nameRequest); sessionRegisterRequest(nameRequest, webSession);
sessionRegisterAnalysis(nameAnalysisEntity); sessionRegisterAnalysis(nameAnalysisEntity, webSession);
sessionClearAnalysisError(); sessionClearAnalysisError(webSession);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
sessionSetAnalysisError(nameRequest); sessionSetAnalysisError(nameRequest, webSession);
} }
} }
private void sessionClearAnalysisError() { private void sessionClearAnalysisError(IWebSession webSession) {
IWebSession session = getIWebSession(); webSession.removeAttribute("analysisError");
session.setAttributeValue("analysisError", null);
} }
private void sessionSetAnalysisError(NameRequest nameRequest) { private void sessionSetAnalysisError(NameRequest nameRequest, IWebSession webSession) {
IWebSession session = getIWebSession(); webSession.setAttributeValue("analysisError", nameRequest);
session.setAttributeValue("analysisError", nameRequest);
} }
private void clearAnalysis() { private void clearAnalysis(IWebSession webSession) {
IWebSession session = getIWebSession(); webSession.removeAttribute("lastAnalysis");
session.setAttributeValue("lastAnalysis", null);
} }
private void sessionRegisterAnalysis(NameAnalysisEntity analysis) { private void sessionRegisterAnalysis(NameAnalysisEntity analysis, IWebSession webSession) {
IWebSession session = getIWebSession(); webSession.setAttributeValue("lastAnalysis", analysis);
session.setAttributeValue("lastAnalysis", analysis);
} }
private void sessionRegisterRequest(NameRequest nameRequest) { private void sessionRegisterRequest(NameRequest nameRequest, IWebSession webSession) {
IWebSession session = getIWebSession(); webSession.setAttributeValue("lastRequest", nameRequest);
session.setAttributeValue("lastRequest", nameRequest);
SessionNameRequest sessionNameRequest = sessionNameRequestFactory.getInstance(nameRequest); SessionNameRequest sessionNameRequest = sessionNameRequestFactory.getInstance(nameRequest);
List<SessionNameRequest> requests = getRequestsFromSession(session); List<SessionNameRequest> requests = getRequestsFromSession(webSession);
requests.add(0, sessionNameRequest); requests.add(0, sessionNameRequest);
} }
@ -136,8 +132,8 @@ public class NameAnalysisController {
return (List<SessionNameRequest>) requests; return (List<SessionNameRequest>) requests;
} }
private IWebSession getIWebSession() { private IWebSession getIWebSession(HttpServletRequest request, HttpServletResponse response) {
IServletWebExchange webExchange = this.jakartaServletWebApplication.buildExchange(request, response); IServletWebExchange exchange = webApp.buildExchange(request, response);
return webExchange.getSession(); return exchange == null ? null : exchange.getSession();
} }
} }