gpt4 book ai didi

java - 在 "form"中传递带有路径参数的对象与 "select"不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:49 26 4
gpt4 key购买 nike

我有一个实体,它有两个属性,即对象(EmpresaCiudad)。

package es.dxd.project.model;

import java.io.Serializable;
import javax.persistence.*;


/**
* The persistent class for the REPRESENTANTE database table.
*
*/
@Entity
public class Representante implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID_REPRESENTANTE")
private long idRepresentante;

@Column(name="EMAIL")
private String email;

@Column(name="NOMBRE")
private String nombre;

@Column(name="NUM_TELEFONO")
private String numTelefono;

//bi-directional many-to-one association to Empresa
@ManyToOne
@JoinColumn(name="ID_EMPRESA")
private Empresa empresa;

//bi-directional many-to-one association to Ciudad
@ManyToOne
@JoinColumn(name="ID_SEDE")
private Ciudad ciudad;

public Representante() {
}

public long getIdRepresentante() {
return this.idRepresentante;
}

public void setIdRepresentante(long idRepresentante) {
this.idRepresentante = idRepresentante;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public String getNombre() {
return this.nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public String getNumTelefono() {
return this.numTelefono;
}

public void setNumTelefono(String numTelefono) {
this.numTelefono = numTelefono;
}

public Empresa getEmpresa() {
return this.empresa;
}

public void setEmpresa(Empresa empresa) {
this.empresa = empresa;
}

public Ciudad getCiudad() {
return this.ciudad;
}

public void setCiudad(Ciudad ciudad) {
this.ciudad = ciudad;
}

}

我想从 .jsp 中插入一个“Representante”实体,并在表单中进行选择。我在下面展示了它。

<form:form method="post" modelAttribute="representante"
action="${pageContext.request.contextPath}/addRepresentante">
<div align="center">
<table>
<tr>
<th colspan="2" style="text-align: center;">Añadir
representante</th>
</tr>
<tr>
<form:hidden path="idRepresentante" />
<td><form:label path="nombre">Nombre:</form:label></td>
<td><form:input path="nombre" size="30" maxlength="20"></form:input></td>
</tr>

<tr>
<td><form:label path="numTelefono">Teléfono:</form:label></td>
<td><form:input path="numTelefono" size="30" maxlength="20"></form:input></td>
</tr>
<tr>
<td><form:label path="email">Email:</form:label></td>
<td><form:input path="email" size="30" maxlength="20"></form:input></td>
</tr>
<tr>
<td>Ciudad:</td>
<td><form:select path="ciudad">
<form:option value="NONE" label="Selecciona una ciudad" />
<form:options items="${listaDeCiudades}" itemLabel="nombreCiudad" />
</form:select></td>
</tr>
<tr>
<td><form:label path="empresa">Empresa:</form:label></td>
<td><form:select path="empresa">
<form:option value="NONE" label="Selecciona una empresa" />
<form:options items="${listaDeEmpresas}" itemLabel="nombre"
itemValue="idEmpresa" />
</form:select></td>
</tr>

<tr>
<td colspan="2"><input type="submit" class="blue-button"
value="Insertar" /></td>

</tr>
</table>
</div>
</form:form>

最后,我显示提交表单时激活的驱动程序:

@RequestMapping(value = "/addRepresentante", method = RequestMethod.POST, headers = "Accept=application/json")
public String addRepresentante(@ModelAttribute("representante") Representante representante) {

if(representante.getIdRepresentante() == 0) {
representanteService.addRepresentante(representante);
}else{
representanteService.updateRepresentante(representante);
}

return "redirect:/getAllRepresentantes";
}

问题是当我提交表单时, Controller 未激活。它返回一个错误:“客户端发送的要求在语法上不正确”。我认为错误是在通过带有参数“path”的表单发送对象时出现的。

最佳答案

表单中的数据无法使用标题“Accept=application/json”发布。您必须在 Controller 函数中使用 @RequestParams 将数据从表单获取到后端 Controller 函数。此外,enctype 参数的值应为“multipart/form-data”。

<form:form method="post" action="fileupload" enctype="multipart/form-data"
id="uploadForm" >Input Elements Here</form:form>

在 Controller 端,您需要对 Controller 进行如下配置:

public void TestController(@RequestParam("email") String email, ...)  { // do your thing here }

关于java - 在 "form"中传递带有路径参数的对象与 "select"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47200517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com