没有GET /索引SpringBoot Thymeleaf的Map

rryofs0p  于 2023-03-11  发布在  Spring
关注(0)|答案(1)|浏览(132)

我正在用Thymeleaf创建Sping Boot 注册表应用程序。当我运行应用程序时,我遇到了一个问题,我在下面附上了堆栈跟踪。我在这里有两个问题,一个是Student类与@jakarta.persistence.Id@Id。我定义它看起来像这样,否则会显示错误。

akarta.servlet.ServletException: Circular view path [error]: would dispatch back to the current handler URL [/error] again.

到目前为止我所尝试的附在下面。

学生控制器

@Controller
@RequestMapping("/")
public class StudentController {

    @Autowired
    StudentService studentService;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("title", "Home Page");
        return "index";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveStudent(@ModelAttribute("student") Student std, HttpServletRequest request) {
        studentService.save(std);

        return "redirect:/";
    }

}

实体

@Entity
public class Student {

    @jakarta.persistence.Id
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String course;
    private String image;

    public Student() { }

学生服务部

public interface StudentService {

    public void save(Student std);
}

服务IMPL

@Service
public class SeviceIMPL implements StudentService {

    @Autowired
    private StudentRepository repo;
    @Override
    public void save(Student std) {
        repo.save(std);
    }
}

学生存储库

@Repository
public interface StudentRepository  extends JpaRepository<Student, Long> {

}

索引.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://thymeleaf.org">
<head th:replace="common/header :: header"/>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:replace="common/header :: navbar"/>

<div class="col-sm-4">
    <!--/*@thymesVar id="student" type=""*/-->
    <form action="#" th:action="@{/save}" th:object="${student}" method="post">

        <div alight="left">
            <tr>
                <label class="form-label" >Student Name</label>
                <td>
                    <input type="hidden" th:field="*{id}" />
                    <input type="text" th:field="*{name}" class="form-control" placeholder="Student Name" />
                </td>
            </tr>
        </div>

        <div alight="left">

            <tr>
                <label class="form-label" >Course</label>
                <td><input type="text" th:field="*{course}" class="form-control" placeholder="Course" /></td>
            </tr>
        </div>
        <div alight="left">
            <tr>
                <label class="form-label" >Fee</label>
                <td><input type="text" th:each="image : {page.content}" class="form-control" placeholder="Fee" /></td>
            </tr>
        </div>
        <br>
        <tr>
            <td colspan="2"><button type="submit" class="btn btn-info">Save</button> </td>
        </tr>
        </table>
    </form>
</div>
</body>
</html>
vyu0f0g1

vyu0f0g11#

我在模板中发现了一些错误,这可能是模板不能正确解析的原因。

  • 您有许多错别字alight,这些错别字应该是align
  • th:each="image : {page.content}"肯定是不正确的,也不是一种生成图像表的方法。
<tr th:each="image: ${page.content}">

    <-- First column with the image label-->
    <td th:text="${image.label}"></td>

    <-- Second column with the image itself -->
    <td>
      <img th:src="@{${image.url}}"/>
    </td>
</tr>
  • 最后我真的不确定@Id@jakarta.persistence.Id哪一个是好的组合,你应该根据依赖版本坚持javaxjakarta,但是只要我很久没有使用Thymeleaf,我就不确定Thymeleaf是否与jakarta兼容。

相关问题