主流のフロントエンド フレームワークを 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"> <div> <input ng-model="name"> <button ng-click="greet()">Greet</button> </div> <h3 ng-bind="greeting"></h3> </body> </html>
React統合
// main.js angular.module('myApp', []) .controller('myCtrl', function($scope) { $scope.greeting = ""; $scope.greet = function() { $scope.greeting = "Hello, " + $scope.name + "!"; }; });
@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> <div id="root"></div> </body> </html>
Vue.js統合
// main.js const Greeting = () => <h1>Hello, World!</h1>; ReactDOM.render(<Greeting />, document.getElementById("root"));
@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> <div id="app"></div> </body> </html>
実際のケース
単純なWebを構築していると仮定しますユーザーが名前と年齢のプロフィールを作成できるアプリケーション。 Spring Boot と Angular を使用してフロントエンドを統合する方法は次のとおりです:
Java バックエンド
// main.js new Vue({ el: "#app", data: { message: "Hello, Vue!" }, template: "<div>{{ message }}</div>" });
Angular Frontend
@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); } }
<!-- profile.html --> <div> <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> </div>
このガイドに従うことで、フロントエンド フレームワークを Java バックエンドと簡単に統合し、強力な Web アプリケーションを作成できます。 。
以上がJava フレームワークを使用してフロントエンド フレームワークを統合するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。