Validation of input triggering 400 error with Spring MVC

Asked

Viewed 422 times

0

Guys, there’s a boring mistake that I’m losing sleep over to figure out how to treat.

Well, every time I perform my test by checking the validation of my field an error 400 and fired.

I don’t know but how do I resolve this issue. The server log shows nothing.

below the HTML:

<div class="body-nest" id="basic">
    <div class="form_center">


        <c:if test="${validator}">
            <div class="alert alert-danger">
                <button data-dismiss="alert" class="close" type="button">×</button>
                <span class="entypo-attention"></span> <strong>Opa!</strong>&nbsp;&nbsp;Você
                não pode deixar o campo abaixo em branco e ele tem que ter mais
                que três caracters!.
            </div>
        </c:if>

        <f:form action="updateCategory" method="get" modelAttribute="categoryModify">
        <input type="hidden" name="id" value="${categoryModify.idCategory}"/>
            <div class="form-group">
                <f:input type="text"
                    id="inputCategory" class="form-control" path="ctName" value="${categoryModify.ctName}"/>
            </div>
            <f:button class="btn btn-info" type="submit">Alterar</f:button>
            <a class="btn btn-default" href="<c:url value="category"/>">Cancelar</a>                            
        </f:form>
    </div>
</div>

The controller:

//Mapeamento para mostrar a categoria na tela.
@RequestMapping(value="editCategory", method = RequestMethod.GET)
public String editCategory(Long id, Model model, @Valid Category category, BindingResult result){    
        model.addAttribute("validator", false);
        model.addAttribute("categoryModify", dashboardFacade.getCategoryId(id));
        return "category/updateCategory";    
}

// Update da categoria
@RequestMapping(value="/updateCategory", method = RequestMethod.GET)
public String updateCategory( @Valid Category category, @RequestParam Long id, @RequestParam String ctName, BindingResult result, Model model) {

    if (result.hasErrors()) {
        model.addAttribute("validator", true);
        return "category/updateCategory";
    } else {
        dashboardFacade.categoryUpdate(ctName, id);
        logger.info("A categoria " + category.getIdCategory() + " pertencente a agência " + dashboardFacade.getAgency() + " foi adicionada.");
        return "redirect:category";
    }
}

Model:

@Column(name="ct_name")
@NotEmpty
private String ctName;

Thank you all!

  • 1

    Buddy, this code of yours is a little confusing. Actually doing what you’re looking for is easier than you think. Take a look at these links: Forms, A practical example. Success! :)

  • Hi Fabio, actually it was my own mistake. Cool the links you sent. It will clarify me many doubts from now on.

1 answer

1


You need to look at the URL to which the form is redirecting.

Your tag <f:form> points to the relative URL updateCategory, while it seems that your controller method points to a context-related URL (/updateCategory).

This is if there is no annotation in the class (which does not appear in the code), which would cause the method URL to be concatenated to the class URL.

Example

Assuming the page with the form is as follows:

http://localhost:8080/app/category/edit

By clicking the button Submit, relative, the URL invoked will be:

http://localhost:8080/app/category/updateCategoy

Still supposing that the expected is:

http://localhost:8080/app/updateCategoy

So the solution would be that the attribute action of the form were:

<f:form action="../updateCategory" ... >
  • Really @utluiz, it was really this problem. Actually as I’m using the Tiles Framework I end up confusing things a little, but I’ve already managed to understand the full functioning!

Browser other questions tagged

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