news 2026/6/17 1:47:11

web作业5

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
web作业5

一、复现上课案例

package com.example.spring.controller; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { //添加一个简单的HelloController @RequestMapping("/hello") public String hello(){ System.out.println("Hello World122221 ~"); return "Hello World1234 ~"; } }

reg1.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户注册</title> </head> <body> <h2>用户注册</h2> <%-- 表单数据提交到 /reg 这个URL,使用POST方法 --%> <form action="/reg1" method="post"> 姓名:<input type="text" name="name" /><br/><br/> 年龄:<input type="text" name="age" /><br/><br/> <input type="submit" value="提交注册" /> </form> </body> </html>

reg-result1.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册结果</title> </head> <body> <h4>使用 request.getParameter() 获取参数值</h4> <h2>注册信息如下:</h2> <%-- 使用EL表达式显示Controller中存入Model的数据 --%> 姓名:${userName}<br/> 年龄:${userAge}<br/> <a href="/reg1">返回注册页面</a> </body> </html>

RegrequestController.java

package com.example.spring.controller; 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 jakarta.servlet.http.HttpServletRequest; @Controller public class RegrequestController { // 处理访问 /reg 的GET请求,显示注册页面 @GetMapping("/reg1") public String showRegPage() { // 返回的逻辑视图名是 "reg",根据配置会映射到 /WEB-INF/jsp/reg.jsp return "reg1"; } // 处理来自注册页面的POST表单提交 @PostMapping("/reg1") public String handleReg(HttpServletRequest request, Model model) { // 使用 request.getParameter() 获取参数值 String name = request.getParameter("name"); String ageParam = request.getParameter("age"); // 将年龄参数转换为 Integer 类型 Integer age = null; if (ageParam != null && !ageParam.isEmpty()) { age = Integer.parseInt(ageParam); } // 将接收到的数据存入Model,以便在结果页面上显示 model.addAttribute("userName", name); model.addAttribute("userAge", age); // 返回逻辑视图名 "reg-result",映射到 /WEB-INF/jsp/reg-result.jsp return "reg-result1"; } }

二、apifox中建立接口

三、完成新页面

reg3.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户注册reg3</title> </head> <body> <h2>3、完成新的页面</h2> <%-- method="post" 提交到当前 /reg3 --%> <form action="/reg3" method="post"> <p>姓名:<input type="text" name="name"></p> <p>年龄:<input type="text" name="age"></p> <p>性别: <input type="radio" name="gender" value="male" id="man"> <label for="man">男</label> <input type="radio" name="gender" value="female" id="woman"> <label for="woman">女</label> </p> <p><input type="submit" value="提交注册"></p> </form> </body> </html>

reg-result3.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册结果</title> </head> <body> <h2>注册信息如下:</h2> <p>姓名:${username}</p> <p>年龄:${userAge}</p> <p>性别:${userGender}</p> <p><a href="/reg3">返回注册页面</a></p> </body> </html>

Reg3Controller.java

package com.example.spring.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/reg3") public class Reg3Controller { // 访问表单页面 GET @RequestMapping public String showForm(){ // 跳转到表单页面 reg3.jsp return "reg3"; } // 提交表单 POST @PostMapping public String submitReg( @RequestParam String name, @RequestParam int age, @RequestParam String gender, Model model ){ // 把参数传给结果页面 model.addAttribute("username", name); model.addAttribute("userAge", age); model.addAttribute("userGender", gender); // 跳转结果页 reg3-result.jsp return "reg-result3"; } }

四、在apifox中新建接口,并测试。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/17 1:31:03

MAA明日方舟自动化助手:如何彻底解放你的游戏时间

MAA明日方舟自动化助手&#xff1a;如何彻底解放你的游戏时间 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手&#xff0c;全日常一键长草&#xff01;| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: https://gitcode…

作者头像 李华
网站建设 2026/6/17 1:27:07

G-Helper:华硕笔记本性能优化的终极轻量级解决方案

G-Helper&#xff1a;华硕笔记本性能优化的终极轻量级解决方案 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops with nearly the same functionality. Works with ROG Zephyrus, Flow, TUF, Strix, Scar, ProArt, Vivobook, Zenbook, Exper…

作者头像 李华
网站建设 2026/6/17 1:21:59

PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战

一、引言&#xff1a;被忽略的"最后一公里"难题 在企业级AI应用落地的过程中&#xff0c;文档解析始终是一个被低估的环节。JBoltAI平台的工程团队在大量项目实践中深刻认识到&#xff0c;文档解析的准确率直接决定了下游所有AI应用的天花板。大量企业在推进RAG知识…

作者头像 李华
网站建设 2026/6/17 1:20:22

【共创季稿事节】鸿蒙原生ArkTS布局方式之Flex+flexGrow弹性增长布局

鸿蒙原生ArkTS布局方式之FlexflexGrow弹性增长布局一、引言&#xff1a;为什么需要弹性增长布局&#xff1f; 在鸿蒙原生应用开发中&#xff0c;布局是构建用户界面的基石。随着设备形态的多样化——从折叠屏到平板&#xff0c;从手表到车机——应用界面需要能够自适应不同屏幕…

作者头像 李华
网站建设 2026/6/17 1:19:05

Stateflow状态机建模:开关控制LED灯状态

汇聚博主多年工程经验的新书《Simulink嵌入式开发实战》现已上市&#xff0c;针对Simulink与MBD开发技术&#xff0c;形成了一套完整的知识体系和方法论。详细介绍和优惠链接可以参考博客《Simulink嵌入式开发实战》新书上市&#xff0c;感谢粉丝长期以来的支持&#xff01; 文…

作者头像 李华
网站建设 2026/6/17 1:15:24

Shell脚本保护利器SHC:从入门到精通

Shell脚本保护利器SHC&#xff1a;从入门到精通 【免费下载链接】shc Shell script compiler 项目地址: https://gitcode.com/gh_mirrors/sh/shc Shell脚本加密与保护是许多开发者和系统管理员面临的常见需求&#xff0c;SHC&#xff08;Shell Script Compiler&#xff…

作者头像 李华