Sample code for BAEL-1148 - earth001@gmail.com (#3268)

* 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
This commit is contained in:
Juan Moreno
2018-01-07 20:51:53 -03:00
committed by KevinGilmore
parent 423486bce1
commit 09cbc1c6ee
10 changed files with 258 additions and 0 deletions
@@ -0,0 +1,48 @@
package com.baeldung.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
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.controller")
public class PushConfiguration implements WebApplicationInitializer, WebMvcConfigurer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(PushConfiguration.class);
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
@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/");
}
}
@@ -0,0 +1,31 @@
package com.baeldung.controller;
import javax.servlet.http.PushBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PushController {
@RequestMapping(value = "/demoWithPush")
public String demoWithPush(PushBuilder pushBuilder) {
if (null != pushBuilder) {
pushBuilder.path("resources/logo.png")
.addHeader("Content-Type", "image/png")
.push();
pushBuilder.path("resources/script.js")
.addHeader("Content-Type", "text/javascript")
.push();
pushBuilder.path("resources/style.css")
.addHeader("Content-Type", "text/css")
.push();
}
return "demo";
}
@RequestMapping(value = "/demoWithoutPush")
public String demoWithoutPush() {
return "demo";
}
}
@@ -0,0 +1,22 @@
<%@ 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>
<link href="<c:url value="/resources/style.css"/>" rel="stylesheet" />
</head>
<body>
<span class="single-title">PushBuilder demo</span>
<br>
<img src="<c:url value="/resources/logo.png"/>" alt="Logo" height="126"
width="411">
<br>
<script type="text/javascript"
src="<c:url value="/resources/script.js"/>"></script>
<br> Go to
<a href="/spring-mvc-push/">index</a>
</body>
</html>
+14
View File
@@ -0,0 +1,14 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PushBuilder demo</title>
</head>
<body>
<h4>
Go to <a href="demoWithPush">PushBuilder demo</a><br> Go to
<a href="demoWithoutPush">Simple demo</a>
</h4>
</body>
</html>
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

@@ -0,0 +1 @@
console.log('Script')
@@ -0,0 +1,9 @@
.single-title {
font-size: 30px;
color: #535353;
font-weight: 200;
letter-spacing: -1.5px;
line-height: 64px;
max-width: 750px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif
}
@@ -0,0 +1,41 @@
package com.baeldung.controller;
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.Disabled;
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.config.PushConfiguration;
@Disabled
@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());
}
}