主流前端框架与 java 后端集成的步骤:angular 整合:使用 spring boot 创建后端、导入 angular 脚本、创建 html 页面、定义控制器。react 整合:同上,但使用 react 脚本和组件。vue.js 整合:同上,但使用 vue.js 脚本和组件。
如何利用 Java 框架集成前端框架
在现代 Web 开发中,将前端框架与 Java 后端框架集成已变得越来越普遍。本文将介绍如何使用 Spring Boot 与 Angular、React 和 Vue.js 等流行前端框架进行集成。
Angular 整合
@SpringBootApplication public class SpringBootAngularDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAngularDemoApplication.class, args); } }
登录后复制
<!-- angular.html --> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script> </head> <body ng-app="myApp"> <p> <input ng-model="name"> <button ng-click="greet()">Greet</button> </p> <h3 ng-bind="greeting"></h3> </body> </html>
登录后复制
// main.js angular.module('myApp', []) .controller('myCtrl', function($scope) { $scope.greeting = ""; $scope.greet = function() { $scope.greeting = "Hello, " + $scope.name + "!"; }; });
登录后复制
React 整合
@SpringBootApplication public class SpringBootReactDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootReactDemoApplication.class, args); } }
登录后复制
<!-- index.html --> <html> <head> <script src="https://unpkg.com/react@17/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script> <script src="main.js"></script> </head> <body> <p id="root"></p> </body> </html>
登录后复制
// main.js const Greeting = () => <h1>Hello, World!</h1>; ReactDOM.render(<Greeting />, document.getElementById("root"));
登录后复制
Vue.js 整合
@SpringBootApplication public class SpringBootVueDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootVueDemoApplication.class, args); } }
登录后复制
<!-- index.html --> <html> <head> <script src="https://unpkg.com/vue@2.6.12/dist/vue.js"></script> <script src="main.js"></script> </head> <body> <p id="app"></p> </body> </html>
登录后复制
// main.js new Vue({ el: "#app", data: { message: "Hello, Vue!" }, template: "<p>{{ message }}</p>" });
登录后复制
实战案例
假设您正在构建一个简单的 Web 应用,该应用允许用户创建一个包含姓名和年龄的个人资料。以下是如何使用 Spring Boot 和 Angular 集成前端:
Java 后端
@Entity public class Profile { private Long id; private String name; private Integer age; // getters and setters } @SpringBootApplication public class SpringBootAngularDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAngularDemoApplication.class, args); } }
登录后复制
Angular 前端
<!-- profile.html --> <p> <form> <label>Name:</label> <input type="text" [(ngModel)]="profile.name"> <br> <label>Age:</label> <input type="number" [(ngModel)]="profile.age"> <br> <button type="submit">Save</button> </form> </p>
登录后复制
// profiles.controller.js angular.module('myApp') .controller('ProfilesCtrl', function($scope, $http) { $scope.save = function() { $http.post('/profiles', $scope.profile) // handle server response }; });
登录后复制
通过遵循本指南,您可以轻松地将前端框架与 Java 后端集成,并创建功能强大的 Web 应用程序。
以上就是如何利用Java框架集成前端框架?的详细内容,更多请关注叮当号网其它相关文章!
文章来自互联网,只做分享使用。发布者:牧草,转转请注明出处:https://www.dingdanghao.com/article/538564.html