简体   繁体   English

HTTP状态400 –错误的请求-Spring MVC

[英]HTTP Status 400 – Bad Request - Spring MVC

i've got this error on my project. 我的项目中出现此错误。

HTTP Status 400 – Bad Request. HTTP状态400 –错误的请求。

The server cannot or will not process the request due to something that is perceived to be a client error (eg, malformed request syntax, invalid request message framing, or deceptive request routing). 由于某些东西被认为是客户端错误(例如,格式错误的请求语法,无效的请求消息框架或欺骗性的请求路由),服务器无法或不会处理请求。

InsertClient.jsp InsertClient.jsp

<form:form action="/moldar/saveCliente/" method="POST" modelAttribute="cliente">

    <div class="form-group">
        <label for="exampleSelect1">Pais</label> 
        <form:select path="pais" class="form-control" id="pais" name="wwww">
            <c:forEach var="lista" items="${listaPaises}">
                <option value="${lista.id}">${lista.nome}</option>
            </c:forEach>
        </form:select>
    </div>

    <div class="form-group">
        <label for="exampleSelect1">Estado</label> 
        <form:select path="estado" class="form-control" id="estado" name="qqqqq">
            <option value="">Selecione um estado</option>
            <c:forEach var="lista" items="${listaEstados}">
                <option value="${lista.id}">${lista.nome}</option>
            </c:forEach>
        </form:select>
    </div>

    <div class="form-group">
        <label for="exampleSelect1">Cidade</label> 
        <form:select path="cidade" class="form-control" id="cidade" name="batata">
            <option value="null">Selecione uma cidade</option>
            <c:forEach var="lista" items="${listaCidades}">
                <option value="${lista.id}">${lista.nome}</option>
            </c:forEach>
        </form:select>
    </div>

My Controllers 我的控制器

@RequestMapping(value = "/saveCliente", method = RequestMethod.POST)
        public String adicionarCliente(@ModelAttribute("cliente") Cliente cliente) {


            clienteDao.saveOrUpdate(cliente);

            return "redirect:/clientes";
        }

@RequestMapping(value = "/addCliente", method = RequestMethod.GET, headers = "Accept=application/json")
        public String inserirCliente(Model model) {

            List<Estado> listaEstados = estadoDao.list(null);
            List<Cidade> listaCidades = cidadeDao.list(null);
            List<Pais> listaPaises = paisDao.list(null);

            model.addAttribute("listaPaises", listaPaises);
            model.addAttribute("listaEstados", listaEstados);
            model.addAttribute("pais", new Pais());
            model.addAttribute("cliente", new Cliente());
            model.addAttribute("estado", new Estado());
            model.addAttribute("cidade", new Cidade());
            model.addAttribute("listaCidades", listaCidades);
            return "inserirCliente";
        }

Model 模型

@Entity
@Table (name= "clientes")
public class Cliente {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column
    private Integer id;

    @Column
    private String nome;

    @Column
    private String cpf;

    @OneToOne 
    @JoinColumn (name="pais")
    private Pais pais;

    @OneToOne
    @JoinColumn (name="estado")
    private Estado estado;

    @OneToOne
    @JoinColumn (name="cidade")
    private Cidade cidade;

    @Column
    private String logradouro;

    @Column
    private String numero;

    @Column
    private String cep;

    @Column
    private String email;

I have trying everything. 我已经尽力了。

Can someone help me ? 有人能帮我吗 ? Please ? 请 ?

In your Contoller this line of code 在您的Contoller中,这行代码

     public String adicionarCliente(@ModelAttribute("cliente") Cliente cliente)

In your Cliente class the variables like pais 在您的Cliente类中,像pais这样的变量

<form:select path="pais" class="form-control" id="pais" name="wwww">

you cannot bind this path="pais" to Cliente class variable pais , Because pais is not a String Type it is an Object type. 您不能将此path =“ pais”绑定到Cliente类变量pais ,因为pais不是字符串类型,所以它是对象类型。

You can Create another class to Bind the input Selection Options to the variables. 您可以创建另一个类,以将输入的选择选项绑定到变量。

class Client {
  private String pais;
  private String estado;
  private String cidade;

public String getPais() {
    return pais;
}

public void setPais(String pais) {
    this.pais = pais;
}

public String getEstado() {
    return estado;
}

public void setEstado(String estado) {
    this.estado = estado;
}

public String getCidade() {
    return cidade;
}

public void setCidade(String cidade) {
    this.cidade = cidade;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM