Form being rendered with wrong action attribute

Asked

Viewed 195 times

0

I am using spring form tags to render the forms present in my views, but I am facing problems because they are being generated with the wrong action attribute.

In my controller, I have the method:

@RequestMapping(value="altera/{theString}")
@PreAuthorize("hasPermission(#user, 'altera_'+#this.this.name)")
public ModelAndView altera(@PathVariable String theString) {
    int id = Integer.valueOf(theString).intValue();
    return new ModelAndView("privado/"+this.getName()+"/altera", "command", serv.listagem(id));
}

that will direct the application to this view:

private/<nome_da_classe>/altera.jsp

<jsp:include page="../../common/altera.jsp">
    <jsp:param name="action" value="/Usuario/altera"/>

    <jsp:param name="elements" value="login"/>
    <jsp:param name="elements" value="senha"/>
    <jsp:param name="elements" value="first_name"/>
    <jsp:param name="elements" value="last_name"/>
    <jsp:param name="elements" value="email"/>

    <jsp:param name="label" value="Login"/>
    <jsp:param name="label" value="Senha"/>
    <jsp:param name="label" value="Nome"/>
    <jsp:param name="label" value="Sobrenome"/>
    <jsp:param name="label" value="E-mail"/>

    <jsp:param name="type" value="text"/>
    <jsp:param name="type" value="password"/>
    <jsp:param name="type" value="text"/>
    <jsp:param name="type" value="text"/>
    <jsp:param name="type" value="email"/>
</jsp:include>

common/altera.jsp

<%@ include file="../include/header_interno.jsp" %>

<form:form class="form" role="form" method="post">

    <table>
        <c:forEach var="item" items="${paramValues.elements}" varStatus="status">
        <tr>
            <td><form:label path="${item}">${paramValues.label[status.index]}</form:label></td>
            <td><form:input path="${item}" type="${paramValues.type[status.index]}" /></td> 
        </tr>
        </c:forEach>
        <tr>
            <td colspan="2">
                <button type="submit" formaction="${action}" class="btn btn-lg btn-primary">Alterar</button>
            </td>
        </tr>
    </table>

</form:form>

<%@ include file="../include/result.jsp" %>

<%@ include file="../include/footer_interno.jsp" %>

that should direct the data entered in the fields to this other controller method:

@RequestMapping(value="altera", method=RequestMethod.POST)
@ResponseBody
public String altera(@ModelAttribute("object") E object, BindingResult result) {
    if(serv.altera(object))
        return "yes";
    else
        return "not";
}

but html is being rendered for this:

<html>
<head>
<title></title>

<body>


<form id="command" role="form" class="form" action="/blog/Usuario/altera/2" method="post">

    <table>

        <tr>
            <td><label for="login">Login</label></td>
            <td><input id="login" name="login" type="text" value="teste"/></td> 
        </tr>

        <tr>
            <td><label for="senha">Senha</label></td>
            <td><input id="senha" name="senha" type="password" value="123"/></td> 
        </tr>

        <tr>
            <td><label for="first_name">Nome</label></td>
            <td><input id="first_name" name="first_name" type="text" value="Usuario"/></td> 
        </tr>

        <tr>
            <td><label for="last_name">Sobrenome</label></td>
            <td><input id="last_name" name="last_name" type="text" value="Primeiro"/></td> 
        </tr>

        <tr>
            <td><label for="email">E-mail</label></td>
            <td><input id="email" name="email" type="email" value="[email protected]"/></td> 
        </tr>

        <tr>
            <td colspan="2">
                <button type="submit" formaction="" class="btn btn-lg btn-primary">Alterar</button>
            </td>
        </tr>
    </table>

</form>


<div id="yes" class="alert alert-success" style="display: none;">
  <strong>Pronto!</strong> Solicita&ccedil;&atilde;o efetuada com sucesso.
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
</div>

<div id="not" class="alert alert-danger" style="display: none;">
  <strong>Erro!</strong> N&atilde;o foi possivel efetivar a sua solicita&ccedil;&atilde;o.
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
</div>



</body>
</html>

where the action attribute is with the value /blog/Usuario/altera/2 when it should be just /blog/Usuario/altera.

someone knows how I can fix this?

1 answer

0


Kleber,

Your problem is probably in the tag that generates the form, you have already tried to add the "action" parameter and specify to "change" ?

If you do not specify the action in the form, it uses a default value for the method in the named controller, in which case as the command comes from "alters/2" it uses it.

Hug,

  • I managed to solve here: it was to use action=${param.altera} and I had used action=${altera}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.