Issue
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 class="container text-center">
<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 class="m-3">
<div class="form-group row">
<label class="col-form-label col-4">Item Name</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{itemName}" required
minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Item Description</label>
<div class="col-8">
<input type="text" class="form-control"
th:field="*{itemDescription}" required
minlength="6" maxlength="10" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Quantity Of Item</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{quantityOfItem}" required
minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">General Item Location</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{itemLocation}"
required minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">E-mail for contact</label>
<div class="col-8">
<input type="email" class="form-control" th:field="*{email}"
required />
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">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 + "]";
}
}
Solution
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) {
...
...
...
}
Answered By - Hacı Celal Aygar
Answer Checked By - Robin (JavaFixing Admin)