如何访问对象 Thymeleaf 的 属性(Spring 启动)
How can I access a property of my object Thymeleaf (Spring Boot)
我网站上的当前输出...
这是 --- Optional[User(id=111, username=Juan Lopez, password=Juanini123, post=Hoy es un gran dia)]
的个人页面
所需的输出只是显示姓名“Juan Lopez”
我的 HTML (Thymleaf)...
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Personal Profile</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="positionlist" th:unless="${#lists.isEmpty(personalUser)}">
<span>This is the personal page of --- </span>
<span th:text="${personalUser}"></span>
</div>
</body>
</html>
我的控制器(Spring启动):
package com.littlesocial.sm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Controller
public class UserController {
@NonNull
private final UserRepository userRepository;
@GetMapping("/myProfile")
public String getPersonalUserProfile(Model model){
userRepository.save(
new User(111L,"Juan Lopez", "Juanini123", "Hoy es un gran dia"));
model.addAttribute("personalUser", userRepository.findById(111L));
return "personalUserProfile";
}
}
我已经尝试过 personalUser.username - 但它不起作用。
请制作:
<span th:text="${personalUser.get().username}"></span>
或者:
Optional<User> foundInDb = userRepository.findById(111L);
if (foundInDb.present()) {
model.addAttribute("personalUserName", // e.g.
foundInDb.get().getUserName()
);
}
根据:
th:text="personalUserName"
非常感谢:
我网站上的当前输出...
这是 --- Optional[User(id=111, username=Juan Lopez, password=Juanini123, post=Hoy es un gran dia)]
的个人页面所需的输出只是显示姓名“Juan Lopez”
我的 HTML (Thymleaf)...
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Personal Profile</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="positionlist" th:unless="${#lists.isEmpty(personalUser)}">
<span>This is the personal page of --- </span>
<span th:text="${personalUser}"></span>
</div>
</body>
</html>
我的控制器(Spring启动):
package com.littlesocial.sm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Controller
public class UserController {
@NonNull
private final UserRepository userRepository;
@GetMapping("/myProfile")
public String getPersonalUserProfile(Model model){
userRepository.save(
new User(111L,"Juan Lopez", "Juanini123", "Hoy es un gran dia"));
model.addAttribute("personalUser", userRepository.findById(111L));
return "personalUserProfile";
}
}
我已经尝试过 personalUser.username - 但它不起作用。
请制作:
<span th:text="${personalUser.get().username}"></span>
或者:
Optional<User> foundInDb = userRepository.findById(111L);
if (foundInDb.present()) {
model.addAttribute("personalUserName", // e.g.
foundInDb.get().getUserName()
);
}
根据:
th:text="personalUserName"
非常感谢: