软件园学生在线

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

登录与注册

【后端一】 李朝阳

  • LiZhaoyang
  • 2022-10-15
  • 0

目录

1. 产品说明

2. 学习历程

3. 代码展示


1. 产品说明

(因为还没学会前后端传输,所以只能手打url给参数了......)

  • 主体是4个GetMapping注解,通过调用不同方法,进行不同种类的运算
  • 代码没有数据校验和溢出检测功能(因为我的@Valid注解没生效)
  • 输入的数据可以是小数(用的double类型)
操作url如下(四选一,x和y是变量):
localhost:8080/jia?a=x&b=y
localhost:8080/jian?c=x&d=y
localhost:8080/cheng?e=x&f=y
localhost:8080/chu?g=x&h=y

2. 学习历程

数据检测

使用@valid注解:

  • @AssertFalse 带注解的元素必须为false,支持boolean/Boolean
  • @AssertTrue 带注解的元素必须为true,支持boolean/Boolean
  • @DecimalMax(Value) 带注解的元素必须是一个数字,其值必须小于等于指定的最大值
  • @DecimalMin(Value) 带注解的元素必须是一个数字,其值必须大于等于指定的最小值
  • @Max(Value) 带注解的元素必须是一个整数,其值必须小于等于指定的最大值
  • @Min(Value) 带注解的元素必须是一个整数,其值必须大于等于指定的最小值
  • @NotNull 带注解的元素不能是Null
  • @Null 带注解的元素必须是Null
  • @Size(max,min) 带注解的元素必须在该范围之内

依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.6.5</version>
</dependency>

摘抄的一段示例代码:

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Validsample {

    @Size(min=2, max=5)
    @NotNull(message = "name不能为null")
    private String name;

    @NotNull(message = "type不能为null")
    @Max(3)
    private Integer type;

    @NotNull(message = "parentId不能为null")
    private Integer parentId;

    @NotNull(message = "orderNum不能为null")
    private Integer orderNum;

前端设计
使用HTML表单创建输入框,按钮等元素
代码如下(是.html格式的文件):

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

<!--文本输入框 text
value="123"   默认初始值
placeholder   提示文本
maxlength="8" 最长可以写几个字符
size="30"     文本框的长度
-->
<p>
  <input type="text" value=" " placeholder="Hello" maxlength="15" size="30">
</p>

<!--多行文本框 textarea
rows="25" 文本框的长度
cols="5"  文本框的宽度
-->
<p>
  <input type="textarea" cols="25" rows="5">text2</textarea>
</p>

<!--单选框 radio
value: 单选框的值
name一样 表示是一个组的
-->
<p>
  <input type="radio" value="boy" name="sex"/>男
  <input type="radio" value="girl" name="sex"/>女
</p>

<!--多选框 checkbox
value: 多选框的值
name一样 表示是一个组的
-->
<p>
  <input type="checkbox" value="hobby">睡懒觉
  <input type="checkbox" value="hobby">敲代码
  <input type="checkbox" value="hobby">玩电脑
  <input type="checkbox" value="hobby">学高数

</p>

<!--按钮 checked
默认是选中
input type="button" 普通按钮
input type="image"  图像按钮
input type="submit" 提交按钮
input type="reset"  重置按钮
-->
<form action="我的第一个网页.html" method="get">
  <p>
    <input type="button" name="button1" value="九阴真经点击就送">
    <input type="image" src="Images/springboot.jpg">
  </p>

  <p>
    <input type="submit">
    <input type="reset">
  </p>

</form>
</body>
</html>

前后端传输

是真没学明白,有机会去前端蹭个课:)

我的问题
下面是@Valid未生效的代码,可以请学长们看看是哪里有问题嘛:)

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;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;

@RestController
public class Main {
    @GetMapping("/jia")
    @Valid
    @DecimalMax(value= "2147483247.0" , message= "请输入合适的数据" )
    @DecimalMin(value= "-2147483247.0" , message= "请输入合适的数据" )
    public double jia(@RequestParam double a , @RequestParam double b){
        return a + b ;
    }
}

3. 代码展示

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("/")
    public String helloWorld(){
        return "Hello,world!";
    }

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

    @GetMapping("jian")

    public double jian(@RequestParam double c , @RequestParam double d){
        return c - d ;
    }
    @GetMapping("cheng")

    public double cheng(@RequestParam double e , @RequestParam double f){
        return e * f ;
    }
    @GetMapping("chu")

    public double chu(@RequestParam double g , @RequestParam double h){
        return g / h ;
    }
}
LiZhaoyang
LiZhaoyang
© 2025 软件园学生在线
Theme by Wing