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:
parent
de5e3f519d
commit
d278ca2066
|
@ -1,95 +1,84 @@
|
|||
<?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 https://maven.apache.org/maven-v4_0_0.xsd">
|
||||
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">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.spring-thymeleaf-attributes.module</groupId>
|
||||
<artifactId>accessing-session-attributes</artifactId>
|
||||
<groupId>com.baeldung.spring-thymeleaf-attributes.module</groupId>
|
||||
<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>
|
||||
<packaging>war</packaging>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-thymeleaf-attributes</groupId>
|
||||
<artifactId>spring-thymeleaf-attributes</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</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>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring6</artifactId>
|
||||
<version>${thymeleaf.spring6.version}</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>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<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>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<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>
|
||||
<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>
|
||||
<spring.boot.starter.thymeleaf>3.1.1</spring.boot.starter.thymeleaf>
|
||||
</properties>
|
||||
</project>
|
|
@ -1,34 +1,26 @@
|
|||
package com.baeldung.accesing_session_attributes.web;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.thymeleaf.spring6.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class SpringWebConfig implements WebMvcConfigurer, ApplicationContextAware {
|
||||
public class SpringWebConfig implements WebMvcConfigurer { // , ApplicationContextAware {
|
||||
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
public SpringWebConfig() {
|
||||
@Autowired
|
||||
public SpringWebConfig(SpringResourceTemplateResolver templateResolver) {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.webApplicationContext = (WebApplicationContext) applicationContext;
|
||||
templateResolver.setPrefix("/WEB-INF/templates/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode(TemplateMode.HTML);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -51,36 +43,4 @@ public class SpringWebConfig implements WebMvcConfigurer, ApplicationContextAwar
|
|||
messageSource.addBasenames("name-analysis");
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.factories.SessionNameRequestFactory;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -27,19 +28,15 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||
public class NameAnalysisController {
|
||||
|
||||
private NameAnalysisService nameAnalysisService;
|
||||
private HttpServletRequest request;
|
||||
private SessionNameRequestFactory sessionNameRequestFactory;
|
||||
private HttpServletResponse response;
|
||||
private JakartaServletWebApplication jakartaServletWebApplication;
|
||||
private JakartaServletWebApplication webApp;
|
||||
|
||||
@Autowired
|
||||
public NameAnalysisController(JakartaServletWebApplication jakartaServletWebApplication, HttpServletRequest request, HttpServletResponse response, NameAnalysisService nameAnalysisService, SessionNameRequestFactory sessionNameRequestFactory) {
|
||||
public NameAnalysisController(NameAnalysisService nameAnalysisService, SessionNameRequestFactory sessionNameRequestFactory, ServletContext servletContext) {
|
||||
super();
|
||||
this.jakartaServletWebApplication = jakartaServletWebApplication;
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.nameAnalysisService = nameAnalysisService;
|
||||
this.sessionNameRequestFactory = sessionNameRequestFactory;
|
||||
this.webApp = JakartaServletWebApplication.buildApplication(servletContext);
|
||||
}
|
||||
|
||||
@ModelAttribute("nameRequest")
|
||||
|
@ -53,22 +50,25 @@ public class NameAnalysisController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/name-analysis", params = { "search" })
|
||||
public String performNameAnalysis(final NameRequest nameRequest, final BindingResult bindingResult) {
|
||||
performNameRequest(nameRequest);
|
||||
public String performNameAnalysis(final NameRequest nameRequest, final BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) {
|
||||
IWebSession webSession = getIWebSession(request, response);
|
||||
performNameRequest(nameRequest, webSession);
|
||||
return "name-analysis";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/name-analysis/clear")
|
||||
public String clearNameAnalysis() {
|
||||
clearAnalysis();
|
||||
public String clearNameAnalysis(HttpServletRequest request, HttpServletResponse response) {
|
||||
IWebSession webSession = getIWebSession(request, response);
|
||||
clearAnalysis(webSession);
|
||||
return "redirect:/name-analysis";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/name-analysis/remove-history-request", params = { "id" })
|
||||
public String removeRequest() {
|
||||
public String removeRequest(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
IWebSession webSession = getIWebSession(request, response);
|
||||
final Integer rowId = Integer.valueOf(request.getParameter("id"));
|
||||
removeRequest(rowId);
|
||||
removeRequest(rowId, webSession);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -76,53 +76,49 @@ public class NameAnalysisController {
|
|||
return "redirect:/name-analysis";
|
||||
}
|
||||
|
||||
private void removeRequest(Integer rowId) {
|
||||
IWebSession session = getIWebSession();
|
||||
Object requests = session.getAttributeValue("requests");
|
||||
if (rowId != null && requests != null && (requests instanceof List)) {
|
||||
((List<SessionNameRequest>) requests).remove(rowId.intValue());
|
||||
private void removeRequest(Integer rowId, IWebSession webSession) {
|
||||
if (rowId != null) {
|
||||
List<SessionNameRequest> requests = getRequestsFromSession(webSession);
|
||||
if (requests != null) {
|
||||
requests.remove(rowId.intValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void performNameRequest(final NameRequest nameRequest) {
|
||||
private void performNameRequest(final NameRequest nameRequest, IWebSession webSession) {
|
||||
try {
|
||||
CompletableFuture<NameAnalysisEntity> nameAnalysis = this.nameAnalysisService.searchForName(nameRequest);
|
||||
NameAnalysisEntity nameAnalysisEntity = nameAnalysis.get(30, TimeUnit.SECONDS);
|
||||
sessionRegisterRequest(nameRequest);
|
||||
sessionRegisterAnalysis(nameAnalysisEntity);
|
||||
sessionClearAnalysisError();
|
||||
sessionRegisterRequest(nameRequest, webSession);
|
||||
sessionRegisterAnalysis(nameAnalysisEntity, webSession);
|
||||
sessionClearAnalysisError(webSession);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
sessionSetAnalysisError(nameRequest);
|
||||
sessionSetAnalysisError(nameRequest, webSession);
|
||||
}
|
||||
}
|
||||
|
||||
private void sessionClearAnalysisError() {
|
||||
IWebSession session = getIWebSession();
|
||||
session.setAttributeValue("analysisError", null);
|
||||
private void sessionClearAnalysisError(IWebSession webSession) {
|
||||
webSession.removeAttribute("analysisError");
|
||||
}
|
||||
|
||||
private void sessionSetAnalysisError(NameRequest nameRequest) {
|
||||
IWebSession session = getIWebSession();
|
||||
session.setAttributeValue("analysisError", nameRequest);
|
||||
private void sessionSetAnalysisError(NameRequest nameRequest, IWebSession webSession) {
|
||||
webSession.setAttributeValue("analysisError", nameRequest);
|
||||
}
|
||||
|
||||
private void clearAnalysis() {
|
||||
IWebSession session = getIWebSession();
|
||||
session.setAttributeValue("lastAnalysis", null);
|
||||
private void clearAnalysis(IWebSession webSession) {
|
||||
webSession.removeAttribute("lastAnalysis");
|
||||
}
|
||||
|
||||
private void sessionRegisterAnalysis(NameAnalysisEntity analysis) {
|
||||
IWebSession session = getIWebSession();
|
||||
session.setAttributeValue("lastAnalysis", analysis);
|
||||
private void sessionRegisterAnalysis(NameAnalysisEntity analysis, IWebSession webSession) {
|
||||
webSession.setAttributeValue("lastAnalysis", analysis);
|
||||
}
|
||||
|
||||
private void sessionRegisterRequest(NameRequest nameRequest) {
|
||||
IWebSession session = getIWebSession();
|
||||
session.setAttributeValue("lastRequest", nameRequest);
|
||||
private void sessionRegisterRequest(NameRequest nameRequest, IWebSession webSession) {
|
||||
webSession.setAttributeValue("lastRequest", nameRequest);
|
||||
|
||||
SessionNameRequest sessionNameRequest = sessionNameRequestFactory.getInstance(nameRequest);
|
||||
List<SessionNameRequest> requests = getRequestsFromSession(session);
|
||||
List<SessionNameRequest> requests = getRequestsFromSession(webSession);
|
||||
requests.add(0, sessionNameRequest);
|
||||
}
|
||||
|
||||
|
@ -136,8 +132,8 @@ public class NameAnalysisController {
|
|||
return (List<SessionNameRequest>) requests;
|
||||
}
|
||||
|
||||
private IWebSession getIWebSession() {
|
||||
IServletWebExchange webExchange = this.jakartaServletWebApplication.buildExchange(request, response);
|
||||
return webExchange.getSession();
|
||||
private IWebSession getIWebSession(HttpServletRequest request, HttpServletResponse response) {
|
||||
IServletWebExchange exchange = webApp.buildExchange(request, response);
|
||||
return exchange == null ? null : exchange.getSession();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue