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;



@Controller

public 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,输入年龄并提交。

在这里插入图片描述

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值