最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進行校驗的示例代碼

 更新時間:2020年09月02日 11:29:52   作者:IT小村  
這篇文章主要介紹了SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進行校驗的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、校驗分類

數(shù)據(jù)的校驗一般分為**前端校驗、后端校驗**

二、前端校驗

前端校驗是最為明顯的,先說一下:

① HTML

非空校驗HTML5 新增的屬性required="true",一旦沒有填寫就輸入框就顯示紅色,具體使用如:

<input type="text" id="name" name="name" required="true"/>

② JS

同時在提交表單發(fā)送 Ajax請求 的時候,來個 onSubmit 函數(shù),具體例如(使用點 EasyUI ):

function submitData(){
		$("#fm").form("submit",{
			url:"/admin/film/save",
			onSubmit:function(){
				var content=CKEDITOR.instances.content.getData();
				if(content==""){
					$.messager.alert("系統(tǒng)提示","內(nèi)容不能為空!");
					return false;
				}
				return $(this).form("validate");
			},
			success:function(result){
				var result=eval('('+result+')');
				if(result.success){
					$.messager.alert("系統(tǒng)提示","保存成功!");
					resetValue();
				}else{
					$.messager.alert("系統(tǒng)提示","保存失??!");
				}
				
			}
		});
	}

但我們都知道,這是防君子不防小人的做法,用戶可以使用 F12,查看源碼,修改關(guān)鍵部位的代碼,
如把 required="true" 刪除掉,就可以提交表單了。
所以前端作用雖然明顯,但是數(shù)據(jù)處理方面,真正用處并不大。

三、后端校驗

前面說了那么多,就是為了引出 后端校驗 這一話題。數(shù)據(jù)是否提交到數(shù)據(jù)庫中去,就看后端的代碼了。
后端校驗,主要實施在 JavaBean、Controller 中。下面列舉一個簡單的例子,從代碼中說明一切。

① 代碼結(jié)構(gòu)圖

② entity

實體屬性部位空,一般使用如 @NotEmpty(message="請輸入用戶名!") ,這樣既不能為 ,也不能為null

package com.cun.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import io.swagger.annotations.ApiModelProperty;

@Entity
@Table(name = "t_person")
public class Person {

	@Id
	@GeneratedValue
	@ApiModelProperty(value = "用戶id")
	private Integer id;

	@NotBlank(message = "用戶名不能為空") // 為""/''都不行
	@Size(min = 2, max = 30, message = "2<長度<30")
	@Column(length = 50)
	@ApiModelProperty(value = "用戶名")
	private String userName;

	@NotNull(message = "用戶密碼不能為空")
	@Column(length = 50)
	@ApiModelProperty(value = "用戶密碼")
	private String password;

	@Max(value = 150, message = "age應(yīng)<150") // 數(shù)字
	@Min(value = 1, message = "age應(yīng)>1") // 數(shù)字
	@NotNull(message = "年齡不能為空")
	@ApiModelProperty(value = "用戶年齡")
	private Integer age;

	@NotNull(message = "郵箱不為空")
	@Email(message = "郵件格式不對")
	@Column(length = 100)
	@ApiModelProperty(value = "用戶郵箱")
	private String email;

	// 使用 JPA 必備
	public Person() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}

③ dao

其實也沒什么代碼,這就是 JPA 的強大之處

package com.cun.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.cun.entity.Person;

public interface PersonDao extends JpaRepository<Person, Integer>, JpaSpecificationExecutor<Person> {

}

④ Service、ServiceImpl (省略)

⑤ Controller

package com.cun.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cun.dao.PersonDao;
import com.cun.entity.Person;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@RestController
@RequestMapping("/person")
@EnableSwagger2
public class PersonController {

	@Autowired
	private PersonDao personDao;

	@PostMapping("/insert")
	public Map<String, Object> insertPerson(@Valid Person person, BindingResult bindingResult) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			List<String> mesList=new ArrayList<String>();
			for (int i = 0; i < errorList.size(); i++) {
				mesList.add(errorList.get(i).getDefaultMessage());
			}
			map.put("status", false);
			map.put("error", mesList);
		} else {
			map.put("status", true);
			map.put("msg", "添加成功");
			personDao.save(person);
		}
		return map;
	}

}

⑥ yml

server:
 port: 80 #為了以后訪問項目不用寫端口號
 context-path: / #為了以后訪問項目不用寫項目名
spring:
 datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/springboot
  username: root
  password: 123
 jpa:
  hibernate:
   ddl-auto: update #數(shù)據(jù)庫同步代碼
  show-sql: true   #dao操作時,顯示sql語句

⑦ POM

使用 SpringBoot Starter 導(dǎo)入 JPA、MySQL

