如何使用百里香叶"ID"选择列表项并定向到另一个详细信息页面?



我正在尝试使用SpringBoot,Hibernate和Thymeleaf构建一个应用程序。

我想在使用 Thymeleaf 的服务列表中获取所选服务的"id",在数据库中搜索此服务,然后定向它到另一个页面,根据所选列表项显示服务的详细信息,但是我收到以下错误消息:

 EL1007E: Property or field 'name' cannot be found on null

显然,我正在获得在Thymeleaf服务列表中选择的服务"id"。我不知道这是否正确,但至少所选的服务 ID 出现在"产品详细信息"页面 URL 中。

查看产品详情

<!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>

查看索引.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>{
}

有人可以告诉我正确的方法吗?

实际上你在控制器中犯了一个愚蠢的错误

@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";
}

试试这个并给出一个答复,无论它是否有效

最新更新