Merge branch 'master' of https://github.com/ivanp81/tutorials into ivanp81-master

This commit is contained in:
David Morley 2016-04-07 06:10:09 -05:00
commit 44eca5ba04
14 changed files with 426 additions and 140 deletions

View File

@ -57,6 +57,13 @@
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
</dependency> </dependency>
<!-- common -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -25,69 +25,69 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
@Configuration @Configuration
public class ClientWebConfig extends WebMvcConfigurerAdapter { public class ClientWebConfig extends WebMvcConfigurerAdapter {
public ClientWebConfig() { public ClientWebConfig() {
super(); super();
} }
// API // API
@Override @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry); super.addViewControllers(registry);
registry.addViewController("/sample.html"); registry.addViewController("/sample.html");
} }
@Bean @Bean
public ViewResolver thymeleafViewResolver() { public ViewResolver thymeleafViewResolver() {
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine()); viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1); viewResolver.setOrder(1);
return viewResolver; return viewResolver;
} }
@Bean @Bean
public ViewResolver viewResolver() { public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver(); final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class); bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/"); bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp"); bean.setSuffix(".jsp");
bean.setOrder(0); bean.setOrder(0);
return bean; return bean;
} }
@Bean @Bean
@Description("Thymeleaf template resolver serving HTML 5") @Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver templateResolver() { public ServletContextTemplateResolver templateResolver() {
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html"); templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5"); templateResolver.setTemplateMode("HTML5");
return templateResolver; return templateResolver;
} }
@Bean @Bean
@Description("Thymeleaf template engine with Spring integration") @Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() { public SpringTemplateEngine templateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver()); templateEngine.setTemplateResolver(templateResolver());
final Set<IDialect> dialects = new HashSet<>(); final Set<IDialect> dialects = new HashSet<>();
dialects.add(new CustomDialect()); dialects.add(new CustomDialect());
templateEngine.setAdditionalDialects(dialects); templateEngine.setAdditionalDialects(dialects);
return templateEngine; return templateEngine;
} }
@Bean @Bean
@Description("Spring message resolver") @Description("Spring message resolver")
public MessageSource messageSource() { public MessageSource messageSource() {
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages"); messageSource.setBasename("messages");
return messageSource; return messageSource;
} }
@Override @Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) { public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
} }
} }

View File

@ -14,28 +14,37 @@ import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer { public class MainWebAppInitializer implements WebApplicationInitializer {
/** private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp";
* Register and configure all Servlet container components necessary to power the web application. private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
System.out.println("MainWebAppInitializer.onStartup()");
// Create the 'root' Spring application context /**
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); * Register and configure all Servlet container components necessary to power the web application.
root.scan("org.baeldung.spring.web.config"); */
// root.getEnvironment().setDefaultProfiles("embedded"); @Override
public void onStartup(final ServletContext sc) throws ServletException {
// Manages the lifecycle of the root application context // Create the 'root' Spring application context
sc.addListener(new ContextLoaderListener(root)); final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("org.baeldung.spring.web.config");
// root.getEnvironment().setDefaultProfiles("embedded");
// Handles requests into the application // Manages the lifecycle of the root application context
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); sc.addListener(new ContextLoaderListener(root));
appServlet.setLoadOnStartup(1);
final Set<String> mappingConflicts = appServlet.addMapping("/"); // Handles requests into the application
if (!mappingConflicts.isEmpty()) { final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
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"); 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<String> 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");
}
}
} }

View File

@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
@ -22,7 +23,19 @@ public class WebConfig extends WebMvcConfigurerAdapter {
super(); 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 @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {

View File

@ -0,0 +1,116 @@
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
}

View File

@ -0,0 +1,32 @@
package org.baeldung.web.controller;
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 = "/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("file", file);
return "fileUploadView";
}
@RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) {
modelMap.addAttribute("files", files);
return "fileUploadView";
}
}

View File

@ -0,0 +1,55 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload Example</title>
</head>
<body>
<h3>Enter The File to Upload (Single file)</h3>
<form:form method="POST" action="/spring-mvc-java/uploadFile" enctype="multipart/form-data">
<table>
<tr>
<td>Select a file to upload</td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
<br />
<h3>Enter The Files to Upload (Multiple files)</h3>
<form:form method="POST" action="/spring-mvc-java/uploadMultiFile" enctype="multipart/form-data">
<table>
<tr>
<td>Select a file to upload</td>
<td><input type="file" name="files" /></td>
</tr>
<tr>
<td>Select a file to upload</td>
<td><input type="file" name="files" /></td>
</tr>
<tr>
<td>Select a file to upload</td>
<td><input type="file" name="files" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>

View File

@ -0,0 +1,36 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Spring MVC File Upload</title>
</head>
<body>
<h2>Submitted File (Single)</h2>
<table>
<tr>
<td>OriginalFileName :</td>
<td>${file.originalFilename}</td>
</tr>
<tr>
<td>Type :</td>
<td>${file.contentType}</td>
</tr>
</table>
<br />
<h2>Submitted Files (Multiple)</h2>
<table>
<c:forEach items="${files}" var="file">
<tr>
<td>OriginalFileName :</td>
<td>${file.originalFilename}</td>
</tr>
<tr>
<td>Type :</td>
<td>${file.contentType}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

View File

@ -8,10 +8,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@Configuration @Configuration
public class ClientWebConfig extends WebMvcConfigurerAdapter { public class ClientWebConfig extends WebMvcConfigurerAdapter {
public ClientWebConfig() { public ClientWebConfig() {
super(); super();
} }
// API // API
} }

View File

@ -1,6 +1,12 @@
package com.baeldung.spring; package com.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.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.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@ -11,27 +17,40 @@ import org.springframework.web.servlet.view.JstlView;
//@Configuration //@Configuration
public class ClientWebConfigJava extends WebMvcConfigurerAdapter { public class ClientWebConfigJava extends WebMvcConfigurerAdapter {
public ClientWebConfigJava() { public ClientWebConfigJava() {
super(); super();
} }
// API @Bean
public MessageSource messageSource() {
@Override final ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
public void addViewControllers(final ViewControllerRegistry registry) { ms.setBasenames("messages");
super.addViewControllers(registry); return ms;
}
registry.addViewController("/sample.html"); @Bean
} public ResourceBundle getBeanResourceBundle() {
@Bean final Locale locale = Locale.getDefault();
public ViewResolver viewResolver() { return new MessageSourceResourceBundle(messageSource(), locale);
final InternalResourceViewResolver bean = new InternalResourceViewResolver(); }
bean.setViewClass(JstlView.class); @Override
bean.setPrefix("/WEB-INF/view/"); public void addViewControllers(final ViewControllerRegistry registry) {
bean.setSuffix(".jsp"); 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;
}
} }

View File

@ -24,7 +24,7 @@
<h3>Welcome, Enter the Person Details</h3> <h3>Welcome, Enter the Person Details</h3>
<form:form method="POST" action="addPerson" modelAttribute="person"> <form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person">
<form:errors path="*" cssClass="errorbox" element="div" /> <form:errors path="*" cssClass="errorbox" element="div" />

View File

@ -61,6 +61,5 @@
<td>${person.notes}</td> <td>${person.notes}</td>
</tr> </tr>
</table> </table>
</body> </body>
</html> </html>