我正在尝试创建注册页面。该网站非常简单,主页将返回注册页面,单击“提交”后,应将其保存到数据库中。但是我在获取页面时遇到了麻烦。
Applicant.java
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;
@Entity
@Table(name = "bayilik_basvuru")
public class Applicant {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotBlank(message = "İsim Soyisim Girilmelidir.")
@Column(name = "isim_soyisim")
private String adSoyad;
public Applicant() {
}
public String getAdSoyad() {
return adSoyad;
}
public void setAdSoyad(String adSoyad) {
this.adSoyad = adSoyad;
}
}
控制器
@Controller
public class HomeContoller {
@Autowired
private ApplicantDAO dao;
@RequestMapping("/")
public ModelAndView getApplicationPage(){
ModelAndView model = new ModelAndView();
Applicant applicant = new Applicant();
model.addObject("applicant",applicant);
model.setViewName("index");
return model;
}
@PostMapping("/save")
public String saveApplicant(@Valid Applicant applicant, BindingResult result){
if (result.hasErrors()) {
return "add-student";
}
dao.save(applicant);
return "index";
}
}
Index.html->表单的一部分,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Bayilik Ön Başvuru Formu</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" th:href="@{../css/style.css}" />
<link rel="stylesheet" type="text/css" th:href="@{../css/bootstrap-datetimepicker.min.css}" />
<link rel="stylesheet" type="text/css" th:href="@{../css/roboto-font.css}" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<form class="form-horizantal" th:action="@{/save}" th:object="${applicant}"
method="POST">
<div class="form-group">
<label class="col-md-4 control-label">İsim Soyisim</label>
<div class="col-md-8">
<input type="text" th:field="*{adSoyad}" class="form-control" /> <!-- it gives error at this line at the start of the th:field-->
<span th:if="${#fields.hasErrors('adSoyad')}" th:errors="*{adSoyad}" class="text-danger"></span>
</div>
</div>
</body>
</html>
我已经处理了好几个小时了。我已经将其与许多示例进行了比较,并且html文件看起来不错,控制器看起来不错,我不知道我在做什么错。
在我的pom文件中,我有百里香;
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
...
<dependendcies>
顺便说一句是这个错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Oct 31 01:40:47 EET 2019
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 27, col 56) // this line is the same line I showed on the index.html file
答案 0 :(得分:2)
您没有在/ save端点上使用注释@ModelAttribute
。尝试像这样使用它:
@PostMapping("/save")
public String saveApplicant( @Valid @ModelAttribute("applicant") Applicant applicant, BindingResult result){
if (result.hasErrors()) {
return "add-student";
}
dao.save(applicant);
return "index";
}
在您的百里香叶模板th:object="${applicant}"
中,表达式声明了用于收集表单数据的模型对象。因此,在控制器中,您必须使用批注@ModelAttribute
将申请人对象绑定到传入的表单内容。
答案 1 :(得分:2)
更改get方法以正确添加对象:
<?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>
<groupId>multithreadedCalls</groupId>
<artifactId>multithreadedCalls</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
</dependencies>
</project>
查看Lombok项目可大大简化您的bean。您缺少@GetMapping("/") //simpler, more informative
public String getApplicationPage(Model model){
model.addAttribute("applicant", new Applicant()); //note method name
return "index";
}
属性的getter和setter,并且在类上标注id
和@Getter
会删除很多样板。
您还希望在post方法上为@Setter
参数添加@ModelAttribute
注释。
将来,也请发布您的完整堆栈跟踪。
答案 2 :(得分:1)
项目上似乎存在层次结构问题。在修复文件层次结构之后,代码开始工作。
感谢答复者的尝试,但是他们不知道问题的核心,因为我没有在问题上附加层次结构的屏幕截图。