使用Eclipse为小白搭建Vue+Java主页:包含两个文本框和登录按钮

这篇文章将带你一步一步使用Eclipse,创建包含两个文本框和一个登录按钮的简单主页,并使用Vue.js和CSS进行前端开发,Java则用于后端。

准备工作:

  1. 安装Eclipse和Java开发环境: 确保你已经安装了Eclipse和Java Development Kit (JDK)。
  2. 新建Java项目:
    • 打开Eclipse,点击“File”->“New”->“Java Project”。
    • 输入项目名称,例如“HomePage”,点击“Finish”。

前端开发:

  1. 创建index.html:
    • 在项目根目录上右键点击,选择“New”->“File”。
    • 输入文件名“index.html”,点击“Finish”。
    • 在index.html中添加以下代码:
<!DOCTYPE html>
<html>
<head>
  <title>主页</title>
  <link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
  <div id='app'>
    <input type='text' v-model='username' placeholder='用户名'>
    <input type='password' v-model='password' placeholder='密码'>
    <button @click='login'>登录</button>
  </div>

  <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script>
  <script src='main.js'></script>
</body>
</html>
  1. 创建style.css:
    • 在项目根目录上右键点击,选择“New”->“File”。
    • 输入文件名“style.css”,点击“Finish”。
    • 在style.css中添加以下代码:
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 100px;
}

input {
  margin-bottom: 10px;
}

button {
  padding: 10px 20px;
}
  1. 创建main.js:
    • 在项目根目录上右键点击,选择“New”->“File”。
    • 输入文件名“main.js”,点击“Finish”。
    • 在main.js中添加以下代码:
new Vue({
  el: '#app',
  data: {
    username: '',
    password: ''
  },
  methods: {
    login() {
      // 登录逻辑,此处仅展示示例
      console.log('用户名:' + this.username);
      console.log('密码:' + this.password);
    }
  }
});

后端开发:

  1. 创建Main类:
    • 在项目根目录上右键点击,选择“New”->“Class”。
    • 输入类名“Main”,点击“Finish”。
    • 在Main类中添加以下代码:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {
  public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase("src/main/webapp");
    webAppContext.setContextPath("/");
    server.setHandler(webAppContext);

    server.start();
    server.join();
  }
}

配置依赖:

  1. 添加服务器运行时:
    • 在项目根目录上右键点击,选择“Properties”。
    • 在左侧导航栏中选择“Java Build Path”,点击“Add Library”。
    • 选择“Server Runtime”,选择你使用的服务器(例如“Apache Tomcat”),点击“Finish”。
  2. 添加Web App Libraries:
    • 在左侧导航栏中选择“Deployment Assembly”,点击“Add”。
    • 选择“Java Build Path Entries”,选择“Web App Libraries”,点击“Finish”。

运行项目:

  1. 配置运行配置:
    • 在Eclipse工具栏中点击“Run”->“Run Configurations”。
    • 在左侧导航栏中选择“Java Application”,点击“New Launch Configuration”。
    • 输入配置名称“HomePage”,选择“Main”类作为“Main class”,点击“Apply”。
  2. 运行项目: 点击“Run”运行项目。

现在你可以在浏览器中访问http://localhost:8080查看你的主页。

注意: 这只是一个简单的示例,你可以根据需要添加更多功能和代码。

希望这篇文章能帮助你开始你的第一个Vue.js+Java项目!