gpt4 book ai didi

java - 在类型为 'name' 的对象上找不到属性或字段 'java.util.Optional' - 可能不是公开的或无效的?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:36 26 4
gpt4 key购买 nike

我想在单击房间名称时显示房间详细信息。但是我有一个问题,我不知道为什么。我使用 Spring MVC、Spring Boot、Spring Data 和 Thymeleaf

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'name' cannot be found on object of type 'java.util.Optional' - maybe not public or not valid?

当我使用 Spring Data findById() 时,我认为问题出在服务中的 Optional这是我的代码

房间模型

@Entity
@Table(name ="room")
public class Room implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_room", nullable = false)
private String id;


@Column(name = "name_room", nullable = false)
private String name;


@Column(name = "Description")
private String describe;

@Column(name = "ID_status")
private String status;

@Column(name = "room_image")
private String image;

public Room() {
super();
}

public Room(String id, String name, String describe, String status,String image) {
super();
this.id = id;
this.name = name;
this.describe = describe;
this.status = status;
this.image = image;

}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescribe() {
return describe;
}

public void setDescribe(String describe) {
this.describe = describe;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}


}

客房服务

public interface RoomService {


Optional<Room> findOne(String id);


}

客房服务工具

public class RoomServiceImpl implements RoomService {
@Autowired
private RoomRepository roomRepository;

@Override
public Optional<Room> findOne(String id) {

return roomRepository.findById(id);
}

}

ROM Controller

@GetMapping("/room/{id}/detail")
public String detail(@PathVariable String id, Model model) {
model.addAttribute("room", romService.findOne(id));
return "roomDetail";
}

roomDetail.html

<div class="col-md-7 single-top-in">
<div class="single-para">
<h4><tr th:text="${room.name}"></h4>
<div class="para-grid">
<span class="add-to">$32.8</span>
<a href="#" class="hvr-shutter-in-vertical cart-to">Add to Cart</a>
<div class="clearfix"></div>
</div>
<h5>100 items in stock</h5>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>

最佳答案

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'name' cannot be found on object of type 'java.util.Optional' - maybe not public or not valid?

意味着 Spring 没有设法插入 name模板中使用的属性:

  <h4><tr th:text="${room.name}"></h4>

这是意料之中的,因为你通过了Optional<Room>反对 MVC 模型而不是传递 Room对象。
您必须打开 Optional 中包含的对象在将其添加到模型之前。
例如:

romService.findOne(id).ifPresent(o -> model.addAttribute("room", o));

关于java - 在类型为 'name' 的对象上找不到属性或字段 'java.util.Optional' - 可能不是公开的或无效的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com