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

JavaEE进阶10——SpringMVC小练习

1.计算器

package cn.edu.hbpu.springbootdemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/calc") public class CalcController { @RequestMapping("/sum") public String calc(Integer num1,Integer num2){ if(num1 == null || num2 == null) { return "请输入两个正整数!"; } return "计算机计算结果为:" + (num1 + num2); } }

2.用户登录:

后端代码(UserController):

package cn.edu.hbpu.springbootdemo; import io.micrometer.common.util.StringUtils; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/user") @RestController public class UserController { @RequestMapping("/login") public Boolean login(String userName, String password, HttpSession session){ if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){ return false; } if("admin".equals(userName) && "admin".equals(password)){ session.setAttribute("loginUser", userName); return true; } return false; } @RequestMapping("/getLoginUser") public String getLoginUser(HttpServletRequest request){ //设置为false,session为null时不会创建session对象 HttpSession session = request.getSession(false); if(session != null) { String loginUser = (String) session.getAttribute("loginUser"); return loginUser; } return ""; } }

前端代码(login.html)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>用户登录</h1> 用户名:<input name="userName" type="text" id="userName"><br> 密码:<input name="password" type="password" id="password"><br> <input type="button" value="登录" onclick="login()"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> function login() { $.ajax({ url:"/user/login", type:"post", data:{ userName:$("#userName").val(), password:$("#password").val() }, success:function(result){ if(result){ location.href="/index.html"; }else{ alert("账号或者密码错误"); } } }); } </script> </body> </html>

index.html

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>用户登录首页</title> </head> <body> 登录人: <span id="loginUser"></span> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> $.ajax({ url:"/user/getLoginUser", type:"post", success:function(userName){ $("#loginUser").text(userName); } }) </script> </body> </html>

3.留言板:

后端(MessageInfo):

package cn.edu.hbpu.springbootdemo; import lombok.Data; @Data public class MessageInfo { private String from; private String to; private String message; }

MessageController:

package cn.edu.hbpu.springbootdemo; import io.micrometer.common.util.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/message") public class MessageController { private List<MessageInfo> messageInfoList = new ArrayList<>(); @RequestMapping("/publish") public Boolean publish(@RequestBody MessageInfo messageInfo){ if(StringUtils.isEmpty(messageInfo.getFrom()) || StringUtils.isEmpty(messageInfo.getTo()) || StringUtils.isEmpty(messageInfo.getMessage())){ return false; } messageInfoList.add(messageInfo); return true; } @RequestMapping("/getList") public List<MessageInfo> getList(){ return messageInfoList; } }

前端(messagewal.html)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>留言板</title> <style> .container { width: 350px; height: 300px; margin: 0 auto; /* border: 1px black solid; */ text-align: center; } .grey { color: grey; } .container .row { width: 350px; height: 40px; display: flex; justify-content: space-between; align-items: center; } .container .row input { width: 260px; height: 30px; } #submit { width: 350px; height: 40px; background-color: orange; color: white; border: none; margin: 10px; border-radius: 5px; font-size: 20px; } </style> </head> <body> <div class="container"> <h1>留言板</h1> <p class="grey">输入后点击提交, 会将信息显示下方空白处</p> <div class="row"> <span>谁:</span> <input type="text" name="" id="from"> </div> <div class="row"> <span>对谁:</span> <input type="text" name="" id="to"> </div> <div class="row"> <span>说什么:</span> <input type="text" name="" id="say"> </div> <input type="button" value="提交" id="submit" onclick="submit()"> <!-- <div>A 对 B 说: hello</div> --> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> $.ajax({ url:"/message/getList", type:"get", success:function(messages){ for(let msg of messages){ let divE = "<div>"+msg.from +"对" + msg.to + "说:" + msg.message+"</div>"; $(".container").append(divE); } } }) function submit(){ //1. 获取留言的内容 var from = $('#from').val(); var to = $('#to').val(); var say = $('#say').val(); if (from== '' || to == '' || say == '') { return; } $.ajax({ url:"/message/publish", type:"post", contentType:"application/json", data:JSON.stringify({ from:from, to:to, message:say }), success:function(result){ if(result){ //2. 构造节点 var divE = "<div>"+from +"对" + to + "说:" + say+"</div>"; //3. 把节点添加到页面上 $(".container").append(divE); //4. 清空输入框的值 $('#from').val(""); $('#to').val(""); $('#say').val(""); }else{ alert("留言失败"); } } }) } </script> </body> </html>

http://www.jsqmd.com/news/476732/

相关文章:

  • 优惠券、满减、折扣同时生效,价格到底怎么算?
  • TI DSP 6678缓存优化全解析:如何用MAR寄存器提升实时性
  • “是我!”庆祝马里奥40年来始终坚持的匠心精神
  • 端口敲门技术深度对比:knockd vs SPA vs SDP,谁更适合你的服务器防护?
  • GIS数据处理必备:ArcMap中北京54与WGS84坐标系的区别与转换技巧
  • 计算机网络的定义和分类
  • EPLAN端子排自定义:从零搭建到高效维护
  • Electron 实战:将用户输入保存到本地文件 —— 基于 `fs.writeFileSync` 与 IPC 的安全写入方案
  • SenseVoice-small-ONNX效果展示:中日韩三国语言混合演讲识别连贯性测试
  • ThinkPad 满分维修评级:进步、妥协与公正性质疑
  • MacBook Air M5:性价比提升与开源支持困境
  • 2024年企业级网络架构实战:跨地域OSPF与BGP混合组网解析
  • 游戏开发必知:透视投影与正交投影的7个核心差异及适用场景
  • pure-ftpd安全配置全指南:从防火墙规则到虚拟用户权限管理
  • 通用文件读写封装:告别重复造轮子,让 C 语言文件操作更高效
  • 个人GPU福音!Kook Zimage真实幻想Turbo在独立游戏美术中的落地实践
  • FFC实战:如何用Fast Fourier Convolution提升图像修复效果(附代码示例)
  • Lattice Radiant 2024.2 从零到一:免费FPGA开发环境搭建与许可激活全攻略
  • 全栈可视化开发新选择 网易 CodeWave 开发效率拉满
  • nanobot效果展示:Qwen3-4B在QQ中执行netstat -tuln并解释监听端口含义
  • 实战指南:如何在鲲鹏云上快速搭建PostgreSQL数据库(含性能调优技巧)
  • 量化交易实战:从零搭建你的首个自动化交易系统(2025版)
  • Niushop开源商城文件上传漏洞实战:从零复现到蚁剑连接完整流程
  • 【ZERO-PAD】基于微雪RP2040-ZERO与QMK的模块化桌面宏键盘DIY全攻略
  • Kook Zimage 真实幻想 Turbo 与AI技术结合:打造高效图像生成方案
  • MusePublic Art Studio艺术治疗应用:心理疗愈图像生成实践案例
  • SDXL-Turbo 保姆级教程:零基础搭建你的实时AI画板
  • 快速上手RexUniNLU:无需标注数据,定义标签即可实现槽位提取
  • 天地图图层代码与坐标系后缀全解析:从URL片段到实战应用
  • Fun-ASR-MLT-Nano-2512快速部署:7860端口映射+HTTPS反向代理Nginx配置示例