我试图将帖子发送到DB。但是,当我尝试使用它们时,它显示了以下错误:
EL1007E:在null异常上找不到属性或字段“名称” 评估SpringEL表达式:“名称”(模板:“ StudentForm”-行 264,col 49)模板解析期间发生错误(模板: “类路径资源[templates / StudentForm.html]”)
这是我在下面尝试过的:
StudentForm.html
<div class="col-lg-10">
<div class="card">
<div class="card-header">
<strong>Basic Form</strong> Elements
</div>
<div class="card-body card-block">
<form th:action="@{/StudentForm}" th:Object="${student}" method="post" enctype="multipart/form-data" class="form-horizontal" role="form">
<div class="row form-group">
<div class="col col-md-3">
<label for="text-input" th:field="*{name}" class=" form-control-label">Ismi sharifi</label>
</div>
<div class="col-12 col-md-9">
<input type="text" id="text-input" placeholder="Iltimos ismni kiriting" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col col-md-3">
<label for="select" class=" form-control-label">Fakulteti</label>
</div>
<!-- <div class="col-12 col-md-9">
<select name="select" id="select" class="form-control">
<option value="0">Fakultetni tanlang</option>
<option value="1">Option #1</option>
<option value="2">Option #2</option>
<option value="3">Option #3</option>
</select>
</div>
</div> -->
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label">Email Input</label>
</div>
<div class="col-12 col-md-9">
<input type="email" id="email-input" th:field="*{email}" placeholder="Emailni kiriting" class="form-control">
<small class="help-block form-text"></small>
</div>
</div>
<div class="row form-group">
<div class="col col-md-3">
<label for="text-input" class=" form-control-label">To'langan summa</label>
</div>
<div class="col-12 col-md-9">
<input type="text" id="text-input" placeholder="Summani kiriting" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col col-md-3">
<label for="file-input" class=" form-control-label">File input</label>
</div>
<div class="col-12 col-md-9">
<input type="file" id="file-input" name="file-input" class="form-control-file">
</div>
</div>
</form>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Yuklash
</button>
<button type="reset" class="btn btn-danger btn-sm">
<i class="fa fa-ban"></i> Reset
</button>
</div>
</div>
StudentController.java
package io.javabrains;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import io.javabrains.Entities.Student;
import io.javabrains.repository.StudentRepository;
import io.javabrains.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentRepository repository;
@Autowired
private StudentService service;
@RequestMapping(value= {"/student"},method=RequestMethod.GET)
public ModelAndView student(HttpServletRequest request) {
request.setAttribute("Student", service.findAll());
request.setAttribute("mode", "MODE_TASKS");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("student");
return modelAndView;
}
@RequestMapping(value="/StudentForm",method=RequestMethod.POST)
public ModelAndView studentForm(@Valid Student student,BindingResult bindingResult,ModelMap modelMap)
{
ModelAndView modelAndView = new ModelAndView();
service.addStudent(student);
modelAndView.addObject("student",new Student());
modelAndView.setViewName("studentForm");
return modelAndView;
}
@RequestMapping(value={"/StudentForm"},method=RequestMethod.GET)
public ModelAndView studentForm() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("StudentForm");
return modelAndView;
}
public List<Student>getAllEmployers(){
return service.findAll();
}
StudentEntity.java
package io.javabrains.Entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "studentId")
private int studentId;
@Column(name="name")
@NotNull(message = "Name is compulsort")
private String Name;
@Column(name="email")
@NotNull(message = "Email is compulsory")
private String Email;
@Column(name="department")
@NotNull(message = "department is compulsort")
private String Department;
@Column(name="amount")
@NotNull(message = "Amount is compulsort")
private double Amount;
@ManyToOne
@JoinColumn(name="studentId",referencedColumnName="id",insertable=false, updatable=false)
private DepartmentCategory departmentCategory;
public Student()
{
}
public Student(int studentId, String name, String email, String department, double amount) {
super();
studentId = studentId;
Name = name;
Email = email;
Department = department;
Amount = amount;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
studentId = studentId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
public double getAmount() {
return Amount;
}
public void setAmount(double amount) {
Amount = amount;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", Name=" + Name + ", Email=" + Email + ", Department=" + Department
+ ", Amount=" + Amount + ", departmentCategory=" + departmentCategory + "]";
}
}
答案 0 :(得分:0)
在studentform.html
中,您已添加
th:field="*{name}"
但是在Student
类中,您已经声明
private String Name;
两者都应该匹配。将其更改为
private String name;
类似地,在其他地方也进行更改,然后尝试。
答案 1 :(得分:0)
您没有设置“学生”对象,应该是这样
@RequestMapping(value={"/StudentForm"},method=RequestMethod.GET)
public ModelAndView studentForm() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("student",new Student());
modelAndView.setViewName("StudentForm");
return modelAndView;
}