我是靠谱客的博主 跳跃鼠标,这篇文章主要介绍Springboot + WebJars + React : HelloWorld 项目pom.xmlSpringBoot Java入口程序前端 React 应用运行效果,现在分享给大家,希望可以做个参考。

作为Java程序员,某些情况下,我们需要ReactES6严谨并且快速地表达一些原型思想。这种情况下,全套的类似npm+webpack这种工具栈略显厚重,完全使用原生Javascript或者jQuery表达逻辑又显得过于底层和零碎。这个时候,我们可以考虑使用Springboot + WebJars + React。该组合有如下好处 :

  • 目标web应用可以以jar包方式独立部署和运行;
  • 使用WebJars方式管理三方JavaScript包,避免琐碎易错的手工管理;
  • 前后端开发在同一个开发项目内完成;
  • ES6模块化使JavaScriptJava语言有更多相似,对Java开发者更友好;
  • React组件能力+JSX让你从琐碎底层的Javascript+DOM操作细节上解脱出来,专注于业务要素;

现在,我们分享一下源代码。需要先说明的一点是,这是一个maven项目。

pom.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <groupId>com.andy</groupId> <artifactId>springboot-webjar-react-demo</artifactId> <version>0.0.1</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.webjars.npm</groupId> <artifactId>react</artifactId> <version>16.8.5</version> </dependency> <dependency> <groupId>org.webjars.npm</groupId> <artifactId>react-dom</artifactId> <version>16.8.5</version> </dependency> <dependency> <groupId>org.webjars.bower</groupId> <artifactId>babel</artifactId> <version>5.8.38</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.3.1</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> <version>0.30</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

对该项目的一些解释 :

  • 使用 spring-boot 2.1.4;
  • 使用spring-boot-starter-web声明这是一个web项目,缺省使用内置tomcat8080端口提供服务;
  • 引入react/react-dom webjars,这是ReactJS web开发必须可少的本尊;
  • 引入bootstrap webjar用于css效果演示;
  • 因为bootstrap潜在依赖于jquery,所以也引入了jquery webjar
  • 因为浏览器并不能理解JSX语法,所以需要引入babel webjar;
  • 引入webjars-locator,这样HTML文件中三方库引入时可以省略版本信息;

SpringBoot Java入口程序

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.andy.webJarWebDemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoWebApplication { public static void main(String[] args) { SpringApplication.run(DemoWebApplication.class, args); } }

前端 React 应用

入口页面 resources/static/index.html

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="/webjars/jquery/jquery.min.js"></script> <script src="/webjars/bootstrap/js/bootstrap.min.js"></script> <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"/> <script src="/webjars/react/umd/react.production.min.js"></script> <script src="/webjars/react-dom/umd/react-dom.production.min.js"></script> <script src="/webjars/babel/browser.min.js"></script> <script type="text/babel" src="./app.jsx"></script> <title>Springboot WebJars ReactJS -- Hello World</title> </head> <body> <div id="root"></div> </body> </html>

对本入口文件的一些说明 :

  • 使用 resources/static/index.html是因为Spring MVC会自动将其映射到/;
  • 注意上面./app.jsx的脚本内容语法实际上是JSX,所以type必须为text/babel以确保浏览器可以理解;
  • 注意上面./app.jsx中的前缀./不能省略;
  • 注意idrootHTML元素div,这是跟下面React应用组件代码的连接点;

React应用组件 resources/static/app.jsx

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class App extends React.Component { render() { return (<div className="jumbotron jumbotron-fluid"> <div className="container"> <h1 className="display-4">Hello World</h1> <p className="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p> <a className="btn btn-primary btn-lg" href="#" role="button">Learn more</a> </div> </div>); } } // ======================================== ReactDOM.render(<App/>, document.getElementById('root'));

对本React应用组件的一些说明 :

  • 语法实际上是JSX,文件结尾可以是jsx,也可以是其他,比如js;
  • 该文件定义了一个React组件App,它向用户展示了一个Bootstrapjumbotron组件;
  • ReactDOM.renderReact组件App渲染到idrootHTML元素div

运行效果

最终我们运行该应用,它会在http://localhost:8080展示该页面如下 :
在这里插入图片描述

最后

以上就是跳跃鼠标最近收集整理的关于Springboot + WebJars + React : HelloWorld 项目pom.xmlSpringBoot Java入口程序前端 React 应用运行效果的全部内容,更多相关Springboot内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部