java - Spring MVC Form - Request method 'GET' not supported -


i trying learn spring mvc don't know how solve problem.

why "request method 'get' not supported" when got url "http://localhost:8080/springtest3/addstudent.html"?

studentcontroller.java:

package com.springtest3;  import javax.validation.valid;  import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.sessionattributes; import org.springframework.web.servlet.modelandview;  @controller @sessionattributes public class studentcontroller {     @requestmapping(value = "/students", method = requestmethod.get)    public modelandview showstudent() {       return new modelandview("student", "command", new student());    }     @requestmapping(value = "/addstudent", method = requestmethod.post)    public string addstudent(@modelattribute("student") student student, bindingresult result/*, final modelmap model*/) {        if (result.haserrors()) {            system.out.println("error error error");        }         /*model.addattribute("name", student.getname());        model.addattribute("age", student.getage());        model.addattribute("id", student.getid());*/         system.out.println("name: " + student.getname());        return "redirect:students.html";    } } 

addstudent.jsp:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head>     <title>spring mvc form handling</title> </head> <body>  <h2>student information</h2> <form:form method="post" action="addstudent.html">    <table>     <tr>         <td><form:label path="name">name</form:label></td>         <td><form:input path="name" /></td>     </tr>     <tr>         <td><form:label path="age">age</form:label></td>         <td><form:input path="age" /></td>     </tr>     <tr>         <td><form:label path="id">id</form:label></td>         <td><form:input path="id" /></td>     </tr>     <tr>         <td colspan="2">             <input type="submit" value="submit"/>         </td>     </tr> </table>   </form:form> </body> </html> 


solution:
suggested below added method in studentcontroller.java:

   @requestmapping(value = "/addstudent", method = requestmethod.get)    public void test(model model) {        model.addattribute("student", new student());           } 

but need change following line in addstudent.jsp:

<form:form method="post" action="addstudent.html"> 

to

<form:form method="post" modelattribute="student" action="addstudent.html"> 

this because when put http://localhost:8080/springtest3/addstudent.html in url, request initiated. however, addstudent method in controller has

@requestmapping(value = "/addstudent", method = requestmethod.post) 

which filters allow post requests access method.

in order allow requests, change mapping addstudent method with

@requestmapping(value = "/addstudent", method = requestmethod.get)  

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -