错误404尝试使用RequestMapping访问html页面

时间:2017-11-29 13:26:28

标签: spring spring-boot model-view-controller

我有一个带百里香的简单Springboot应用程序。我在src / main / resources / templates / pacientes下创建了一个formPaciente.html,并在一个控制器上映射了URL,但是在尝试访问它时遇到了错误404,我为index.html做了同样的事情并且它有效。< / p>

项目ls -R

ls -R
.:
main  test

./main:
java  resources

./main/java:
com  Controllers  models

./main/java/com:
Laudosapp

./main/java/com/Laudosapp:
laudosapp

./main/java/com/Laudosapp/laudosapp:
indexController.java  LaudosappApplication.java

./main/java/Controllers:
PacienteController.java

./main/java/models:
Paciente.java

./main/resources:
application.properties  static  templates

./main/resources/static:

./main/resources/templates:
index.html  pacientes

./main/resources/templates/pacientes:
formPaciente.html

./test:
java

./test/java:
com

./test/java/com:
Laudosapp

./test/java/com/Laudosapp:
laudosapp

./test/java/com/Laudosapp/laudosapp:
LaudosappApplicationTests.java

formPaciente.html

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1> Cadastro de pacientes</h1>
<form method="post">
    <input type=""text" value="" name="pacientenome"/>
    <input type=""text" value="" name="pacientesexo"/>
    <input type=""text" value="" name="pacientedatanasc"/>
    <input type=""text" value="" name="pacientedataexame"/>
    <input type=""text" value="" name="medicosolicitante"/>
    <input type=""text" value="" name="specmedica"/>
    <input type=""text" value="" name="hda"/>
    <input type=""text" value="" name="pacienteconvenio"/>

    <button type="submit">Salvar</button>
</form>
</body>
</html>

控制器

package Controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PacienteController {

    @RequestMapping("/cadastrarpaciente")
    public String form(){

        return "pacientes/formPaciente";
    }
}

3 个答案:

答案 0 :(得分:1)

您似乎从.htm页面发送了一个对象,但未在后端控制器中接收它。所以看一下这个链接

http://www.baeldung.com/spring-requestmapping

答案 1 :(得分:1)

您似乎已经将控制器与Spring SpringApplication文件放在不同的子包中。因此无法从 Spring main()

访问Controller

请添加

@SpringBootApplication @ComponentScan("Controllers")

或者 将 PacienteController.java 放到 ./ main / java / com / Laudosapp / laudosapp:

对于HTML标记错误添加到application.properties

  

spring.thymeleaf.mode = LEGACYHTML5

的pom.xml
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> </dependency>

答案 2 :(得分:0)

将PacienteController.java放置到./main/java/com/Laudosapp/laudosapp:或将@ComponentScan(&#34; Controllers&#34;)添加到Spring主应用程序文件。