【发布时间】:2021-06-30 06:33:28
【问题描述】:
我是 Spring Boot 框架的初学者。我想使用 Spring Boot 应用程序处理搜索记录。当我在员工 ID 文本框中输入员工 ID 并单击搜索按钮时,我的 index.html 已成功加载相关员工姓名结果想要显示以下文本框。但我不知道如何传递它。我累了所以我附在下面。
index.html
<form action="#" th:action="@{/search}" th:object="${employee}" method="post">
<div alight="left">
<tr>
<label class="form-label" >Employee ID</label>
<td>
<input type="hidden" th:field="*{id}" />
<input type="text" th:field="*{id}" class="form-control" placeholder="Employee ID" />
</td>
</tr>
</div>
<br>
<tr>
<td colspan="2"><button type="submit" class="btn btn-info">Search</button> </td>
</tr>
<div alight="left">
<tr>
<label class="form-label" >Employee Name</label>
<td>
<input type="text" th:field="*{ename}" class="form-control" placeholder="Employee Name" />
</td>
</tr>
</div>
</form>
控制器
@Controller
public class EmployeeController {
@Autowired
private EmployeeService service;
@GetMapping("/")
public String add(Model model) {
List<Employee> listemployee = service.listAll();
// model.addAttribute("listemployee", listemployee);
model.addAttribute("employee", new Employee());
return "index";
}
@RequestMapping("/search/{id}")
public ModelAndView showSearchEmployeePage(@PathVariable(name = "id") int id) {
ModelAndView mav = new ModelAndView("new");
Employee emp = service.get(id);
mav.addObject("employee", emp);
return mav;
}
}
实体
@Entity
public class Employee {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String ename;
private int mobile;
private int salary;
public Employee() {
}
public Employee(Long id, String ename, int mobile, int salary) {
this.id = id;
this.ename = ename;
this.mobile = mobile;
this.salary = salary;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", ename=" + ename + ", mobile=" + mobile + ", salary=" + salary + "]";
}
存储库
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
【问题讨论】:
标签: spring spring-boot thymeleaf