Select List Item by "ID" using Thymeleaf and direct to another detail page

Asked

Viewed 520 times

0

I’m trying to build an application using Springboot, Hibernate and Thymeleaf.

I would like to take the "id" of the selected service in the list using Thymeleaf, search this service in the database and then direct it to another page that shows the details of the service according to the item in the selected list, but I am getting the following error message:

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

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

Apparently I’m getting the "id" of the selected service with Thymeleaf. I don’t know if it’s correct but at least the id of the selected service is appearing in the URL of the page "productdetail".

View productdetail:

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

Controller:

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

Model:

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

}

Can someone show me the right way to do this?

  • You are sending one <Optional> to the view: Optional<Service> service = repository.findById(serviceId);``, por isso não acha o getterparaname`

  • Denis Souza, I am a beginner in language, I do not understand very well what this means, can you explain me? I only put the <Optional> because it gives me an error when using Repository.findById(serviceId); and asks to change from Service to Object

  • Take a look here, but could be a problem elsewhere in the code

2 answers

0

To access the elements within an OPTIONAL is different from a LIST. So that you have access to the OPTIONAL elements try in the next line to add the . get().

modelAndView.addObject("service", service);

And it’ll stay that way

modelAndView.addObject("service", service.get());

-1

Have you considered passing th-Object="${service}" as model and th:field="*{id}" to pass id?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.