Skip to content

Commit 896a2d0

Browse files
binodpantamaibin
authored andcommitted
* commit first as binodpanta * revert test change * A short example of real-time event streaming using Spring WebFlux * Code for http://jira.baeldung.com/browse/BAEL-1527 * remove unrelated files * Apply feedback changes to rename test and remove link from readme file, ongoing work * Update formatting fixes to code and add pom changes, that partially fix test runnning issues in IDE but not in cmdline * Apply Eclipse formatter to test code and apply suggested pom fixes * BAEL-1527 Formatting fix in pom.xml * Use string.format to cleanup logging code * BAEL-1527 Changed logging pattern * Start the spring-boot-vue module, WIP * some small updates with comments * Add index html template page * merge pom.xml fixes * Add integration test with MockMvc to verify index.html content is rendered correctly * fix up pom merge issues * merge issues fix for pom * pom end of file newline
1 parent 977a92e commit 896a2d0

9 files changed

Lines changed: 248 additions & 1 deletion

File tree

‎pom.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@
422422
<module>spring-boot-persistence</module>
423423
<module>spring-boot-security</module>
424424
<module>spring-boot-mvc</module>
425+
<module>spring-boot-vue</module>
425426
<module>spring-boot-logging-log4j2</module>
426427
<module>spring-cloud-data-flow</module>
427428
<module>spring-cloud</module>
@@ -1223,4 +1224,5 @@
12231224
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
12241225
</properties>
12251226

1226-
</project>
1227+
</project>
1228+

‎spring-boot-vue/.gitignore‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

‎spring-boot-vue/pom.xml‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-boot-vue</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-vue</name>
12+
<description>Demo project for Spring Boot Vue project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<!-- needed to render html templates in /resources/templates -->
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
56+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.springbootmvc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
7+
@SpringBootApplication
8+
public class SpringBootMvcApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(SpringBootMvcApplication.class, args);
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.springbootmvc.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
@Controller
9+
public class MainController {
10+
11+
@RequestMapping(value = "/", method = RequestMethod.GET)
12+
public String index(Model model) {
13+
// this attribute will be available in the view index.html as a thymeleaf variable
14+
model.addAttribute("eventName", "FIFA 2018");
15+
// this just means render index.html from static/ area
16+
return "index";
17+
}
18+
}

‎spring-boot-vue/src/main/resources/application.properties‎

Whitespace-only changes.
14.7 KB
Binary file not shown.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!doctype html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<!-- Include Bootstrap -->
9+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
10+
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
11+
<!-- Optional theme -->
12+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
13+
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
14+
</head>
15+
<body>
16+
<div class="container-fluid">
17+
<h3 class="display-3">This is an example Vue.js application developed with Spring Boot</h3>
18+
<p class="lead">This file is rendered by a Spring built-in default controller for index.html (/) using
19+
Spring's built-in
20+
Thymeleaf templating engine.
21+
Although we don't need it per se, we customized the information passed to this
22+
view from thymeleaf by adding a controller method for "/" route to demonstrate how to pass information from
23+
Thymeleaf to this page.
24+
The combination of template engine and frontend framework like Vue can make this a powerful approach to build
25+
more complex applications while leveraging the benefits of a framework like Vue.js.
26+
You can use thymeleaf features too but this project focuses mainly on using Vue.js on the
27+
frontend as the framework and makes minimal use of Thymeleaf.
28+
Also we don't use any routing and multiple components in this example, so what you see is technically a
29+
Single Page Application (SPA) without any routes configured.
30+
</p>
31+
32+
<div id="contents-main">
33+
<div class="lead">
34+
<strong>Name of Event:</strong>
35+
<span th:text="${eventName}"></span>
36+
</div>
37+
<div id="contents">
38+
<!-- Since we create a Vue component pointing to id=contents,
39+
Vue will generate a unordered list of items such
40+
as this inside this div.
41+
v-for will cause a loop to run over all players
42+
as per the players array found in app.data
43+
<ul>
44+
<li></li>
45+
<li></li>
46+
</ul>
47+
-->
48+
<ul>
49+
<li style="list-style-type:none" v-for="player in players">
50+
<player-card
51+
v-bind:player="player" v-bind:key="player.id">
52+
</player-card>
53+
</li>
54+
</ul>
55+
</div>
56+
</div>
57+
</div>
58+
59+
<!-- include Vue.js -->
60+
<!-- we include babel js so that we can write ES6 code in the browser
61+
for a more production like setup it is recommended to setup a build process
62+
to transpile and minify the code (such as using webpack)
63+
-->
64+
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
65+
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
66+
<script type="text/babel">
67+
// player-card is now a Vue component that generates a 'card' showing a player
68+
// It can be used
69+
// declaratively like <player-card v-bind:player="someplayer">
70+
Vue.component('player-card', {
71+
props: ['player'],
72+
template: `<div class="card">
73+
<div class="card-body">
74+
<h6 class="card-title">
75+
{{ player.name }}
76+
</h6>
77+
<p class="card-text">
78+
<div>
79+
{{ player.description }}
80+
</div>
81+
</p>
82+
</div>
83+
</div>`
84+
});
85+
86+
var app = new Vue({
87+
el: '#contents',
88+
data: {
89+
players: [
90+
{id: "1", name: "Lionel Messi", description: "Argentina's superstar"},
91+
{id: "2", name: "Christiano Ronaldo", description: "Portugal top-ranked player"}
92+
]
93+
}
94+
});
95+
96+
</script>
97+
</body>
98+
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.springbootmvc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
11+
12+
import static org.hamcrest.CoreMatchers.containsString;
13+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
@AutoConfigureMockMvc
19+
public class SpringBootMvcApplicationTests {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
/**
25+
* If this test passes, we got a page with the thymeleaf provided variable
26+
* value for eventName
27+
*/
28+
@Test
29+
public void shouldLoadCorrectIndexPage() throws Exception {
30+
mockMvc.perform(get("/")).andExpect(status().isOk()).
31+
andExpect(MockMvcResultMatchers.content()
32+
.string(containsString("FIFA 2018")));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)