日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring Boot中登录错误消息的显示

發布時間:2025/3/15 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot中登录错误消息的显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

概念

演示


?

概念

通過模板引擎取出服務器發來的msg,通過模板引擎中的th:if標簽,判斷是否有msg,從而使得一個p標簽起作用。

?

演示

初始化截圖:

輸入了錯誤的用戶名和密碼:

關鍵代碼如下:

程序結構如下:

源碼如下:

MyMvcConfig.java

package firstlogindemo.demo.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {@Beanpublic WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("index.html").setViewName("index");}};return adapter;} }

LoginController.java

package firstlogindemo.demo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.thymeleaf.util.StringUtils;import java.util.Map;@Controller public class LoginController {//@DeleteMapping//@PutMapping//@GetMapping//@RequestMapping(value = "/usr/login", method = RequestMethod.POST)@PostMapping(value = "/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Map<String, Object> map){if(!StringUtils.isEmpty(username) && "123456".equals(password)){return "success";}map.put("msg", "用戶名密碼錯誤");return "index";} }

index.html

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><form th:action="@{/user/login}" method="post"><p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p><p>用戶名:<input name="username" type="text" placeholder="userName"></p><p>密 碼:<input name="password" type="password" placeholder="Password"></p><button type="submit">提交</button> </form></body> </html>

success.html

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

application.properties

spring.thymeleaf.cache=false

porn.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.19.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.loginWebDemo</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>loginWeb</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--引入jquery-webjar--><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.3.1</version></dependency><!--引入bootstrap--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>4.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

?

總結

以上是生活随笔為你收集整理的Spring Boot中登录错误消息的显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。