0

So I got this simple code I'd like to test but somehow it doesn't return the dataBaseEntity.html.

@Controller
public class DataBaseMicroserviceRestConnectorProvider {

    @Autowired
    private DataBaseSpringDataConnectorRequester dataBaseSpringDataConnectorRequester;

    @RequestMapping(value = "/add", method = RequestMethod.GET)                                 
    public String addNewDataBaseEntityForm(Model model) {
        model.addAttribute("dataBaseEntity", new DataBaseEntity());
        return "dataBaseEntity";
    }

The html file is allocated in src/main/resources/templates/dataBaseEntity.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/dataBaseEntity}" th:object="${dataBaseEntity}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Name: <input type="text" th:field="*{name}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

pom.xml

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

And I got a second question: is there a way to do this with @RestController instead of @Controller?

elp
  • 840
  • 3
  • 12
  • 36

2 Answers2

0

handle form submission in spring boot

go to this link, it will help to solve your problem.

there can be many things you may be missing in your example,

like :- I can't see your dataBaseEntity request in your controller , may be there you are missing something.

@RestController

we usually use for our rest end point not for jsp request. hope by this link you will get your solution.

Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
0

You may have a wrong configuration, please make sure you have set the right suffix and prefix for your templates:

@Bean
public SpringResourceTemplateResolver templateResolver(){
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(true);
    return templateResolver;
}

If you don't have a config right now, this might be your problem, please refer to: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

For your second question about the @RestController annotation. This is basically the same as writing: @Controller and @ResponseBody

See: Difference between spring @Controller and @RestController annotation