前臺代碼
(xxx.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="iform" th:action="@{/list}" th:method="post" th:object="${user}">
<input type="text" th:field="*{name}"/>
<input type="submit">
</form>
</body>
</html>
后臺代碼
package com.example.demo;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.bean.user;
@Controller
public class Test {
@RequestMapping("/test")
public String login(Model model) {
return "xxx";
}
@RequestMapping("/list")
public String user(@ModelAttribute(value="https://bbs.csdn.net/topics/user")user u, Model model,HttpServletRequest req) {
String name=u.getName();
name=req.getAttribute("name").toString();
model.addAttribute("name", name);
return "login";
}
}
資料系結的bean
package com.example.bean;
import javax.validation.constraints.NotEmpty;
public class user {
@NotEmpty(message="用戶名不能為空")
private String name;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotEmpty(message="密碼不能為空")
private String pwd;
}
報錯資訊
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/xxx.html]")
org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "xxx" - line 10, col 20)
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "xxx" - line 10, col 20)
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
Neither BindingResult nor plain target object for bean name 'user' available as request attribute
ERROR 4672 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/xxx.html]")] with root cause
以上
我是通過
http://localhost:8080/test 進入到xxx.html的xxx里面有個form表單我本來是想通過表單提交資料到后臺,但是現在只要進入到xxx.html就報錯問題好像是出現在<input type="text" th:field="*{name}"/> 里面的th:field="*{name}" 我在網上找了很多資料但是大多數都不是針對我出現的狀況麻煩大佬們指點迷津小弟萬分感謝
uj5u.com熱心網友回復:
取值的時候 應該類似這樣 ${user.name}uj5u.com熱心網友回復:
你把 th:object="${user}" 從form 表單摘出去, 單獨設定個div 將 你的input 包起來。 或者按照樓上那么弄。 試試uj5u.com熱心網友回復:
我不是取值我是和后臺系結
uj5u.com熱心網友回復:
這樣就不能和后臺bean系結了啊
uj5u.com熱心網友回復:
錯誤是告訴你,我thymeleaf找不到你要取的值,所以我無法決議這個頁面,請檢查thymeleaf引數是否接受正確uj5u.com熱心網友回復:
改成@RestControlleruj5u.com熱心網友回復:
你控制器的 user 方法使用的是 @ModelAttribute(value="https://bbs.csdn.net/topics/user")user u user物件,而你form表單中傳的是 *{name},用 ${user.name} 試試uj5u.com熱心網友回復:
!${name}uj5u.com熱心網友回復:
controller改下:@RequestMapping("/test")
public String login(Model model) {
model.addAttribute("user",new user());
return "xxx";
}
uj5u.com熱心網友回復:
你的頁面xxx,但是執行test方法中的model沒有賦值,所以${user}是nulluj5u.com熱心網友回復:

///
(template: "xxx" - line 10, col 20)
///
這個已經指出錯誤的位置了.
uj5u.com熱心網友回復:
你的頁面標簽中指定標簽的name屬性了嘛,form格式的請求資料,和jquery的serizabled/serizabledArray異曲同工之妙,都是根據標簽的name資料實作傳值的效果uj5u.com熱心網友回復:
正解這里,直接使用name屬性傳值,或者在進入此頁面的Controller把空物件通過model.addAttribute("object", new xxx())傳入,這里的物件是你上面th:object中的物件xxx.uj5u.com熱心網友回復:
之前我也是這個錯,后來發現Model寫成ModelAndView了試試這樣寫
uj5u.com熱心網友回復:
大哥,你解決沒有啊,大家都説是user物件里面的某個屬性沒有定義,但是我看著都定義了呀,方法都試了,結果沒用。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/64695.html
標籤:Web 開發
上一篇:Java ssh連接linux 輸出su命令沒反應,求大神指點
下一篇:鴻蒙開發-撰寫應用程式第一個界面
