java - Spring Boot - POST Request Method did not work but GET did -
i learning spring boot , wrote small application. application has controller:
@controller @requestmapping("/") public class applicationcontroller { @requestmapping(value="/account", method = requestmethod.post) public string getaccountvo(modelmap model) { accountvo vo = new accountvo(); vo.setaccountno("0102356"); vo.setaccountholdername("dinesh"); model.addattribute("acc", vo); return "account"; } } ... , page (view) is:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>account details</title> </head> <body> <form> account number <input type="text" name="acctno" value="${acc.getaccountno()}"><br> account holder name <input type="text" name="name" value="${acc.getaccountholdername()}"><br> </form> </body> </html> when ran application, got http status 405 message request method 'get' not supported. when changed method in @requestmapping annotation method=requestmethod.get got expected page.
why did happen?
@requestmapping(value="/account", method = requestmethod.post) this means getaccountvo method handler responsible post requests on /account endpoint. when fire get request /account endpoint, since haven't define method handler process that, spring complains 405 method not supported.
if intent have form processing workflow, typical approach define 2 method handlers on /account endpoint: 1 displaying form , other processing submitted form, kinda this:
@controller @requestmapping("/") public class applicationcontroller { @requestmapping(value="/account", method = requestmethod.get) public string displayaccountform(...) { // whatever suits requirements return "account"; } @requestmapping(value="/account", method = requestmethod.post) public string handlesubmittedform(...) { // whatever suits requirements return "successpage"; } }
Comments
Post a Comment