i am relatively new to spring mvc and have been exploring some form submit. However, currently, i have an error of HTTP 405 which would mean that i am unable to post.
The error is that the HTTP post is not supported. I have googled and checked that i would need to override and implement doPost method in my code but i am unsure of how to use the servlet.
By overriding the doPost method, how do I ensure that the new doPost method is applied?
This is my controller class:
package com.**.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.**.dao.*;
import com.**.model.User;
@Controller
public class MainController {
@Autowired
private UserDAO UserDAO;
@RequestMapping(value = {"/hello", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView defaultPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This is default page!");
model.setViewName("hello");
return model;
}
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This page is for ROLE_ADMIN only!");
model.setViewName("admin");
return model;
}
@RequestMapping(value = {"/h", "/login"}, method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
//for 403 access denied page
@RequestMapping(value = "/402", method = RequestMethod.GET)
public ModelAndView accesssDenied() {
ModelAndView model = new ModelAndView();
//check if user is login
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
System.out.println(userDetail);
model.addObject("username", userDetail.getUsername());
}
model.setViewName("402");
return model;
}
@RequestMapping(value = "/Account/userManagement", method = RequestMethod.GET)
public ModelAndView accountPage() {
ModelAndView model = new ModelAndView();
model.setViewName("Account/userManagement");
return model;
}
@RequestMapping(value = "Notification/notification", method = RequestMethod.GET)
public ModelAndView NotificationPage() {
ModelAndView model = new ModelAndView();
model.setViewName("Notification/notification");
return model;
}
@RequestMapping(value = "test", method = RequestMethod.POST)
public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
{
ModelAndView model = new ModelAndView();
UserDAO.create(user);
model.setViewName("hello");
return model;
}
@RequestMapping(value = {"/","/Account/registration"}, method = RequestMethod.GET)
public ModelAndView registerPage()
{
ModelAndView model = new ModelAndView("/Account/registration", "user-entity", new User());
return model;
}
}
this is my form code
<form:form action="test" method="POST" modelAttribute = "user-entity">
<td> <td><form:label path="Username">Name:</form:label></td>
<td>
<form:input path="Username"></form:input>
</td>
</tr>
<tr>
<td>Password: </td>
<td><form:input type = "password" path = "Password" ></form:input></td>
</tr>
</form:form>
Exception
Aug 03, 2014 6:12:53 PM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported WARNING: Request method 'POST' not supported
This is a link to my previous question
POST not working in spring mvc 4
I modified my form action to
<form:form action="/<packagename>/Account/test" method="POST" modelAttribute = "user-entity">
the default url of the registration page is
http://localhost:8080/<package name>/
and i updated my corresponding controller code to
@RequestMapping(value = "/<package name>/Account/test", method = RequestMethod.POST)
public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
{
ModelAndView model = new ModelAndView();
UserDAO.create(user);
model.setViewName("/Account/test");
return model;
}
The url i am trying to get to is /Account/test
The problem is that your URL doesn't support a POST method. By looking at all your GET requests, they all start with a relative path and then the real URL, for example:
And in your form you post to "test":
Which means that any post will go to
/<whatever_goes_here>/test
i.e. (since you don't specify which one is your current view):And you don't have any of the first two mappings.
Solution: fix your URLs or go to the real URL by using
HttpServletRequest#getContextPath
. Note: avoid usage of scriptlets, instead use the Expression Language in your JSP:${request.contextPath}
.