【问题标题】:Thymeleaf iterator th:each does not view informationThymeleaf迭代器th:each不查看信息
【发布时间】:2023-01-02 22:40:54
【问题描述】:

所以,我写了 spring boot Controller,写了 Entity -> Course,写了 html views-> courses,但是我的信息没有在我的模板中查看。

@Controller
@RequestMapping ("/courses")
public class CourseController {
    
    private CourseService courseService;
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }
    
    
    @GetMapping ("/index")
    public String getCourses (Model model, @RequestParam (name="keyword", defaultValue = "") String keyword ) {
        List <Course> courses = courseService.findCourseBycourseName(keyword);
        model.addAttribute("listCourses", courses);
        model.addAttribute("keyword", keyword);
        return "views/courses";
        
    }

HTML + Thymeleaf courses.html -> 当我启动应用程序时只加载空表。

<tbody> 
<tr class ="text-center" th:each="course : ${listCourses}" > 
  <td th:text = "${course.getCourseId()}"> </td>
  <td th:text = "${course.getCourseName()}"> </td>
  <td th:text = "${course.getCourseDuration()}"> </td>
  <td th:text = "${course.getCourseDescription()}"> </td>
  <td th:text = "${course.getInstructor().getInstructorFirstName()} + '' + course.getInstructor().getInstructorLastName"> </td>
  <td th:text = "${course.getListStudent().size()}"> </td>

</tbody>
</table>

谁知道哪里有问题

【问题讨论】:

  • courseService.findCourseBycourseName() 方法有返回值吗?如果 courses 列表有项目,也许用调试器检查一下?

标签: spring-boot thymeleaf


【解决方案1】:

您需要在 HTML sn-p 中关闭标签。

<tbody> 
<tr class ="text-center" th:each="course : ${listCourses}" > 
  <td th:text = "${course.getCourseId()}"> </td>
  <td th:text = "${course.getCourseName()}"> </td>
  <td th:text = "${course.getCourseDuration()}"> </td>
  <td th:text = "${course.getCourseDescription()}"> </td>
  <td th:text = "${course.getInstructor().getInstructorFirstName()} + '' + course.getInstructor().getInstructorLastName"> </td>
  <td th:text = "${course.getListStudent().size()}"> </td>
</tr><!-- bingo -->
</tbody>
</table>

另请参阅How to print Array size in thymeleaf?,因为这可能是您的下一个问题。

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2018-05-09
    • 2018-07-10
    • 2018-05-11
    • 2023-03-31
    • 2015-06-17
    • 2020-10-09
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多