BAEL-1148 earth001@gmail.com - Merged spring-mvc-push module in spring-mvc-simple (#3459)

* Sample code for BAEL-1148 - earth001@gmail.com

* Change tabs for spaces in non java files

* Change tabs for spaces in non java files

* Removed unnecessary argument

* BAEL-1148 earth001@gmail.com - Mapped get method in controller / Removed resources

* Merged spring-mvc-push module in spring-mvc-simple
This commit is contained in:
Juan Moreno
2018-01-22 12:08:33 -03:00
committed by KevinGilmore
parent a115c94f32
commit b1266c833b
23 changed files with 138 additions and 374 deletions

View File

@@ -7,13 +7,13 @@ import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
class ApplicationConfiguration extends WebMvcConfigurerAdapter {
class ApplicationConfiguration implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

View File

@@ -16,24 +16,24 @@ import de.neuland.jade4j.spring.view.JadeViewResolver;
public class JadeTemplateConfiguration {
@Bean
public SpringTemplateLoader templateLoader() {
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
templateLoader.setBasePath("/WEB-INF/views/");
templateLoader.setSuffix(".jade");
return templateLoader;
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
templateLoader.setBasePath("/WEB-INF/views/");
templateLoader.setSuffix(".jade");
return templateLoader;
}
@Bean
public JadeConfiguration jadeConfiguration() {
JadeConfiguration configuration = new JadeConfiguration();
configuration.setCaching(false);
configuration.setTemplateLoader(templateLoader());
return configuration;
JadeConfiguration configuration = new JadeConfiguration();
configuration.setCaching(false);
configuration.setTemplateLoader(templateLoader());
return configuration;
}
@Bean
public ViewResolver viewResolver() {
JadeViewResolver viewResolver = new JadeViewResolver();
viewResolver.setConfiguration(jadeConfiguration());
return viewResolver;
JadeViewResolver viewResolver = new JadeViewResolver();
viewResolver.setConfiguration(jadeConfiguration());
return viewResolver;
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.spring.configuration;
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.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.spring.controller.push")
public class PushConfiguration implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}

View File

@@ -11,42 +11,23 @@ import javax.servlet.ServletRegistration;
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfiguration.class);
//ctx.register(ThymeleafConfiguration.class);
//ctx.register(FreemarkerConfiguration.class);
//ctx.register(GroovyConfiguration.class);
//ctx.register(JadeTemplateConfiguration.class);
ctx.setServletContext(container);
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfiguration.class);
// ctx.register(ThymeleafConfiguration.class);
// ctx.register(FreemarkerConfiguration.class);
// ctx.register(GroovyConfiguration.class);
// ctx.register(JadeTemplateConfiguration.class);
// ctx.register(PushConfiguration.class);
// ctx.setServletContext(container);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(ctx));
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(ctx));
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
// @Override
// public void onStartup(ServletContext container) {
// // Create the 'root' Spring application context
// AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// rootContext.register(ServiceConfig.class, JPAConfig.class, SecurityConfig.class);
//
// // Manage the lifecycle of the root application context
// container.addListener(new ContextLoaderListener(rootContext));
//
// // Create the dispatcher servlet's Spring application context
// AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
// dispatcherServlet.register(MvcConfig.class);
//
// // Register and map the dispatcher servlet
// ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
// dispatcher.setLoadOnStartup(1);
// dispatcher.addMapping("/");
//
// }
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}

View File

@@ -6,10 +6,10 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
public class AnnotationMethodHandlerAdapterExample {
@RequestMapping("/annotedName")
public ModelAndView getEmployeeName() {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Dinesh");
return model;
}
@RequestMapping("/annotedName")
public ModelAndView getEmployeeName() {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Dinesh");
return model;
}
}

View File

@@ -23,13 +23,13 @@ public class FileUploadController implements HandlerExceptionResolver {
}
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView uploadFile(MultipartFile file) throws IOException{
public ModelAndView uploadFile(MultipartFile file) throws IOException {
ModelAndView modelAndView = new ModelAndView("file");
InputStream in = file.getInputStream();
InputStream in = file.getInputStream();
File currDir = new File(".");
String path = currDir.getAbsolutePath();
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename());
FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + file.getOriginalFilename());
int ch = 0;
while ((ch = in.read()) != -1) {
f.write(ch);
@@ -37,15 +37,17 @@ public class FileUploadController implements HandlerExceptionResolver {
f.flush();
f.close();
modelAndView.getModel().put("message", "File uploaded successfully!");
modelAndView.getModel()
.put("message", "File uploaded successfully!");
return modelAndView;
}
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
ModelAndView modelAndView = new ModelAndView("file");
if (exc instanceof MaxUploadSizeExceededException) {
modelAndView.getModel().put("message", "File size exceeds limit!");
modelAndView.getModel()
.put("message", "File size exceeds limit!");
}
return modelAndView;
}

View File

@@ -6,10 +6,10 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
public class RequestMappingHandlerAdapterExample {
@RequestMapping("/requestName")
public ModelAndView getEmployeeName() {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Madhwal");
return model;
}
@RequestMapping("/requestName")
public ModelAndView getEmployeeName() {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Madhwal");
return model;
}
}

View File

@@ -6,13 +6,11 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class SimpleControllerHandlerAdapterExample extends
AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Dinesh Madhwal");
return model;
}
public class SimpleControllerHandlerAdapterExample extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Dinesh Madhwal");
return model;
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.spring.controller.push;
import javax.servlet.http.PushBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PushController {
@GetMapping(path = "/demoWithPush")
public String demoWithPush(PushBuilder pushBuilder) {
if (null != pushBuilder) {
pushBuilder.path("resources/logo.png")
.addHeader("Content-Type", "image/png")
.push();
}
return "demo";
}
@GetMapping(path = "/demoWithoutPush")
public String demoWithoutPush() {
return "demo";
}
}