软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

【后端一】 郭雨欣

  • 郭 雨欣
  • 2022-10-14
  • 0

First Spring Boot

产品介绍

适用于int类型的简单四则运算

使用指南

  1. 导入IDEA后,在浏览器中进入localhost:8080
  2. 根据你的运算需求选择相应的按钮
  3. 进入相应运算的页面后,依照格式修改URL中的参数:a=_&b=_
  4. 按下回车,得到你的答案

错误类型

Message Description
数据溢出 计算结果2147483647
数据类型错误 输入的值不是整数类型而是浮点类型或字符(字符串)
运行错误 运用/时,/0会报错

考虑到的特殊情况:

  • 做个简单的前端,但是尝试了很多遍,但我貌似还是做不到将前端输入框中的参数运用到后端(前端向后端传参),只能做个简易按钮,然后让用户自己改参数...orz
  • 至于将整数扩展到实数,看了资料也上手尝试过,总是会报错...就没打算往这走了

学习历程

文档:很详细的教程,跟着就会

曲折:

  1. 当localhost:8080拒绝访问的时候,倒腾了好久,查阅了关于Maven的一些知识

  2. 涉及到前端简单的html知识(超链接,制作按钮)

  3. Controller和Restcontroller的区别,比如说Restcontroller一般=Controller+ResponseBody的结合使用,一开始

    Restcontroller的"hello, world"使static里的index.html的页面显示不出来...嗯.

  4. 认识了一些html表单提交知识

  5. 群里发的关于java大数处理方案(Biglnteger和BigDecimal),尝试了但并不能运行,总是报错...

/*在高数作业和英语急需准备小组ppt作业的压迫下,ddl也快到了,我的效率也比较低,只能到这种简陋程度了(T^T),我希望能在之后学了更多知识后再不断填充改进,斯米马赛*/

代码

│  │  ├─main
│  │  │  ├─java
│  │  │  │  └─com
│  │  │  │      └─example
│  │  │  │          └─firstspringboot
│  │  │  │              │  FirstSpringBootApplication.java
│  │  │  │              │  
│  │  │  │              └─controller
│  │  │  │                      Main.java
│  │  │  │                      
│  │  │  └─resources
│  │  │      │  application.properties
│  │  │      │  
│  │  │      ├─static
│  │  │      │      index.html
│  │  │      │      
│  │  │      └─templates
│  │  └─test
│  │      └─java
│  │          └─com
│  │              └─example
│  │                  └─firstspringboot
│  │                          FirstSpringBootApplicationTests.java
│  │                          
│  └─target
│      ├─classes
│      │  │  application.properties
│      │  │  
│      │  ├─com
│      │  │  └─example
│      │  │      └─firstspringboot
│      │  │          │  FirstSpringBootApplication.class
│      │  │          │  
│      │  │          └─controller
│      │  │                  Main.class
│      │  │                  
│      │  └─static
│      │          index.html
│      │          
│      ├─generated-sources
│      │  └─annotations
│      ├─generated-test-sources
│      │  └─test-annotations
│      └─test-classes
│          └─com
│              └─example
│                  └─firstspringboot
│                          FirstSpringBootApplicationTests.class
│                          

./src/main/java/com.example.firstspringboot/controller/Main.java:

package com.example.firstspringboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Main {

    @GetMapping("/add")
    public int add(@RequestParam int a,@RequestParam int b) {
        return a + b;
    }

    @GetMapping("/sub")
    public int sub(@RequestParam int a,@RequestParam int b){
        return a - b ;
    }

    @GetMapping("/muti")
    public int muti(@RequestParam int a,@RequestParam int b){
        return a * b ;
    }

    @GetMapping("/div")
    public int div(@RequestParam int a,@RequestParam int b){
        return a / b ;
    }

}

./src/main/resources/static/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MISERY</title>

</head>

<body>
<h1><strong>简单四则运算</strong></h1>
<p>                    </p>
<p>食用方法:<br/>
    1.根据需求选择对应的按钮使用<br/>
    2.请在URL后面依照格式修改参数a,b:<strong>a=_&b=_</strong><br/>
    3.回车等待结果</p>
</p>
<a href="http://localhost:8080/add?a=8&b=7">
    <button>add+</button>
</a>
<a href="http://localhost:8080/sub?a=8&b=7">
    <button>sub-</button>
</a>
<a href="http://localhost:8080/muti?a=4&b=7">
    <button>muti*</button>
</a>
<a href="http://localhost:8080/div?a=4&b=7">
    <button>div/</button>
</a>

</body>
</html>
郭 雨欣
郭 雨欣
© 2025 软件园学生在线
Theme by Wing