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:
committed by
KevinGilmore
parent
a115c94f32
commit
b1266c833b
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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/");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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("/");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
<context:component-scan base-package="com.baeldung.spring.controller" />
|
||||
<bean
|
||||
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
|
||||
<bean
|
||||
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
|
||||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/views/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,28 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
<context:component-scan base-package="com.baeldung.spring.controller" />
|
||||
<bean
|
||||
class=
|
||||
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
|
||||
<bean
|
||||
class=
|
||||
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
|
||||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/views/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
<bean id="HandlerMapping"
|
||||
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
|
||||
<bean name="/greeting.html"
|
||||
class="com.baeldung.spring.controller.SimpleControllerHandlerAdapterExample"/>
|
||||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/views/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
</beans>
|
||||
17
spring-mvc-simple/src/main/webapp/WEB-INF/views/demo.jsp
Normal file
17
spring-mvc-simple/src/main/webapp/WEB-INF/views/demo.jsp
Normal file
@@ -0,0 +1,17 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>PushBuilder demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<span>PushBuilder demo</span>
|
||||
<br>
|
||||
<img src="<c:url value="/resources/logo.png"/>" alt="Logo"
|
||||
height="126" width="411">
|
||||
<br>
|
||||
<!-- Content -->
|
||||
</body>
|
||||
</html>
|
||||
@@ -4,7 +4,7 @@ html
|
||||
title User Registration
|
||||
body
|
||||
form(action="register" method="post" )
|
||||
label(for="email") Emailaaaaaaaa:
|
||||
label(for="email") Email:
|
||||
input(type="text" name="email")
|
||||
label(for="password") Password:
|
||||
input(type="password" name="password")
|
||||
|
||||
BIN
spring-mvc-simple/src/main/webapp/resources/logo.png
Normal file
BIN
spring-mvc-simple/src/main/webapp/resources/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.2 KiB |
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.controller.push;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.spring.configuration.PushConfiguration;
|
||||
|
||||
@SpringJUnitWebConfig(PushConfiguration.class)
|
||||
public class PushControllerIntegrationTest {
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithoutPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithoutPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user