I am new to spring boot. I have get and post methods in my controller class.
But when I hide the get method I think the post method should run without an error until i hit submit. I have thyme leaf and spring-web dependencies.
when i use both post and get method it works fine.
singer.html page:
<code>ENTER SINGER NAME
Enter Singer name
Enter Singer name
Enter Singer name
</code>
ENTER SINGER NAME
Enter Singer name
Enter Singer name
Enter Singer name
Songs.html page:
<code>CHALEYA
HERIYE HERIYE
MUSKURANE KE WAJAH
PHIR BHI TUMKO CHAHUNGA
</code>
CHALEYA
HERIYE HERIYE
MUSKURANE KE WAJAH
PHIR BHI TUMKO CHAHUNGA
But when i comment out the get mapping method in the controller class I get an error:
Error
when I reload my localhost:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Sep 12 21:32:45 IST 2024
There was an unexpected error (type=Method Not Allowed, status=405).
I get the following error in console:
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ is not supported]
Controller class: (GetMapping method commented out)
<code>package com.aman.music.Controller;
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 com.aman.music.Singers;
public class MyController {
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
// public String hii(Model model) {
// model.addAttribute("this",new Singers());
<code>package com.aman.music.Controller;
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 com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
// @GetMapping("/")
// public String hii(Model model) {
// model.addAttribute("this",new Singers());
// return "singers";
// }
}
</code>
package com.aman.music.Controller;
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 com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
// @GetMapping("/")
// public String hii(Model model) {
// model.addAttribute("this",new Singers());
// return "singers";
// }
}
These are my files:
MusicPlayerApplication.java
<code>package com.aman.music;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class MusicPlayerApplication {
public static void main(String[] args) {
SpringApplication.run(MusicPlayerApplication.class, args);
<code>package com.aman.music;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MusicPlayerApplication {
public static void main(String[] args) {
SpringApplication.run(MusicPlayerApplication.class, args);
}
}
</code>
package com.aman.music;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MusicPlayerApplication {
public static void main(String[] args) {
SpringApplication.run(MusicPlayerApplication.class, args);
}
}
Controller.java
<code>package com.aman.music.Controller;
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 com.aman.music.Singers;
public class MyController {
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
public String hii(Model model) {
model.addAttribute("this",new Singers());
<code>package com.aman.music.Controller;
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 com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
@GetMapping("/")
public String hii(Model model) {
model.addAttribute("this",new Singers());
return "singers";
}
}
</code>
package com.aman.music.Controller;
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 com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
@GetMapping("/")
public String hii(Model model) {
model.addAttribute("this",new Singers());
return "singers";
}
}
Singers.java
<code>package com.aman.music;
private String ArjitSingh;
public String getHello() {
public void setHello(String hello) {
public String getArjitSingh() {
public void setArjitSingh(String arjitSingh) {
public String getSinger() {
public void setSinger(String singer) {
public String toString() {
return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
<code>package com.aman.music;
public class Singers {
private String ArjitSingh;
private String Singer;
private String hello;
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public String getArjitSingh() {
return ArjitSingh;
}
public void setArjitSingh(String arjitSingh) {
ArjitSingh = arjitSingh;
}
public String getSinger() {
return Singer;
}
public void setSinger(String singer) {
Singer = singer;
}
@Override
public String toString() {
return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
}
}
</code>
package com.aman.music;
public class Singers {
private String ArjitSingh;
private String Singer;
private String hello;
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public String getArjitSingh() {
return ArjitSingh;
}
public void setArjitSingh(String arjitSingh) {
ArjitSingh = arjitSingh;
}
public String getSinger() {
return Singer;
}
public void setSinger(String singer) {
Singer = singer;
}
@Override
public String toString() {
return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
}
}
Songs.html
<html xmlns:th="https://www.thymeleaf.org">
<title>Songs List</title>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
<code><!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Songs List</title>
</head>
<body>
<p>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
</body>
</html>
</code>
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Songs List</title>
</head>
<body>
<p>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
</body>
</html>
singers.html
<html xmlns:th="https://www.thymeleaf.org">
<meta charset="UTF-8" http-equiv="Content-Type">
<h1>ENTER SINGER NAME</h1>
<form action="#" th:action="@{/}" th:object="${this}" method="post">
<p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
<p>Enter Singer name<input type="text" th:field="*{Singer}"/>
<p>Enter Singer name<input type="text" th:field="*{hello}"/>
<p><input type="submit" value="SUBMIT"/>
<code><!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8" http-equiv="Content-Type">
<title>Singer</title>
</head>
<body>
<h1>ENTER SINGER NAME</h1>
<form action="#" th:action="@{/}" th:object="${this}" method="post">
<p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
<p>Enter Singer name<input type="text" th:field="*{Singer}"/>
<p>Enter Singer name<input type="text" th:field="*{hello}"/>
<p><input type="submit" value="SUBMIT"/>
</form>`
</body>
</html>
</code>
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8" http-equiv="Content-Type">
<title>Singer</title>
</head>
<body>
<h1>ENTER SINGER NAME</h1>
<form action="#" th:action="@{/}" th:object="${this}" method="post">
<p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
<p>Enter Singer name<input type="text" th:field="*{Singer}"/>
<p>Enter Singer name<input type="text" th:field="*{hello}"/>
<p><input type="submit" value="SUBMIT"/>
</form>`
</body>
</html>
Kindly ignore the variables and file names i am using