Error 404 trying to access html page with RequestMapping

I have a simple Springboot application with thymeleaf. I created a formPaciente.html under src/main/resources/templates/pacientes , and a controller to map the URL, but I get error 404 while trying to access it, I did the same thing for index.html and it works.

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

Controller

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";
    }
}

Upvotes: 0

Views: 222

Answers (3)

sujoydutta
sujoydutta

Reputation: 1

Place PacienteController.java to ./main/java/com/Laudosapp/laudosapp: or Add @ComponentScan("Controllers") to Spring main application file.

Upvotes: 0

GoutamS
GoutamS

Reputation: 3763

It seems that you are you have placed your controller different sub package than spring SpringApplication file. So Controller is not accessible from Spring main()

Please add

@SpringBootApplication @ComponentScan("Controllers")

Or Place PacienteController.java to ./main/java/com/Laudosapp/laudosapp:

And for HTML tag error add to application.properties

spring.thymeleaf.mode=LEGACYHTML5

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

Upvotes: 1

Amr Alaa
Amr Alaa

Reputation: 553

It seems that you are sending an object from the .htm page but not receiving it in the backend controller. So have a look on this link

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

Upvotes: 1

Related Questions