Request method 'POST' not supported

0

I'm trying to redirect to another page but by putting the name of the value of the RequestMapping and raising the system I get an error of "Request method 'POST' not supported".

Main Controller

   @RequestMapping(value = "/hello.do", method = RequestMethod.GET)
         public ModelAndView helloPage() {

    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("hello");

    return model;

}

Login.jsp form

       <form name='loginForm'
        action="<c:url value='/hello.do' />" method='POST'>

        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='username'></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='password' /></td>
            </tr>
            <tr>
                <td colspan='2'><input name="submit" type="submit"
                    value="submit" /></td>
            </tr>
        </table>

        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
    </form>

hello.jsp form

<sec:authorize access="hasRole('ROLE_ADMIN')">

    <!-- For login user -->
    <c:url value="/logout" var="logoutUrl" />
    <form action="${logoutUrl}" method="post" id="logoutForm">
        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
    </form>
    <script>
        function formSubmit() {
            document.getElementById("logoutForm").submit();
        }
    </script>

    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <h2>
            User : ${pageContext.request.userPrincipal.name} | <a
                href="javascript:formSubmit()"> Logout</a>
        </h2>
    </c:if><h1>hola admin</h1>

</sec:authorize>

    <sec:authorize access="hasRole('ROLE_USER')">
    <!-- For login user -->
    <c:url value="/logout" var="logoutUrl" />
    <form action="${logoutUrl}" method="post" id="logoutForm">
        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
    </form>
    <script>
        function formSubmit() {
            document.getElementById("logoutForm").submit();
        }
    </script>

    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <h2>
            User : ${pageContext.request.userPrincipal.name} | <a
                href="javascript:formSubmit()"> Logout</a>
        </h2>
    </c:if><h1>hola USUARIO</h1>

</sec:authorize>
    
asked by Jose Diaz 01.02.2017 в 15:13
source

1 answer

2

Without knowing spring-mvc, I see that in your method helloPage , about /hello.do you expect a type of request GET :

@RequestMapping(value = "/hello.do", method = RequestMethod.GET)
         public ModelAndView helloPage() {

And in your form you have a call with POST:

<form name='loginForm'
        action="<c:url value='/hello.do' />" method='POST'>

I understand that the error will come around and it could be as simple as changing to

@RequestMapping(value = "/hello.do", method = RequestMethod.POST)
             public ModelAndView helloPage() {

In the end, what Spring will be looking for is a method / class that deals with a request on /hello.do and that is of the POST type, and as you have your code, none exists.

    
answered by 01.02.2017 в 15:29