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

@ -18,19 +18,14 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</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.thymeleaf</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>thymeleaf-spring6</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${thymeleaf.spring6.version}</version> <version>${spring.boot.starter.thymeleaf}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
@ -57,16 +52,9 @@
<version>${junit.jupiter.engine.version}</version> <version>${junit.jupiter.engine.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -91,5 +79,6 @@
<junit.jupiter.engine.version>5.9.3</junit.jupiter.engine.version> <junit.jupiter.engine.version>5.9.3</junit.jupiter.engine.version>
<mockito.version>5.3.1</mockito.version> <mockito.version>5.3.1</mockito.version>
<thymeleaf.spring6.version>3.1.1.RELEASE</thymeleaf.spring6.version> <thymeleaf.spring6.version>3.1.1.RELEASE</thymeleaf.spring6.version>
<spring.boot.starter.thymeleaf>3.1.1</spring.boot.starter.thymeleaf>
</properties> </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();
} }
} }