使用 Swagger 演示

<!-- swagger生成接口API -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>

		<!-- 接口API生成html文檔 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>

四、演示

輸入 http://localhost/swagger-ui.html 進入接口測試站點

什么都沒有填寫,直接點擊Try it out!,可以看到返回給前端的 JSON 數(shù)據(jù),這時候數(shù)據(jù)的數(shù)據(jù)是沒有改動的,一條sql 語句都沒有執(zhí)行

當(dāng)然還可以進行其他測試,這里就省略了

到此這篇關(guān)于SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進行校驗的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot JSR303校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于工廠方法模式的Java實現(xiàn)

    關(guān)于工廠方法模式的Java實現(xiàn)

    這篇文章主要介紹了關(guān)于工廠方法模式的Java實現(xiàn)講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot過濾器的使用

    SpringBoot過濾器的使用

    過濾器是對數(shù)據(jù)進行過濾,預(yù)處理過程,當(dāng)我們訪問網(wǎng)站時,有時候會發(fā)布一些敏感信息,發(fā)完以后有的會用*替代,還有就是登陸權(quán)限控制等,一個資源,沒有經(jīng)過授權(quán),肯定是不能讓用戶隨便訪問的,這個時候,也可以用到過濾器,需要的朋友可以參考一下
    2021-11-11
  • 淺談String類型如何轉(zhuǎn)換為time類型存進數(shù)據(jù)庫

    淺談String類型如何轉(zhuǎn)換為time類型存進數(shù)據(jù)庫

    這篇文章主要介紹了String類型如何轉(zhuǎn)換為time類型存進數(shù)據(jù)庫,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • spring中WebClient如何設(shè)置連接超時時間以及讀取超時時間

    spring中WebClient如何設(shè)置連接超時時間以及讀取超時時間

    這篇文章主要給大家介紹了關(guān)于spring中WebClient如何設(shè)置連接超時時間以及讀取超時時間的相關(guān)資料,WebClient是Spring框架5.0引入的基于響應(yīng)式編程模型的HTTP客戶端,它提供一種簡便的方式來處理HTTP請求和響應(yīng),需要的朋友可以參考下
    2024-08-08
  • JavaSE經(jīng)典小練習(xí)項目之拷貝文件夾

    JavaSE經(jīng)典小練習(xí)項目之拷貝文件夾

    文件拷貝是一個常見的任務(wù),無論是備份文件,還是將文件從一個位置復(fù)制到另一個位置,文件拷貝都是必不可少的,這篇文章主要給大家介紹了關(guān)于JavaSE經(jīng)典小練習(xí)項目之拷貝文件夾的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Java中mybatis的三種分頁方式

    Java中mybatis的三種分頁方式

    這篇文章主要介紹了Java中mybatis的三種分頁方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • spring boot 如何指定profile啟動

    spring boot 如何指定profile啟動

    這篇文章主要介紹了spring boot 如何指定profile啟動的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 通過Java實現(xiàn)對PDF頁面的詳細(xì)設(shè)置

    通過Java實現(xiàn)對PDF頁面的詳細(xì)設(shè)置

    這篇文章主要介紹了通過Java實現(xiàn)對PDF頁面的詳細(xì)設(shè)置,下面的示例將介紹通過Java編程來對PDF頁面進行個性化設(shè)置的方法,包括設(shè)置頁面大小、頁邊距、紙張方向、頁面旋轉(zhuǎn)等,需要的朋友可以參考下
    2019-07-07
  • java實現(xiàn)根據(jù)ip地址獲取地理位置

    java實現(xiàn)根據(jù)ip地址獲取地理位置

    本文給大家匯總介紹了2種分別使用新浪和淘寶接口,實現(xiàn)根據(jù)IP地址獲取詳細(xì)的地理位置的代碼,非常的實用,有需要的小伙伴可以參考下。
    2016-03-03
  • SpringBoot中封裝Cors自動配置方式

    SpringBoot中封裝Cors自動配置方式

    這篇文章主要介紹了SpringBoot中封裝Cors自動配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03

最新評論

惠东县| 云安县| 大悟县| 和硕县| 台湾省| 静乐县| 汾西县| 顺平县| 龙胜| 怀柔区| 沧源| 邻水| 来凤县| 九江县| 海城市| 高陵县| 汕头市| 远安县| 长治市| 雅安市| 信阳市| 银川市| 绥宁县| 永城市| 新营市| 特克斯县| 湛江市| 太仓市| 临高县| 洞口县| 民勤县| 潢川县| 南郑县| 和田县| 长子县| 铜陵市| 青冈县| 丽水市| 崇左市| 西宁市| 尖扎县|