[JAVA-28185] (#15367)

* [JAVA-28185] Moved code for article(Introduction to HtmlUnit) to spring-mvc-java-2

* [JAVA-28185] Moved code for article(Upload and Display Excel Files with Spring MVC) to spring-mvc-java-2

* [JAVA-28185] Clean up

* [JAVA-28185]
This commit is contained in:
panos-kakos 2023-12-09 21:15:35 +02:00 committed by GitHub
parent bb989a8fe3
commit f624403a86
13 changed files with 121 additions and 61 deletions

View File

@ -5,3 +5,5 @@
- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables) - [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables)
- [Converting a Spring MultipartFile to a File](https://www.baeldung.com/spring-multipartfile-to-file) - [Converting a Spring MultipartFile to a File](https://www.baeldung.com/spring-multipartfile-to-file)
- [Testing a Spring Multipart POST Request](https://www.baeldung.com/spring-multipart-post-request-test) - [Testing a Spring Multipart POST Request](https://www.baeldung.com/spring-multipart-post-request-test)
- [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit)
- [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files)

View File

@ -46,6 +46,57 @@
<artifactId>commons-fileupload</artifactId> <artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version> <version>${commons-fileupload.version}</version>
</dependency> </dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${htmlunit.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>${javax.el.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${javax-servlet-api.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -63,6 +114,11 @@
<spring.mvc.version>5.2.2.RELEASE</spring.mvc.version> <spring.mvc.version>5.2.2.RELEASE</spring.mvc.version>
<jaxb-runtime.version>2.3.5</jaxb-runtime.version> <jaxb-runtime.version>2.3.5</jaxb-runtime.version>
<commons-fileupload.version>1.5</commons-fileupload.version> <commons-fileupload.version>1.5</commons-fileupload.version>
<htmlunit.version>2.32</htmlunit.version>
<poi.version>3.16-beta1</poi.version>
<javax.el.version>3.0.1-b09</javax.el.version>
<javax-servlet-api.version>2.3.3</javax-servlet-api.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
</properties> </properties>
</project> </project>

View File

@ -1,18 +1,19 @@
package com.baeldung.web.controller; package com.baeldung.excel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
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.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.baeldung.excel.*;
import java.util.Map;
import java.util.List;
import javax.annotation.Resource;
@Controller @Controller
public class ExcelController { public class ExcelController {

View File

@ -1,13 +1,14 @@
package com.baeldung.excel; package com.baeldung.excel;
import org.apache.poi.ss.usermodel.Cell; import java.io.File;
import org.apache.poi.xssf.usermodel.XSSFCell; import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFColor; import java.util.ArrayList;
import org.apache.poi.xssf.usermodel.XSSFFont; import java.util.HashMap;
import org.apache.poi.xssf.usermodel.XSSFRow; import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFSheet; import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.util.stream.IntStream;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFFont;
@ -15,16 +16,15 @@ import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import java.io.File; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFColor;
import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFFont;
import java.util.Map; import org.apache.poi.xssf.usermodel.XSSFRow;
import java.util.HashMap; import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.util.ArrayList; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.stream.IntStream;
public class ExcelPOIHelper { public class ExcelPOIHelper {

View File

@ -0,0 +1,32 @@
package com.baeldung.excel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.baeldung.excel" })
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ExcelPOIHelper excelPOIHelper() {
return new ExcelPOIHelper();
}
}

View File

@ -9,8 +9,6 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring
### Relevant Articles: ### Relevant Articles:
- [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring) - [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring)
- [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload) - [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload)
- [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit)
- [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files)
- [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config) - [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config)
- [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml) - [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml)
- [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js) - [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js)

View File

@ -44,21 +44,7 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- common --> <!-- common -->
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${htmlunit.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
@ -91,12 +77,7 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- Validation --> <!-- Validation -->
<dependency> <dependency>
<groupId>org.hibernate.validator</groupId> <groupId>org.hibernate.validator</groupId>
@ -221,19 +202,15 @@
<!-- testing --> <!-- testing -->
<httpcore.version>4.4.5</httpcore.version> <httpcore.version>4.4.5</httpcore.version>
<httpclient.version>4.5.2</httpclient.version> <httpclient.version>4.5.2</httpclient.version>
<net.sourceforge.htmlunit>2.23</net.sourceforge.htmlunit>
<!-- maven plugins --> <!-- maven plugins -->
<maven-resources-plugin.version>2.7</maven-resources-plugin.version> <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version> <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
<!-- AspectJ --> <!-- AspectJ -->
<aspectj.version>1.9.1</aspectj.version> <aspectj.version>1.9.1</aspectj.version>
<!-- excel -->
<poi.version>3.16-beta1</poi.version>
<javax.el.version>3.0.1-b09</javax.el.version> <javax.el.version>3.0.1-b09</javax.el.version>
<javax.version>4.0.1</javax.version> <javax.version>4.0.1</javax.version>
<javax-servlet-api.version>2.3.3</javax-servlet-api.version> <javax-servlet-api.version>2.3.3</javax-servlet-api.version>
<htmlunit.version>2.32</htmlunit.version>
<json-path.version>2.8.0</json-path.version> <json-path.version>2.8.0</json-path.version>
<start-class>com.baeldung.SpringMVCApplication</start-class> <start-class>com.baeldung.SpringMVCApplication</start-class>
</properties> </properties>

View File

@ -27,7 +27,6 @@ import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.ITemplateResolver;
import com.baeldung.excel.ExcelPOIHelper;
@EnableWebMvc @EnableWebMvc
@Configuration @Configuration
@ -118,11 +117,6 @@ public class WebConfig implements WebMvcConfigurer {
configurer.setUrlPathHelper(urlPathHelper); configurer.setUrlPathHelper(urlPathHelper);
} }
@Bean
public ExcelPOIHelper excelPOIHelper() {
return new ExcelPOIHelper();
}
@Bean(name = "multipartResolver") @Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() { public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();