【发布时间】:2022-01-14 04:31:01
【问题描述】:
我使用单个 HTML 表单创建了一个 spring boot crud 应用程序。单击编辑按钮时,我想在文本字段中显示数据以进行修改。但是在控制台中打印的特定数据没有出现在文本字段中。我找不到什么问题。
人物实体类
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "persion_id")
private int persondId;
@Column(name = "persion_name")
private String personName;
@Column(name = "persion_contact_no")
private String personContactNo;
@Column(name = "persion_address")
private String personAddress;
public Person() {
}
public Person(int persondId, String personName, String personContactNo, String personAddress) {
this.persondId = persondId;
this.personName = personName;
this.personContactNo = personContactNo;
this.personAddress = personAddress;
}
public int getPersondId() {
return persondId;
}
public void setPersondId(int persondId) {
this.persondId = persondId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public String getPersonContactNo() {
return personContactNo;
}
public void setPersonContactNo(String personContactNo) {
this.personContactNo = personContactNo;
}
public String getPersonAddress() {
return personAddress;
}
public void setPersonAddress(String personAddress) {
this.personAddress = personAddress;
}
@Override
public String toString() {
return "Person [persondId=" + persondId + ", personName=" + personName + ", personContactNo=" + personContactNo
+ ", personAddress=" + personAddress + "]";
}
}
控制器类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.obydul.spring.model.Person;
import com.obydul.spring.service.PersonService;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/")
public String home() {
return "person";
}
@RequestMapping("/person")
public String showPerson(Model model) {
Person person = new Person();
model.addAttribute("person", person);
model.addAttribute("persons", personService.getAllPersons());
return "person";
}
@RequestMapping(value = "/person_save", method = RequestMethod.POST)
public String savePerson(@ModelAttribute Person person, Model model) {
personService.savePerson(person);
// model.addAttribute("persons", personService.getAllPersons());
return "redirect:/person";
}
@RequestMapping("/person_edit/{persondId}")
public String editPerson(@PathVariable int persondId, Model model) {
model.addAttribute("person", personService.getPersonById(persondId));
System.out.println("person edit :: " + personService.getPersonById(persondId));
return "redirect:/person";
}
@GetMapping("/persons/{persondId}")
public String deleteStudent(@PathVariable int persondId) {
personService.deletePersonById(persondId);
return "redirect:/person";
}
}
Person.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Person Information Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<br>
<div class="container">
<div class="col-lg-10 col-md-8 col-sm-6 container justify-content-center card">
<h3 class="text-center">Person Info</h3>
<div class="card-body">
<form action="#" th:action="@{/person_save}" th:object="${person}"
method="post">
<div class="form-group">
<label>Person Name</label> <input type="text" name="personName"
th:field="*{personName}" class="form-control" />
</div>
<div class="form-group">
<label>Contact No</label> <input type="text"
name="personContactNo" th:field="*{personContactNo}"
class="form-control" />
</div>
<div class="form-group">
<label>Address</label> <input type="text" name="personAddress"
th:field="*{personAddress}" class="form-control" />
</div>
<div class="box-footer">
<button type="submit" class="btn btn-success">Save</button>
<button type="reset" class="btn btn-info">Reset</button>
</div>
<input type="hidden" id="persondId" th:field="*{persondId}"/>
<br>
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th class="text-center">Person Id</th>
<th class="text-center">Person Name</th>
<th class="text-center">Contact No</th>
<th class="text-center">Address</th>
<th class="text-center">Edit</th>
<th class="text-center">Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="person: ${persons}">
<td class="text-center" th:text="${person.persondId}" />
<td th:text="${person.personName}" />
<td class="text-center" th:text="${person.personContactNo}" />
<td th:text="${person.personAddress}" />
<td class="text-center">
<a th:href="@{/person_edit/{persondId}(persondId=${person.persondId})}" class="btn btn-primary">Edit</a>
</td>
<td class="text-center">
<a th:href="@{/persons/{persondId}(persondId=${person.persondId})}" class="btn btn-danger">Delete</a>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
pom.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 https://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>2.6.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.obydul.spring </groupId>
<artifactId>SpringBoot_Hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBoot_Hibernate</name>
<description>spring boot hibernate one to many relationship</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
个人信息屏幕
控制台输出
人员编辑 :: 人员 [persondId=8, personName=John, personContactNo=021450000, personAddress=Dhaka]
【问题讨论】:
标签: spring spring-boot spring-mvc spring-data-jpa spring-data