当前位置: 首页 > news >正文

java-SpringBoot框架开发计算器网页端编程练习项目【web版】

今天分享一个使用springboot 写一个 前后端不分离的项目,网页计算器,来熟悉springboot框架的使用。
java版本:8。
springboot:2.6.13
使用的技术是:
Java + Spring Boot + Thymeleaf + HTML/CSS/JS 构建的 Web 端简约按钮式计算器。
熟悉 Spring Boot 控制器(@Controller, @GetMapping, @PostMapping)
· 掌握表单提交与参数绑定(@RequestParam)
· 学会使用 Thymeleaf 在前端绑定变量
· 理解 HTML 与 JS 如何联动后端数据
先给大家看一下做出来的效果:
在这里插入图片描述
项目部分代码:

package com.jsonl.jisuanqi.controller;/**** User: Json* Date: 2025/6/21**/import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;/*** User:Json* Date: 2025/6/21**/
@Controller
public class IndexController {// 创建 JavaScript 脚本引擎,用于后端计算表达式private final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");// 显示主页面,初始化表达式为空@GetMapping("/")public String home(Model model) {model.addAttribute("expression", ""); // 页面首次加载无表达式return "calculator"; // 返回 calculator.html}// 处理计算按钮提交的表达式@PostMapping("/calculate")public String calculate(@RequestParam String expression, Model model) {try {// 若表达式为空或无效,设置错误信息if (expression == null || expression.trim().isEmpty()) {model.addAttribute("result", "错误");expression = "";} else {// 使用 JavaScript 引擎计算表达式结果Object result = engine.eval(expression);model.addAttribute("result", result.toString()); // 设置结果expression = result.toString(); // 把结果变成新表达式,支持连续计算}} catch (Exception e) {// 计算出错时显示“错误”model.addAttribute("result", "错误");expression = "";}// 将最新表达式返回页面继续显示model.addAttribute("expression", expression);return "calculator";}// 清除表达式(点击 C 时)@GetMapping("/reset")public String reset(Model model) {model.addAttribute("expression", "");model.addAttribute("result", "");return "calculator";}
}

在这里插入图片描述
有兴趣的小伙伴,可以拿去看看,希望能在你编程学习的过程中帮助到你。
完整代码和简单的操作说明已经打包好了。可以获取:
https://wwwoop.com/home/Index/projectInfo?goodsId=92&typeParam=2&subKey=1

相关文章:

  • 网站建设制作 南京公司关键词搜索量查询
  • 交通信用网站建设百度官网电话
  • 进qq空间上面没有网站上海搜索关键词排名
  • 浙江网站制作公司最新的全国疫情数据
  • 网站开发服务器资源怎么弄黄山网络推广公司
  • 免费单页网站建设深圳网络营销策划
  • 马克思主义基本原理知识笔记
  • MediaMarktSaturn EDI 对接指南:欧洲零售卖场的数字化协同范例
  • 虚幻基础:插槽
  • C++面试6——类和结构体的区别和使用场景
  • 零基础学习RabbitMQ(3)--核心概念
  • 打包上传到Linux部署并启动
  • C++ string类的操作
  • FFMPEG常用函数
  • 应用层协议 HTTP
  • 618风控战升级,瑞数信息“动态安全+AI”利剑出鞘
  • 无人机航电系统之语音通信技术篇
  • elk+filebeat收集springboot项目日志
  • 开疆智能CCLinkIE转ModbusTCP网关连接川崎机器人配置案例
  • 桥头守望者
  • WRF模式与Python融合技术在多领域中的应用及精美绘图;Python助力WRF自动化运行、WRF模式前后处理
  • Android Navigation 架构
  • 从虚拟机角度解释python3相对导入问题(下)
  • 创始人IP打造:知识付费领域破局的核心方法论
  • 服务器安装指南
  • PyEcharts教程(008):PyEchart仪表盘