Validation of input triggering error 400 with Spring MVC

0

Personal, there is a boring error in which I'm losing sleep to find out how to treat.

Well, every time I run my test by checking the validation of my field a 400 error is triggered.

I do not know how I can resolve this. The server log does not report anything.

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";
    }
}

Template:

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

Thank you all!

    
asked by anonymous 19.08.2014 / 00:38

1 answer

1

Attention to the URL that the form is redirecting to.

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

If there is no annotation in the class (which does not appear in the code), that would cause the method's URL to be concatenated to the class URL.

Example

Assuming that the page with the form is the following:

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

By clicking on the submit button, relative, the URL invoked will be:

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

Assuming also that expected is:

http://localhost:8080/app/updateCategoy

So the solution would be for the action attribute of the form to be:

<f:form action="../updateCategory" ... >
    
16.09.2014 / 16:39