要使用IntelliJ IDEA中的Spring Boot和VS Code中的JavaScript (AJAX)创建一个简单的WebApp,你需要遵循以下步骤:
确保你已经安装了以下工具:
npm init -y
来创建一个package.json
文件。express
和axios
:express
和axios
:在Spring Boot项目的src/main/java
目录下,创建一个控制器类,例如HelloController.java
:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot!";
}
}
在VS Code的前端项目文件夹中,创建一个index.html
文件和一个app.js
文件。
index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple WebApp</title>
</head>
<body>
<h1 id="message"></h1>
<script src="app.js"></script>
</body>
</html>
app.js
:
document.addEventListener('DOMContentLoaded', function() {
axios.get('http://localhost:8080/hello')
.then(function(response) {
document.getElementById('message').textContent = response.data;
})
.catch(function(error) {
console.error('There was an error!', error);
});
});
http-server
:http-server
:打开浏览器,访问http://localhost:8080
(或者是你的HTTP服务器提供的地址),你应该能看到显示“Hello from Spring Boot!”的消息。
通过以上步骤,你应该能够成功创建一个简单的WebApp,其中后端使用Spring Boot,前端使用JavaScript (AJAX)。
没有搜到相关的文章