错误404与th:field - thymeleaf

时间:2017-05-02 22:07:18

标签: java maven thymeleaf

我是百里香的初学者,我无法正确输入。

标签th:字段在页面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>

我的控制器很简单。

@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>

1 个答案:

答案 0 :(得分:0)

欢迎来到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"; 
}