0

estoy tratando de hacer una redireccion a otra pagina pero al poner el nombre del valor del RequestMapping y levanto el sistema me sale un error de "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>

1 respuesta 1

2

Sin conocer spring-mvc, veo que en tu método helloPage, sobre /hello.do esperas un tipo de request GET:

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

Y en tu form tienes llamada con POST:

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

Entiendo que el error vendrá por ahí y podría ser tan simple como cambiar a

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

Al final, lo que Spring estará buscando es un método/clase que trate una petición sobre /hello.do y que sea de tipo POST, y tal como tienes tu código no existe ninguno.

1
  • tienes razon, no me habia fijado en eso.. lo probe con el POST y si hace la redireccion bien pero no me agarra el usuario correspondiente..
    – Jose Diaz
    el 1 feb. 2017 a las 15:21

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.