From ecc662501780e7337bc0ca0cda627bb95de3e104 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:49:55 +0100 Subject: [PATCH 1/8] Add form tag example --- .../org/baeldung/spring/ClientWebConfig.java | 8 +-- .../baeldung/spring/ClientWebConfigJava.java | 53 +++++++++++++------ .../main/webapp/WEB-INF/view/personForm.jsp | 6 +-- .../main/webapp/WEB-INF/view/personView.jsp | 15 ------ .../src/main/webapp/WEB-INF/web.xml | 2 +- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java index c4d819caa5..9705d51606 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java @@ -8,10 +8,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java index d2b57da818..c82201880f 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java @@ -1,6 +1,12 @@ package org.baeldung.spring; +import java.util.Locale; +import java.util.ResourceBundle; + +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.support.MessageSourceResourceBundle; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -11,27 +17,40 @@ import org.springframework.web.servlet.view.JstlView; //@Configuration public class ClientWebConfigJava extends WebMvcConfigurerAdapter { - public ClientWebConfigJava() { - super(); - } + public ClientWebConfigJava() { + super(); + } - // API + @Bean + public MessageSource messageSource() { - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + final ResourceBundleMessageSource ms = new ResourceBundleMessageSource(); + ms.setBasenames("messages"); + return ms; + } - registry.addViewController("/sample.html"); - } + @Bean + public ResourceBundle getBeanResourceBundle() { - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + final Locale locale = Locale.getDefault(); + return new MessageSourceResourceBundle(messageSource(), locale); + } - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - return bean; - } + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 79bda7c17d..63d18a987c 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -24,7 +24,7 @@

Welcome, Enter The Person Details

- + @@ -104,10 +104,6 @@ Notes - - Select a file to upload - - diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp index 8893314d20..bd14474dfe 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -61,20 +61,5 @@ ${person.notes} - -

Submitted File

- - - - - - - - - - - - -
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
\ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..4e38b2fae6 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - /tmp + C:/Users/ivan/Desktop/tmp From 934c0dda3740ae8142fadef664efcc26f7537d0f Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:51:16 +0100 Subject: [PATCH 2/8] Add form tag example --- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 4e38b2fae6..a39c4fdd3a 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - C:/Users/ivan/Desktop/tmp + /tmp From 925f174c54672c5436f3d83d1ad43e77a9bd49ae Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:53:39 +0100 Subject: [PATCH 3/8] Add form tag example --- spring-mvc-xml/src/main/resources/webMvcConfig.xml | 7 ------- spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp | 2 +- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 3 --- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index f3297fdbf6..f127cfd637 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -25,11 +25,4 @@ - - - - - \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 63d18a987c..2836c11668 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -22,7 +22,7 @@ -

Welcome, Enter The Person Details

+

Welcome, Enter the Person Details

diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..5275efdf24 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -28,9 +28,6 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 - - /tmp - mvc From 8d398b28529fd16316f38496617894fde2bf2d05 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:12:12 +0100 Subject: [PATCH 4/8] Add file upload example --- spring-mvc-java/.classpath | 74 ++++++------ spring-mvc-java/pom.xml | 7 ++ .../spring/web/config/ClientWebConfig.java | 112 +++++++++--------- .../web/config/MainWebAppInitializer.java | 50 ++++---- .../baeldung/spring/web/config/WebConfig.java | 15 +++ .../web/controller/FileUploadController.java | 80 +++++++++++++ .../webapp/WEB-INF/view/fileUploadForm.jsp | 47 ++++++++ .../webapp/WEB-INF/view/fileUploadView.jsp | 20 ++++ 8 files changed, 293 insertions(+), 112 deletions(-) create mode 100644 spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp diff --git a/spring-mvc-java/.classpath b/spring-mvc-java/.classpath index 6b533711d3..a642d37ceb 100644 --- a/spring-mvc-java/.classpath +++ b/spring-mvc-java/.classpath @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index d9a578cb8c..4358e7939f 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -57,6 +57,13 @@ slf4j-log4j12 ${org.slf4j.version} + + + commons-fileupload + commons-fileupload + 1.3.1 + + junit diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index db57b4716b..3f451f4259 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -25,69 +25,71 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } + registry.addViewController("/sample.html"); + registry.addViewController("/fileUpload.html"); + registry.addViewController("/fileUploadForm.html"); + } - @Bean - public ViewResolver thymeleafViewResolver() { - final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); - viewResolver.setTemplateEngine(templateEngine()); - viewResolver.setOrder(1); - return viewResolver; - } + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; + } - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } - @Bean - @Description("Thymeleaf template resolver serving HTML 5") - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); - templateResolver.setPrefix("/WEB-INF/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode("HTML5"); - return templateResolver; - } + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } - @Bean - @Description("Thymeleaf template engine with Spring integration") - public SpringTemplateEngine templateEngine() { - final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(templateResolver()); - final Set dialects = new HashSet<>(); - dialects.add(new CustomDialect()); - templateEngine.setAdditionalDialects(dialects); - return templateEngine; - } + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + final Set dialects = new HashSet<>(); + dialects.add(new CustomDialect()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } - @Bean - @Description("Spring message resolver") - public MessageSource messageSource() { - final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); - return messageSource; - } + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } - @Override - public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 87502e2088..9dd9ae98b0 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,28 +14,38 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - /** - * Register and configure all Servlet container components necessary to power the web application. - */ - @Override - public void onStartup(final ServletContext sc) throws ServletException { - System.out.println("MainWebAppInitializer.onStartup()"); + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 + // MB + private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB - // Create the 'root' Spring application context - final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); - // root.getEnvironment().setDefaultProfiles("embedded"); + /** + * Register and configure all Servlet container components necessary to power the web application. + */ + @Override + public void onStartup(final ServletContext sc) throws ServletException { - // Manages the lifecycle of the root application context - sc.addListener(new ContextLoaderListener(root)); + // Create the 'root' Spring application context + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.scan("org.baeldung.spring.web.config"); + // root.getEnvironment().setDefaultProfiles("embedded"); - // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); - appServlet.setLoadOnStartup(1); - final Set mappingConflicts = appServlet.addMapping("/"); - if (!mappingConflicts.isEmpty()) { - throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); - } - } + // Manages the lifecycle of the root application context + sc.addListener(new ContextLoaderListener(root)); + + // Handles requests into the application + final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); + appServlet.setLoadOnStartup(1); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); + + final Set mappingConflicts = appServlet.addMapping("/"); + if (!mappingConflicts.isEmpty()) { + throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); + } + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index 58438d2976..b7d87dffbc 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -22,6 +22,21 @@ public class WebConfig extends WebMvcConfigurerAdapter { super(); } + // @Bean + // public StandardServletMultipartResolver multipartResolver() { + // return new StandardServletMultipartResolver(); + // } + + // @Bean(name = "multipartResolver") + // public CommonsMultipartResolver multipartResolver() { + // + // final CommonsMultipartResolver multipartResolver = new + // CommonsMultipartResolver(); + // multipartResolver.setMaxUploadSize(100000); + // + // return multipartResolver; + // } + @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java new file mode 100644 index 0000000000..5c9d3cbef3 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -0,0 +1,80 @@ +package org.baeldung.web.controller; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.io.FileUtils; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@Controller +public class FileUploadController { + + @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { + + modelMap.addAttribute("fileName", file.getOriginalFilename()); + modelMap.addAttribute("fileType", file.getContentType()); + + return "fileUploadView"; + } + + @RequestMapping(value = "/addFile2", method = RequestMethod.POST) + public String submit(final HttpServletRequest request, final HttpServletResponse response, + final ModelMap modelMap) { + + final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + + try { + + final DiskFileItemFactory factory = new DiskFileItemFactory(); + + // Configure a repository (to ensure a secure temp location is used) + final File repository = new File(TEMP_PATH); + factory.setRepository(repository); + + // Create a new file upload handler + final ServletFileUpload upload = new ServletFileUpload(factory); + + // Parse the request + final List items = upload.parseRequest(request); + + final Iterator iter = items.iterator(); + + while (iter.hasNext()) { + + final FileItem item = iter.next(); + + if (!item.isFormField()) { + + final File targetFile = new File(TEMP_PATH + item.getName()); + FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); + + modelMap.addAttribute("fileName", item.getName()); + modelMap.addAttribute("fileType", item.getContentType()); + } + } + + } catch (final FileUploadException e) { + e.printStackTrace(); + + } catch (final IOException e) { + e.printStackTrace(); + } + + return "fileUploadView"; + } +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp new file mode 100644 index 0000000000..05a7e25a02 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -0,0 +1,47 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + File Upload Example + + + + +

Enter The File to Upload (MultipartFile handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ +
+ +

Enter The File to Upload (HttpServletRequest handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp new file mode 100644 index 0000000000..779c26a597 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -0,0 +1,20 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC File Upload + + +

Submitted File

+ + + + + + + + + +
OriginalFileName :${fileName}
Type :${fileType}
+ + \ No newline at end of file From 09a1ebfa07b9d697fc4973aa20d87700bc8aa370 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:16:10 +0100 Subject: [PATCH 5/8] Add file upload example --- .../java/org/baeldung/web/controller/FileUploadController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index 5c9d3cbef3..a25a620c8b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -36,7 +36,7 @@ public class FileUploadController { public String submit(final HttpServletRequest request, final HttpServletResponse response, final ModelMap modelMap) { - final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + final String TEMP_PATH = "/tmp/"; try { From b7d0b2947a0bffd2c89639425dd5a7b6bd91d4cb Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 30 Mar 2016 15:06:37 +0200 Subject: [PATCH 6/8] Add file upload example --- .../spring/web/config/ClientWebConfig.java | 2 - .../web/config/MainWebAppInitializer.java | 3 +- .../baeldung/spring/web/config/WebConfig.java | 18 ++--- .../web/controller/FileUploadController.java | 70 +++---------------- .../webapp/WEB-INF/view/fileUploadForm.jsp | 22 ++++-- .../webapp/WEB-INF/view/fileUploadView.jsp | 22 +++++- 6 files changed, 55 insertions(+), 82 deletions(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index 3f451f4259..6084943ddd 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -36,8 +36,6 @@ public class ClientWebConfig extends WebMvcConfigurerAdapter { super.addViewControllers(registry); registry.addViewController("/sample.html"); - registry.addViewController("/fileUpload.html"); - registry.addViewController("/fileUploadForm.html"); } @Bean diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 9dd9ae98b0..ad37bbec5e 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,8 +14,7 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 - // MB + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB /** diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index b7d87dffbc..cd9ae582d8 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; @@ -27,15 +28,14 @@ public class WebConfig extends WebMvcConfigurerAdapter { // return new StandardServletMultipartResolver(); // } - // @Bean(name = "multipartResolver") - // public CommonsMultipartResolver multipartResolver() { - // - // final CommonsMultipartResolver multipartResolver = new - // CommonsMultipartResolver(); - // multipartResolver.setMaxUploadSize(100000); - // - // return multipartResolver; - // } + @Bean(name = "multipartResolver") + public CommonsMultipartResolver multipartResolver() { + + final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); + multipartResolver.setMaxUploadSize(100000); + + return multipartResolver; + } @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index a25a620c8b..6f557adf0f 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -1,18 +1,5 @@ package org.baeldung.web.controller; -import java.io.File; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.FileUploadException; -import org.apache.commons.fileupload.disk.DiskFileItemFactory; -import org.apache.commons.fileupload.servlet.ServletFileUpload; -import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,58 +10,23 @@ import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { - @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + @RequestMapping(value = "/fileUpload", method = RequestMethod.GET) + public String displayForm() { + + return "fileUploadForm"; + } + + @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { - modelMap.addAttribute("fileName", file.getOriginalFilename()); - modelMap.addAttribute("fileType", file.getContentType()); - + modelMap.addAttribute("file", file); return "fileUploadView"; } - @RequestMapping(value = "/addFile2", method = RequestMethod.POST) - public String submit(final HttpServletRequest request, final HttpServletResponse response, - final ModelMap modelMap) { - - final String TEMP_PATH = "/tmp/"; - - try { - - final DiskFileItemFactory factory = new DiskFileItemFactory(); - - // Configure a repository (to ensure a secure temp location is used) - final File repository = new File(TEMP_PATH); - factory.setRepository(repository); - - // Create a new file upload handler - final ServletFileUpload upload = new ServletFileUpload(factory); - - // Parse the request - final List items = upload.parseRequest(request); - - final Iterator iter = items.iterator(); - - while (iter.hasNext()) { - - final FileItem item = iter.next(); - - if (!item.isFormField()) { - - final File targetFile = new File(TEMP_PATH + item.getName()); - FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); - - modelMap.addAttribute("fileName", item.getName()); - modelMap.addAttribute("fileType", item.getContentType()); - } - } - - } catch (final FileUploadException e) { - e.printStackTrace(); - - } catch (final IOException e) { - e.printStackTrace(); - } + @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST) + public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) { + modelMap.addAttribute("files", files); return "fileUploadView"; } } diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 05a7e25a02..371acdc0ed 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload (MultipartFile handling)

+

Enter The File to Upload

- + - + @@ -24,16 +24,24 @@ -
+
-

Enter The File to Upload (HttpServletRequest handling)

+

Enter The Files to Upload (Multiple files)

- +
Select a file to uploadSelect a file to upload (Single file)
- + + + + + + + + + diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp index 779c26a597..d6f748c6af 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -5,16 +5,32 @@ Spring MVC File Upload -

Submitted File

+ +

Submitted File (Single)

Select a file to upload
Select a file to upload
Select a file to upload
- + - +
OriginalFileName :${fileName}${file.originalFilename}
Type :${fileType}${file.contentType}
+
+ +

Submitted Files (Multiple)

+ + + + + + + + + + + +
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
\ No newline at end of file From 3061fc7c0f9b17bf517259154900e9a70a0c512b Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 4 Apr 2016 14:03:44 +0200 Subject: [PATCH 7/8] Add file upload example --- .../src/main/webapp/WEB-INF/view/fileUploadForm.jsp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 371acdc0ed..1414b824ff 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload

+

Enter The File to Upload (Single file)

- + From 9630170b032304afb7adff66b29f59a70112352c Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 7 Apr 2016 06:17:57 -0500 Subject: [PATCH 8/8] Rename package --- .../baeldung/aop/LoggingAspect.java | 6 +- .../baeldung/aop/PerformanceAspect.java | 2 +- .../baeldung/aop/PublishingAspect.java | 4 +- .../baeldung/aop/annotations/Entity.java | 2 +- .../baeldung/aop/annotations/Loggable.java | 2 +- .../{org => com}/baeldung/dao/FooDao.java | 6 +- .../baeldung/dialect/CustomDialect.java | 4 +- .../baeldung/events/FooCreationEvent.java | 2 +- .../events/FooCreationEventListener.java | 2 +- .../{org => com}/baeldung/model/Employee.java | 2 +- .../java/{org => com}/baeldung/model/Foo.java | 4 +- .../{org => com}/baeldung/model/User.java | 2 +- .../baeldung/processor/NameProcessor.java | 2 +- .../spring/web/config/ClientWebConfig.java | 4 +- .../config/ContentManagementWebConfig.java | 2 +- .../web/config/MainWebAppInitializer.java | 4 +- .../baeldung/spring/web/config/WebConfig.java | 4 +- .../java/{org => com}/baeldung/web/BeanA.java | 2 +- .../java/{org => com}/baeldung/web/BeanB.java | 2 +- .../web/controller/EmployeeController.java | 4 +- .../web/controller/FileUploadController.java | 2 +- .../web/controller/UserController.java | 4 +- .../spring/web/config/WebConfig.java.orig | 116 ------------------ .../{org => com}/baeldung/aop/beans.xml | 4 +- .../src/main/webapp/WEB-INF/web_old.xml | 2 +- .../baeldung/aop/AopLoggingTest.java | 8 +- .../baeldung/aop/AopPerformanceTest.java | 6 +- .../baeldung/aop/AopPublishingTest.java | 10 +- .../aop/AopXmlConfigPerformanceTest.java | 6 +- .../baeldung/config/TestConfig.java | 4 +- 30 files changed, 54 insertions(+), 170 deletions(-) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/LoggingAspect.java (90%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/PerformanceAspect.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/PublishingAspect.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/annotations/Entity.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/aop/annotations/Loggable.java (87%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/dao/FooDao.java (75%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/dialect/CustomDialect.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/events/FooCreationEvent.java (86%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/events/FooCreationEventListener.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/Employee.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/Foo.java (80%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/model/User.java (90%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/processor/NameProcessor.java (94%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/ClientWebConfig.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/ContentManagementWebConfig.java (97%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/MainWebAppInitializer.java (95%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/spring/web/config/WebConfig.java (96%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/BeanA.java (89%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/BeanB.java (83%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/EmployeeController.java (95%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/FileUploadController.java (96%) rename spring-mvc-java/src/main/java/{org => com}/baeldung/web/controller/UserController.java (90%) delete mode 100644 spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig rename spring-mvc-java/src/main/resources/{org => com}/baeldung/aop/beans.xml (86%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopLoggingTest.java (95%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopPerformanceTest.java (95%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopPublishingTest.java (90%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/aop/AopXmlConfigPerformanceTest.java (94%) rename spring-mvc-java/src/test/java/{org => com}/baeldung/config/TestConfig.java (67%) diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java index c59c4f060a..7ae37404be 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; @@ -27,11 +27,11 @@ public class LoggingAspect { public void repositoryMethods() { } - @Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)") + @Pointcut("@annotation(com.baeldung.aop.annotations.Loggable)") public void loggableMethods() { } - @Pointcut("@args(org.baeldung.aop.annotations.Entity)") + @Pointcut("@args(com.baeldung.aop.annotations.Entity)") public void methodsAcceptingEntities() { } diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java index 2d07e5a5f3..1f2076adff 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java index 324605dab1..7791c63e7b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java @@ -1,10 +1,10 @@ -package org.baeldung.aop; +package com.baeldung.aop; +import com.baeldung.events.FooCreationEvent; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; -import org.baeldung.events.FooCreationEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java index f964c3979e..61d91b0777 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java similarity index 87% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java index ef2863957f..92aa950e58 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java similarity index 75% rename from spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java rename to spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java index f204440b2d..1d28b082ec 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java +++ b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java @@ -1,7 +1,7 @@ -package org.baeldung.dao; +package com.baeldung.dao; -import org.baeldung.aop.annotations.Loggable; -import org.baeldung.model.Foo; +import com.baeldung.aop.annotations.Loggable; +import com.baeldung.model.Foo; import org.springframework.stereotype.Repository; @Repository diff --git a/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java rename to spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java index e6d1ad6b74..0c6a7c3ae0 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dialect/CustomDialect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java @@ -1,9 +1,9 @@ -package org.baeldung.dialect; +package com.baeldung.dialect; import java.util.HashSet; import java.util.Set; -import org.baeldung.processor.NameProcessor; +import com.baeldung.processor.NameProcessor; import org.thymeleaf.dialect.AbstractDialect; import org.thymeleaf.processor.IProcessor; diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java index af11f3a4be..5ea4b46c04 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationEvent; diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java index 35dcfd2bc3..c0aa744bc1 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/model/Employee.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Employee.java index 5365068a89..d0f6b724eb 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package com.baeldung.model; import javax.xml.bind.annotation.XmlRootElement; diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java similarity index 80% rename from spring-mvc-java/src/main/java/org/baeldung/model/Foo.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Foo.java index 87bd7132e6..01f5f43f60 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java @@ -1,6 +1,6 @@ -package org.baeldung.model; +package com.baeldung.model; -import org.baeldung.aop.annotations.Entity; +import com.baeldung.aop.annotations.Entity; @Entity public class Foo { diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/User.java b/spring-mvc-java/src/main/java/com/baeldung/model/User.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/model/User.java rename to spring-mvc-java/src/main/java/com/baeldung/model/User.java index df549cd21d..dc4480c986 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/User.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/User.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package com.baeldung.model; public class User { private String firstname; diff --git a/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java rename to spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java index df9a4da7f0..9a7857198c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/processor/NameProcessor.java +++ b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java @@ -1,4 +1,4 @@ -package org.baeldung.processor; +package com.baeldung.processor; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Element; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java index 6084943ddd..c108a450ae 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java @@ -1,9 +1,9 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import java.util.HashSet; import java.util.Set; -import org.baeldung.dialect.CustomDialect; +import com.baeldung.dialect.CustomDialect; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java similarity index 97% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java index 2c5b423029..9780575678 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java similarity index 95% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java index ad37bbec5e..4a11ba986c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import java.util.Set; @@ -25,7 +25,7 @@ public class MainWebAppInitializer implements WebApplicationInitializer { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); + root.scan("com.baeldung.spring.web.config"); // root.getEnvironment().setDefaultProfiles("embedded"); // Manages the lifecycle of the root application context diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java similarity index 96% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index bd2af0886e..693fd74f74 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -16,7 +16,7 @@ import org.springframework.web.servlet.view.XmlViewResolver; @Configuration @EnableWebMvc -@ComponentScan("org.baeldung.web") +@ComponentScan("com.baeldung.web") public class WebConfig extends WebMvcConfigurerAdapter { public WebConfig() { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java similarity index 89% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java index b6b6f49c16..79fac724f7 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java similarity index 83% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java index 49e5af4ccb..05c9560a0c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java similarity index 95% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java index e18bbdbf63..38272b23cb 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java @@ -1,9 +1,9 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; import java.util.HashMap; import java.util.Map; -import org.baeldung.model.Employee; +import com.baeldung.model.Employee; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java similarity index 96% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java index 6f557adf0f..bc9cf13c34 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java @@ -1,4 +1,4 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java similarity index 90% rename from spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java index da39a36adf..fda159f204 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/UserController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java @@ -1,6 +1,6 @@ -package org.baeldung.web.controller; +package com.baeldung.web.controller; -import org.baeldung.model.User; +import com.baeldung.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig deleted file mode 100644 index 78307849b4..0000000000 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java.orig +++ /dev/null @@ -1,116 +0,0 @@ -package org.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.web.multipart.commons.CommonsMultipartResolver; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; -import org.springframework.web.servlet.view.ResourceBundleViewResolver; -import org.springframework.web.servlet.view.XmlViewResolver; - -@Configuration -@EnableWebMvc -@ComponentScan("org.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { - -<<<<<<< HEAD - public WebConfig() { - super(); - } - - // - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - - @Bean - public ViewResolver xmlViewResolver() { - final XmlViewResolver bean = new XmlViewResolver(); - bean.setLocation(new ClassPathResource("views.xml")); - bean.setOrder(1); - return bean; - } - - @Bean - public ViewResolver resourceBundleViewResolver() { - final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); - bean.setBasename("views"); - bean.setOrder(0); - return bean; - } - -======= - public WebConfig() { - super(); - } - - // @Bean - // public StandardServletMultipartResolver multipartResolver() { - // return new StandardServletMultipartResolver(); - // } - - @Bean(name = "multipartResolver") - public CommonsMultipartResolver multipartResolver() { - - final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); - multipartResolver.setMaxUploadSize(100000); - - return multipartResolver; - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - - @Bean - public ViewResolver xmlViewResolver() { - - final XmlViewResolver bean = new XmlViewResolver(); - bean.setLocation(new ClassPathResource("views.xml")); - bean.setOrder(1); - return bean; - } - - @Bean - public ViewResolver resourceBundleViewResolver() { - - final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); - bean.setBasename("views"); - bean.setOrder(0); - return bean; - } ->>>>>>> 3061fc7c0f9b17bf517259154900e9a70a0c512b -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml similarity index 86% rename from spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml rename to spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml index 17c63e39e4..e6aa9d77c4 100644 --- a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml +++ b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml @@ -7,8 +7,8 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> - - + + diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml index 016369ad27..c8b38fae30 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml +++ b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml @@ -16,7 +16,7 @@ contextConfigLocation - org.baeldung.spring.web.config + com.baeldung.spring.web.config diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java similarity index 95% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java index b1c9867e41..19bf4d0fac 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopLoggingTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopLoggingTest.java @@ -1,8 +1,8 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; -import org.baeldung.model.Foo; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; +import com.baeldung.model.Foo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java similarity index 95% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java index 69083c60a2..4ad5a3e1a6 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPerformanceTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPerformanceTest.java @@ -1,7 +1,7 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java similarity index 90% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java index e691dbd32e..c075db9fc6 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopPublishingTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopPublishingTest.java @@ -1,9 +1,9 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.config.TestConfig; -import org.baeldung.dao.FooDao; -import org.baeldung.events.FooCreationEventListener; -import org.baeldung.model.Foo; +import com.baeldung.config.TestConfig; +import com.baeldung.dao.FooDao; +import com.baeldung.events.FooCreationEventListener; +import com.baeldung.model.Foo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java b/spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java similarity index 94% rename from spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java rename to spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java index 7ef25d743c..4d2df50d18 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/aop/AopXmlConfigPerformanceTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/aop/AopXmlConfigPerformanceTest.java @@ -1,6 +1,6 @@ -package org.baeldung.aop; +package com.baeldung.aop; -import org.baeldung.dao.FooDao; +import com.baeldung.dao.FooDao; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -21,7 +21,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("/org/baeldung/aop/beans.xml") +@ContextConfiguration("/com/baeldung/aop/beans.xml") public class AopXmlConfigPerformanceTest { @Before diff --git a/spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java b/spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java similarity index 67% rename from spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java rename to spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java index f9573b2add..641513a24a 100644 --- a/spring-mvc-java/src/test/java/org/baeldung/config/TestConfig.java +++ b/spring-mvc-java/src/test/java/com/baeldung/config/TestConfig.java @@ -1,11 +1,11 @@ -package org.baeldung.config; +package com.baeldung.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration -@ComponentScan(basePackages = { "org.baeldung.dao", "org.baeldung.aop", "org.baeldung.events" }) +@ComponentScan(basePackages = { "com.baeldung.dao", "com.baeldung.aop", "com.baeldung.events" }) @EnableAspectJAutoProxy public class TestConfig { }
Select a file to upload (Single file)Select a file to upload