更新時間:2021-09-10 09:59:56 來源:動力節(jié)點 瀏覽1451次
針對前端傳送過來的一個表單中的數(shù)據(jù)如果后臺對應(yīng)的action方法中沒有跟form表單組成的實體相同名稱的入?yún)?那么就會報出異常:
先上一段前端form表單對應(yīng)的一個action方法,注意其中沒有任何的入?yún)?
@RequestMapping("/profile")
public String displayProfile() {
return "profile/profilePage";
}
前端代碼如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout/default">
<head lang="en">
<title>Your profile</title>
</head>
<body>
<div class="row" layout:fragment="content">
<h2 class="indigo-text center">Personal info</h2>
<form th:action="@{/profile}" method="post" th:object="${profileForm}" class="col m8 s12 offset-m2">
<div class="row">
<div class="input-field col s6">
<input th:filed="${profileForm.twitterHandle}" id="twitterHandle" type="text"/>
<label for="twitterHandle">Twitter handle</label>
</div>
<div class="input-field col s6">
<input th:field="${profileForm.email}" id="email" type="email"/>
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input th:field="${profileForm.birthDate}" id="birthDate" type="text"/>
<label for="birthDate">Birth Date</label>
</div>
</div>
<div class="row s12">
<button class="btn waves-effect waves-light" type="submit" name="save">Submit
<i class="mdi-content-send right"></i>
</button>
</div>
</form>
</div>
</body>
</html>
運行并訪問應(yīng)用回展示如下的前端錯誤。

后端會報:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "profile", template might not exist or might not be accessible by any of the configured Template Resolvers
仔細查看了一下是由于自己沒有在controler方法中加入請求參數(shù),將action方法變?yōu)槿缦录纯赏ㄟ^
@RequestMapping("/profile")
public String displayProfile(ProfileForm profileForm) {
return "profile/profilePage";
}
以上就是動力節(jié)點小編介紹的"SpringMVC的form表單處理",希望對大家有幫助,想了解更多可查看SpringMVC教程。動力節(jié)點在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。