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

springboot简单案例

必答[简答题]从页面输入年龄,输入的年龄在1-200之间为正常,其余定义为异常,并把年龄结果显示在页面上,同时给出判断(正常输出“年龄是XX岁“;若年龄大于200或小于0,输出自定义异常”年龄输入不正确“;其他显示异常情况)要求:(1)给出上述程序的代码,填写到答案区。(代码要有格式)(2)给出程序运行结果的截图

在Spring Boot中实现上述功能,我们需要创建一个简单的Web应用程序。以下是实现步骤和代码示例:

  1. 创建Spring Boot项目:可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。
    (https://start.aliyun.com/)或者在阿里云提供的在线服务选好,下载demo

在这里插入图片描述
再添加一个thymeleaf
在这里插入图片描述

  1. 添加依赖:在pom.xml文件中添加Spring Web依赖。

  2. 创建Controller:创建一个控制器来处理HTTP请求。

  3. 创建Service:创建一个服务来处理业务逻辑。

  4. 创建Repository(可选):如果需要持久化数据,可以创建一个Repository。

  5. 创建Entity(可选):如果需要持久化数据,可以创建一个实体类。

  6. 编写前端页面:创建一个HTML页面来输入年龄。

或者手动添加

以下是具体的代码示例:

pom.xml(添加Spring Web依赖)


<dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
</dependencies>

AgeController.java


package com.example.demo.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 org.springframework.web.bind.annotation.RequestParam;@Controllerpublic class AgeController {@GetMapping("/age")public String ageForm() {return "ageForm";}@PostMapping("/submitAge")public String submitAge(@RequestParam("age") int age, Model model) {if (age >= 1 && age <= 200) {model.addAttribute("message", "年龄是" + age + "岁");} else {model.addAttribute("message", "年龄输入不正确");}return "result";}}

AgeService.java


package com.example.demo.service;public class AgeService {public String checkAge(int age) {if (age >= 1 && age <= 200) {return "年龄是" + age + "岁";} else {return "年龄输入不正确";}}}

result.html


<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>年龄验证结果</title></head><body><h1 th:text="${message}"></h1></body></html>

ageForm.html


<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>输入年龄</title></head><body><form action="/submitAge" method="post"><label for="age">请输入年龄:</label><input type="number" id="age" name="age" min="1" max="200"><button type="submit">提交</button></form></body></html>

application.properties


spring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.html

application.yml

server:port: 8090
spring:mvc:static-path-pattern: /**web:resources:static-locations: classpath:/static/

运行步骤:

  1. 创建项目:使用Spring Initializr创建项目,并添加Spring Web依赖。

  2. 编写代码:按照上述代码示例编写控制器、服务、视图等。

  3. 运行项目:使用Spring Boot运行项目。

  4. 访问页面:在浏览器中访问http://localhost:8090/age,输入年龄并提交。

在这里插入图片描述

在这里插入图片描述

附上目录结构
在这里插入图片描述


http://www.mrgr.cn/news/46756.html

相关文章:

  • Windows10的MinGW安装和VS Code配置C/C++编译环境
  • 速盾:cdn经常换ip有利于SEO吗?
  • ECCV24高分论文:MVSplat稀疏视图下的高效的前馈三维重建模型
  • iPhone 16 Pro 現斷觸問題,疑為 iOS 18 韌體所致
  • 右键菜单添加cmd
  • Springboot 阿里云对象存储OSS 工具类
  • C++实现字符串 trim,C++实现字符串split, C++如何分割字符串为数组,C++如何去除字符串两边的空格
  • 【AIGC】OpenAI API在快速开发中的实践与应用:优化ChatGPT提示词Prompt加速工程
  • 速盾:cdn加速是高防cdn好还是普通cdn好?
  • 物联网(IoT)的未来发展:智能互联时代的到来
  • SQL进阶技巧:Order by 中 NULLS LAST特性使用?
  • Kotlin真·全平台——Kotlin Compose Multiplatform Mobile(kotlin跨平台方案、KMP、KMM)
  • OpenCV库模块解析
  • KVM虚拟化技术介绍
  • ESP32-C3实现ADC
  • 代码随想录算法训练营第五六天| 99. 岛屿数量 100. 岛屿的最大面积
  • 搜维尔科技:Haption远程操作项目模拟项目
  • 动态内存分配 VS 静态内存分配
  • 【虚拟化】内核级虚拟化技术KVM介绍,全/半虚拟化的区别,使用libvirt搭建虚拟化平台(go/java/c++)
  • 这个问题做项目的时给某些客户普及过,这里再给你普及一下