1

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:&nbsp;</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

Community
  • 1
  • 1
aceminer
  • 4,089
  • 9
  • 56
  • 104
  • You don't need to override `doPost` if you are using Spring. Just provide an appropriate handler method in your `@Controller`. – Sotirios Delimanolis Aug 04 '14 at 14:52
  • @SotiriosDelimanolis i did actually but its throwing up the error as per the link in my previous question – aceminer Aug 04 '14 at 14:52
  • Please don't post the same question several times in the site. Focus on one and wait for it. If it needs more attention, provide a bounty. – Luiggi Mendoza Aug 04 '14 at 14:54
  • Also, next time use the proper tags for your question. – Luiggi Mendoza Aug 04 '14 at 14:54
  • What is different from the previous question you posted? Did you repost it again because you wanna more attention? – Roman C Aug 04 '14 at 14:55
  • @RomanC In my previous question i didn't have the solution so i asked for it. In this current question i thought i had found a direction hence needing a different question. I was merely using my previous question as a reference – aceminer Aug 04 '14 at 14:59
  • @aceminer Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Roman C Aug 04 '14 at 15:03
  • @RomanC the problem is pretty clear if you read the first question but that wasn't tagged with Java. – Luiggi Mendoza Aug 04 '14 at 15:05
  • @LuiggiMendoza This is duplicate question which was posted as unclear with the obsolete link. You are right about tagging it to java tag, however it is IMO, java isn't related in that question like would say by many SO users. – Roman C Aug 04 '14 at 16:40
  • @LuiggiMendoza What about other languages based on Java technology why not to tag them as Java? – Roman C Aug 04 '14 at 16:50
  • @LuiggiMendoza Why did you start it? – Roman C Aug 04 '14 at 16:52

1 Answers1

1

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:

/Notification/notification
/Account/userManagement
/h <-- this seems ridiculous...
/admin
/hello

And in your form you post to "test":

<form:form action="test" method="POST" modelAttribute = "user-entity">
    <!-- rest of your html code ... -->
</form:form>

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):

/Notification/test
/Account/test
/test <-- this may work as expected
/test <-- this may work as expected
/test <-- this may work as expected

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}.

<form:form action="${request.contextPath}/test" method="POST" modelAttribute = "user-entity">
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I still am experiencing this issue. I have updated my code as follows and tried a few solutions – aceminer Aug 04 '14 at 15:40
  • @aceminer edit your current question and provide the necessary details to replicate your problem again. – Luiggi Mendoza Aug 04 '14 at 15:41
  • @aceminer I can see you edited your `@Controller^ but don't see any changes in your HTML code for the action of your form. Please provide the exact URL you're trying to access with GET/POST request and your mappings. Or, probably you need to stop working with Spring MVC and learn from scratch and go to Servlets. – Luiggi Mendoza Aug 04 '14 at 15:44
  • My bad. i didn't post the whole action up. I have made it clearer this time. – aceminer Aug 04 '14 at 15:49
  • @aceminer what's this `` you provide? What's your exact URL? – Luiggi Mendoza Aug 04 '14 at 15:51
  • my default URL would be localhost:8080// and i would like to access localhost:8080//Account/test – aceminer Aug 04 '14 at 15:54
  • @aceminer no, your default URL will be `http://://`. So, in your mappings, you should only care about the rest of the URL. Your mapping should only be `/Account/test`, no need to provide `/` because the application server will do it. – Luiggi Mendoza Aug 04 '14 at 15:56
  • yes I left out the http. And I meant the application name for the package name – aceminer Aug 04 '14 at 16:00
  • @aceminer nevermind. Remove this `` from your URL mappings. – Luiggi Mendoza Aug 04 '14 at 16:00
  • I have removed as per what you advised and the url returned was http://localhost:8080/Account/test instead of the desired http://localhost:8080/ApplicationName/Account/test – aceminer Aug 04 '14 at 16:15
  • @Luggi Mendoza Nope unfortunately as this is not the URL which it should be mapped to. I am not sure if its somewhere wrong with my annotation configuration or something but i have no issue using GET just only POST is not working – aceminer Aug 04 '14 at 16:19
  • @aceminer use Chrome tools or Firebug in Firefox, open one of these tools in your browser before sending the post request. Then, go to the Network tab. After doing this, fire your POST request and check the URL. Check if you have mapped that URL or if is the wrong URL. Edit your question accordingly to provide more help. – Luiggi Mendoza Aug 04 '14 at 16:22