My resister page works fine, it takes the input and pushes to data base when "register" is clicked. However, when I click to go to a "list an item" page, which similarly takes input to be pushed to a table in a database when "list item" is clicked, it comes up with "Request Method 'POST' not supported". So the "list an item" page doesn't appear when clicked from the main logged-in menu page.
Part of the controller class here:
@GetMapping("/register")
public String goToRegisterPage(Model model) {
User user = new User();
model.addAttribute("user", user);
return "register_page";
}
@GetMapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
FreeItem freeItem = new FreeItem();
model.addAttribute("freeItem", freeItem);
return "create_item_page";
}
Part of the html file here that offers to go to the create_item_page:
<div>
<form th:action="@{/update_user_page}" method="post">
<input type="submit" value="Update account" />
</form>
</div>
<div>
<form th:action="@{/go_to_create_item_page}" method="post">
<input type="submit" value="List an Item" />
</form>
</div>
The create_item_page:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Create Item</title>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div >
<div>
<h1>Create an Item to list publicly</h1>
</div>
<form th:action="@{/process_created_item}" method="post"
style="max-width: 600px; margin: 0 auto;"
th:object="${freeItem}">
<div >
<div >
<label >Item Name</label>
<div >
<input type="text" th:field="*{itemName}" required
minlength="2" maxlength="20" />
</div>
</div>
<div >
<label >Item Description</label>
<div >
<input type="text"
th:field="*{itemDescription}" required
minlength="6" maxlength="10" />
</div>
</div>
<div >
<label >Quantity Of Item</label>
<div >
<input type="text" th:field="*{quantityOfItem}" required
minlength="2" maxlength="20" />
</div>
</div>
<div >
<label >General Item Location</label>
<div >
<input type="text" th:field="*{itemLocation}"
required minlength="2" maxlength="20" />
</div>
</div>
<div >
<label >E-mail for contact</label>
<div >
<input type="email" th:field="*{email}"
required />
</div>
</div>
<div>
<button type="submit" >List Item</button>
</div>
</div>
</form>
</div>
</body>
</html>
And the FreeItem class that specifies the objects being pushed to the database.
package com.marinelli.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "free_items")
public class FreeItem {
public FreeItem(String itemName, String itemDescription, String quantityOfItem, String itemLocation, String email) {
super();
this.itemName = itemName;
this.itemDescription = itemDescription;
this.quantityOfItem = quantityOfItem;
this.itemLocation = itemLocation;
this.email = email;
}
public FreeItem() {
}
@Id
@Column(nullable = false, unique = true, length = 64)
private String itemName;
@Column(nullable = false, length = 64)
private String itemDescription;
@Column(nullable = false, length = 25)
private String quantityOfItem;
@Column(nullable = false, length = 25)
private String itemLocation;
@Column(nullable = false, unique = true, length = 45)
private String email;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getQuantityOfItem() {
return quantityOfItem;
}
public void setQuantityOfItem(String quantityOfItem) {
this.quantityOfItem = quantityOfItem;
}
public String getItemLocation() {
return itemLocation;
}
public void setItemLocation(String itemLocation) {
this.itemLocation = itemLocation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "FreeItem [itemName=" itemName ", itemDescription=" itemDescription ", quantityOfItem="
quantityOfItem ", itemLocation=" itemLocation ", email=" email "]";
}
}
CodePudding user response:
The error clearly stated that you are using the incorrect method for that provided endpoint.
In your case, /go_to_create_item_page defined as GET, your form declared method = post. Either changing them to both use POST or GET should resolve the problem (In terms of API design, it should be GET, as you are getting resources from the server rather than creating / updating).
CodePudding user response:
you need to create go_to_create_item_page
and update_user_page
as @Postmapping("....")
@Postmapping("/update_user_page")
public String goToUpdateUserPage(Model model) {
...
...
...
}
@Postmapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
...
...
...
}