gpt4 book ai didi

java - 错误 404 与 :field - thymeleaf

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:21 24 4
gpt4 key购买 nike

我是 thymeleaf 的初学者,无法正确输入。

标签 th:field 在页面 html 中不会自动完成。

<form method="post" th:object="${livro}">

<label>Nome: </label>
<input type="text" id="nome" th:field="*{nome}" />
<br>

<label for="anotacao">anotacao </label>
<textarea rows="3" cols="30" id="anotacao" th:field="*{anotacao}"></textarea>
<br>

<input type="submit" value="Salvar">

</form>

我的 Controller 很简单。

@RequestMapping(value="/novo", method = RequestMethod.POST)
public String salvar( @Valid Livro livro, BindingResult result,Model model, RedirectAttributes attributes){

if(result.hasErrors()){
model.addAttribute("mensagem", "Erro no formulario");
return "/Livro";
}

livroService.salvar(livro);
attributes.addFlashAttribute("mensagem", "Livro salvo com sucesso!!!");
return "redirect:/Livros/novo";
}

我的模型:

@Entity
public class Livro {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotBlank
private String nome;
@Size(min = 1, max = 60, message = "minimo de 1 e maximo de 60 caracteres")
private String anotacao;

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>br.com.admescola.escola</groupId>
<artifactId>admescola</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>admescola</name>
<description>Administração de escola</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!-- Conector MySQL -->

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<!-- DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<!-- Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test do String -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.5.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
</dependency>

<!-- thymeleaf-spring4 -->

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.5.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
</dependency>



<!-- Thymeleaf - Layout Dialect -->
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.2.1</version>
</dependency>
[</d][1]ependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

最佳答案

欢迎来到SO。

您可能需要包含 th:action在您的form上使其正确发布的标签,例如:<form method="post" th:action="@{/novo}" th:object="${livro}">或者无论它是什么。

另外,请注意您的返回声明:

@PostMapping("/novo")  //can use shortened form
public String salvar(@Valid Livro livro,
BindingResult result,
Model model,
RedirectAttributes attributes) {

if (result.hasErrors()) {
model.addAttribute("mensagem", "Erro no formulario");
return "Livro"; //you may want to remove the slash. This will look for something like Livro.jsp or Livro.html.
}

livroService.salvar(livro);

attributes.addFlashAttribute("mensagem", "Livro salvo com sucesso!!!");
return "redirect:/Livros/novo";
}

关于java - 错误 404 与 :field - thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748005/

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