我正在尝试使用SpringBoot,Hibernate和Thymeleaf构建应用程序。
我想使用Thymeleaf在服务列表中获取选定服务的“ id”,在数据库中搜索该服务,然后将其定向 转到另一个页面,该页面根据所选列表项显示服务的详细信息,但是我收到以下错误消息:
EL1007E: Property or field 'name' cannot be found on null
显然,我在Thymeleaf的服务列表中获得了所选服务的“ id”。我不知道这是否正确,但是“ productdetail”页面网址中至少出现了所选的服务ID。
查看产品详细信息
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Product Detail</h1>
<p>Name: </p>
<p th:text="${service.name}"></p>
</body>
</html>
查看index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div id="wrapper">
<header id="home">
<div id="serviceGalery">
<div class="service" th:each="service : ${services}" >
<p th:text="${service.name}"></p>
<p th:text="${service.description}"></p>
<p th:value="${service.id}" ></p>
<p th:text="${service.grade}"></p>
<a th:href="@{/productdetail/{id}(id = ${service.id}) }">Buy</a>
</div>
</div>
</header>
</div>
</body>
</html>
控制器
@Controller
public class ServiceController {
@Autowired
private ServiceRepository repository;
@RequestMapping("/")
public String index(Model model) {
Iterable<Service> services = repository.findAll();
model.addAttribute("services", services);
return "index";
}
@RequestMapping(value = "/productdetail/{id}", method = RequestMethod.GET)
public String productDetail(@PathVariable("id") Long serviceId) {
Optional<Service> service = repository.findById(serviceId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("service", service);
System.out.println("teste: " + serviceId);
return "productdetail";
}
型号
package com.markus.getachef.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="servico")
public class Service {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String grade;
private String description;
public Service() {
public Service(String name, String grade, String description) {
super();
this.name = name;
this.grade = grade;
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
存储库
package com.markus.getachef.repository;
import org.springframework.data.repository.CrudRepository;
import com.markus.getachef.model.Service;
public interface ServiceRepository extends CrudRepository<Service, Long>{
}
有人可以告诉我正确的方法吗?
答案 0 :(得分:0)
实际上,您在控制器中犯了一个愚蠢的错误
@RequestMapping(value = "/productdetail/{id}", method = RequestMethod.GET)
public String productDetail(@PathVariable("id") Long serviceId) {
Optional<Service> service = repository.findById(serviceId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("service", service);
System.out.println("teste: " + serviceId);
return "productdetail";
}
您正在定义模型和视图。根据您的要求,您的控制器将类似于
@RequestMapping(value = "/productdetail/{id}", method = RequestMethod.GET)
public String productDetail(@PathVariable("id") Long serviceId, Model model) {
Service service = repository.getFirstById(serviceId);
model.addAttribute("service", service);
System.out.println("teste: " + serviceId);
return "productdetail";
}
尝试一下并给出答复是否有效