Skip to content

Commit 09cbc1c

Browse files
earth001KevinGilmore
authored andcommitted
Sample code for BAEL-1148 - earth001@gmail.com (eugenp#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
1 parent 423486b commit 09cbc1c

10 files changed

Lines changed: 258 additions & 0 deletions

File tree

‎spring-mvc-push/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.tern-project

‎spring-mvc-push/pom.xml‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-mvc-push</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>spring-mvc-push</name>
9+
<properties>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
13+
<maven.compiler.version>3.7.0</maven.compiler.version>
14+
<maven-war-plugin.version>3.2.0</maven-war-plugin.version>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<junit.jupiter.version>5.0.2</junit.jupiter.version>
17+
<spring.version>5.0.2.RELEASE</spring.version>
18+
<servlet.version>4.0.0</servlet.version>
19+
<jstl.version>1.2</jstl.version>
20+
<jsp-api.version>2.3.2-b02</jsp-api.version>
21+
<junit.jupiter.version>5.0.2</junit.jupiter.version>
22+
<junit.platform.version>1.0.2</junit.platform.version>
23+
</properties>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-webmvc</artifactId>
28+
<version>${spring.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>javax.servlet</groupId>
32+
<artifactId>javax.servlet-api</artifactId>
33+
<version>${servlet.version}</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>jstl</artifactId>
39+
<version>${jstl.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>javax.servlet.jsp</groupId>
43+
<artifactId>javax.servlet.jsp-api</artifactId>
44+
<version>${jsp-api.version}</version>
45+
</dependency>
46+
<!--Testing -->
47+
<dependency>
48+
<groupId>org.springframework</groupId>
49+
<artifactId>spring-test</artifactId>
50+
<version>${spring.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter-engine</artifactId>
56+
<version>${junit.jupiter.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>${maven.compiler.version}</version>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-war-plugin</artifactId>
70+
<version>${maven-war-plugin.version}</version>
71+
<configuration>
72+
<warName>spring-mvc-push</warName>
73+
<failOnMissingWebXml>false</failOnMissingWebXml>
74+
<outputDirectory>${deploy-path}</outputDirectory>
75+
</configuration>
76+
</plugin>
77+
<plugin>
78+
<artifactId>maven-surefire-plugin</artifactId>
79+
<version>${maven-surefire-plugin.version}</version>
80+
<dependencies>
81+
<dependency>
82+
<groupId>org.junit.platform</groupId>
83+
<artifactId>junit-platform-surefire-provider</artifactId>
84+
<version>${junit.platform.version}</version>
85+
</dependency>
86+
</dependencies>
87+
</plugin>
88+
</plugins>
89+
<finalName>spring-mvc-push</finalName>
90+
</build>
91+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.config;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletRegistration;
6+
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.web.WebApplicationInitializer;
11+
import org.springframework.web.context.ContextLoaderListener;
12+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
13+
import org.springframework.web.servlet.DispatcherServlet;
14+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
15+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
16+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
17+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
18+
19+
@Configuration
20+
@EnableWebMvc
21+
@ComponentScan(basePackages = "com.baeldung.controller")
22+
public class PushConfiguration implements WebApplicationInitializer, WebMvcConfigurer {
23+
24+
@Override
25+
public void onStartup(ServletContext container) throws ServletException {
26+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
27+
context.register(PushConfiguration.class);
28+
container.addListener(new ContextLoaderListener(context));
29+
ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(context));
30+
dispatcher.setLoadOnStartup(1);
31+
dispatcher.addMapping("/");
32+
}
33+
34+
@Bean
35+
public InternalResourceViewResolver jspViewResolver() {
36+
InternalResourceViewResolver bean = new InternalResourceViewResolver();
37+
bean.setPrefix("/WEB-INF/views/");
38+
bean.setSuffix(".jsp");
39+
return bean;
40+
}
41+
42+
@Override
43+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
44+
registry.addResourceHandler("/resources/**")
45+
.addResourceLocations("/resources/");
46+
}
47+
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.controller;
2+
3+
import javax.servlet.http.PushBuilder;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
8+
@Controller
9+
public class PushController {
10+
11+
@RequestMapping(value = "/demoWithPush")
12+
public String demoWithPush(PushBuilder pushBuilder) {
13+
if (null != pushBuilder) {
14+
pushBuilder.path("resources/logo.png")
15+
.addHeader("Content-Type", "image/png")
16+
.push();
17+
pushBuilder.path("resources/script.js")
18+
.addHeader("Content-Type", "text/javascript")
19+
.push();
20+
pushBuilder.path("resources/style.css")
21+
.addHeader("Content-Type", "text/css")
22+
.push();
23+
}
24+
return "demo";
25+
}
26+
27+
@RequestMapping(value = "/demoWithoutPush")
28+
public String demoWithoutPush() {
29+
return "demo";
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>PushBuilder demo</title>
8+
<link href="<c:url value="/resources/style.css"/>" rel="stylesheet" />
9+
</head>
10+
<body>
11+
12+
<span class="single-title">PushBuilder demo</span>
13+
<br>
14+
<img src="<c:url value="/resources/logo.png"/>" alt="Logo" height="126"
15+
width="411">
16+
<br>
17+
<script type="text/javascript"
18+
src="<c:url value="/resources/script.js"/>"></script>
19+
<br> Go to
20+
<a href="/spring-mvc-push/">index</a>
21+
</body>
22+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<title>PushBuilder demo</title>
7+
</head>
8+
<body>
9+
<h4>
10+
Go to <a href="demoWithPush">PushBuilder demo</a><br> Go to
11+
<a href="demoWithoutPush">Simple demo</a>
12+
</h4>
13+
</body>
14+
</html>
7.22 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Script')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.single-title {
2+
font-size: 30px;
3+
color: #535353;
4+
font-weight: 200;
5+
letter-spacing: -1.5px;
6+
line-height: 64px;
7+
max-width: 750px;
8+
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.controller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Disabled;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
13+
import org.springframework.web.context.WebApplicationContext;
14+
15+
import com.baeldung.config.PushConfiguration;
16+
17+
@Disabled
18+
@SpringJUnitWebConfig(PushConfiguration.class)
19+
public class PushControllerIntegrationTest {
20+
@Autowired
21+
private WebApplicationContext webAppContext;
22+
private MockMvc mockMvc;
23+
24+
@BeforeEach
25+
public void setup() {
26+
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
27+
.build();
28+
}
29+
30+
@Test
31+
public void whenDemoWithPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
32+
mockMvc.perform(get("/demoWithPush"))
33+
.andExpect(status().isOk());
34+
}
35+
36+
@Test
37+
public void whenDemoWithoutPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
38+
mockMvc.perform(get("/demoWithoutPush"))
39+
.andExpect(status().isOk());
40+
}
41+
}

0 commit comments

Comments
 (0)