【问题标题】:how to select a List Item by "ID" using Thymeleaf and direct to another detail page?如何使用 Thymeleaf 按“ID”选择列表项并定向到另一个详细信息页面?
【发布时间】:2019-06-11 05:53:35
【问题描述】:

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

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

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

显然,我正在使用 Thymeleaf 获取在我的服务列表中选择的服务的“id”。我不知道这是否正确,但至少选定的服务 ID 出现在“productdetail”页面 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>

查看 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>{

}

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

【问题讨论】:

  • 我发布了一个答案。检查这个!

标签: java hibernate spring-boot thymeleaf


【解决方案1】:

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

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

试试这个,不管它是否有效并给出回复

【讨论】:

  • Avijit,我按照你给我看的做了,消息错误已经改变。 EL1008E: Property or field 'name' cannot be found on object of type 'java.util.Optional' - maybe not public or not valid? 你知道我的可选变量有什么问题吗?我该如何解决?谢谢队友
  • 看来这家伙和我有同样的问题。[stackoverflow.com/questions/50904742/…我试过了,但我不知道如何将这个解决方案应用到我的代码中。
  • @MarkusBranco 我更新了我的答案。你能试试吗
  • Avijit,它显示一条消息错误说我无法从 Optional 转换为 Service
  • @MarkusBranco 你能发帖ServiceRepository吗?
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 2015-09-10
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 2016-09-03
相关资源
最近更新 更多