I am creating my first form with Java developing with Sprint Tool Suite on my local machine. I created an index.html page with a simple form with 3 input fields. I want to have the form submit to a results.html page which just displays the values entered into the form. The issue I am having is that when I launch the application, fill in some data, and click the submit button, I get a 404 error. Also, what I noticed is that when I click on the submit button, the url box of the browser changes to localhost:8080/person. I expected to see localhost:8080/results. I thank you in advance for any assistance.
My main application class code is this:
package com.baconbuddy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BaconBuddyApplication {
public static void main(String[] args) {
SpringApplication.run(BaconBuddyApplication.class, args);
}
}
My index.html has the following code:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Hello world!</h1>
<form action="#" th:action="@{/person}" th:object="${person}" method="post" >
<input th:field="*{firstName}" placeholder="First Name" />
<input th:field="*{lastName}" placeholder="Last Name" />
<input th:field="*{age}" placeholder="Age" />
<button >Submit</button>
</form>
</body>
</html>
My results.html has the following code:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<p>First Name:
<span th:text="${person.firstName}"></span>
</p>
<br />
<p>Last Name:
<span th:text="${person.lastName}"></span>
</p>
<br />
<p>Age:
<span th:text="${person.age}"></span>
</p>
</div>
</body>
</html>
My Person.java file looks like this:
package com.baconbuddy.models;
public class Person {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
and my HomeController.java looks like this:
package com.baconbuddy.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baconbuddy.models.Person;
@Controller
@RequestMapping({ "/", "/home" })
public class HomeController {
@GetMapping()
public String index(Model model) {
model.addAttribute("person", new Person());
// The string index will be looked for in src/main/resources/templates
return "index";
}
@PostMapping()
public String personSubmit(@ModelAttribute Person person) {
return "results";
}
}