Spring MVC,如何通过AJAX提交表单并使用ModelAttribute @Valid进入控制器

时间:2017-06-04 16:05:06

标签: java ajax spring web

我有以下表格:

<form:form method="POST" action="${root}/Rhindo/Gerente/AddCargo" commandName="formCargo" >
    <div class="modal-body">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <label class="text-muted">Nome</label>
                <form:input type="text" path="nome" cssClass="form-control"/>
            </div>
        </div>
        <form:errors path="nome" cssClass="text-danger" />
        <br /><br />
        <div class="row">
            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <label class="text-muted">Salario</label>
                <form:input path="salario" placeholder="Somente Números" cssClass="form-control" value=""/>
            </div>
        </div>
        <form:errors path="salario" cssClass="text-danger" />
        <br /><br />
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <label class="text-muted">Requisitos</label>
                <form:textarea type="text" path="requisitos" cssClass="form-control" style="resize: none"/>
            </div>
        </div>
        <form:errors path="salario" cssClass="text-danger" />
        <br /><br />
        <div class="row">
            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <label class="text-muted">Carga Mínima</label>
                <form:input type="text" path="cargamin" cssClass="form-control" value=""/>
                <form:errors path="cargamin" cssClass="text-danger" />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <label class="text-muted">Desconto Impostos</label>
                <form:input type="text" path="descImpostos" cssClass="form-control" value=""/>
                <form:errors path="descImpostos" cssClass="text-danger" />
                <br />
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <input type="button" class="btn btn-warning" value="Cancelar" data-dismiss="modal" />
        <input type="submit" class="btn btn-primary" value="Cadastrar Cargo" id="cadCargo" />
    </div>
</form:form>

类别:

@Model
public class CargoCadastroModel {

    @NotEmpty(message = "Informe o nome!") @Pattern(regexp = "^\\p{L}+(?: \\p{L}+)*$", message = "Nome inválido")
    private String nome;

    @NotEmpty(message = "Informe o salário!") @Pattern(regexp = "[0-9]d+,d{0,2}$", message = "Valor inválido")
    private String salario;

    @NotEmpty(message = "Informe os Requisitos!")
    private String requisitos;

    @NotEmpty(message = "Informe a carga mínima!") @Pattern(regexp = "[0-9]+$", message = "Valor inválido, somente números")
    private String cargamin;

    @NotEmpty(message = "Informe o imposto!") @Pattern(regexp = "[0-9]d+,d{0,2}$", message = "Valor inválido somente números")
    private String descImpostos;

    public CargoCadastroModel() {
    }

Ajax脚本:

$(document).on('click', '#cadCargo', function (e) {
    e.preventDefault();  // prevent standard form submission
    var fdata = new FormData($('#formCargo')[0]);
    debugger;
    $.ajax({
        type: 'post',
        url: '/Rhindo/Gerente/AddCargo',
        data: fdata,
        async: false,
        cache: false,
        processData: false,
        dataType: "html",
        contentType: 'application/json',
        success: function (content) {
            debugger;
            $("#cadModal").html(content);
        },
    });
    return false;
});

和控制器:

@RequestMapping(value = "/Gerente/AddCargo", method = RequestMethod.POST)
public String getFormCargo(@RequestBody @ModelAttribute("formCargo") @Valid CargoCadastroModel cargoModel, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        model.addAttribute(cargoModel);
        return "/gerente/cadastroCargo";
    }
    else{

    }
    return "/gerente/cadastroCargo";
}

但是在控制器中我的模型总是为空; 我刚试过JSON.strignfy,序列化,没什么用 请帮忙

0 个答案:

没有答案