我一直在使用百里香和弹簧靴。我可以在POST和一个简单的类中使用表单发送数据。 (没有对象列表)。我试图修改(以我的形式)包含对象列表的“对象”,但在我的控制器中,我得到了具有正确数据的对象,但列表为空。
我有这个类,它包含一个对象列表:
@Entity
@Table(name = "maquina_modelo")
public class MaquinaModelo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "modelo")
private String modelo;
@Basic(optional = false)
@NotNull
@Column(name = "anio")
private int anio;
@NotNull
@Column(name = "costo")
private BigDecimal costo;
@NotNull
@Column(name = "depreciacion")
private BigDecimal depreciacion;
//HACE REFERENCIA AL ATRIBUTO. El CAMPO DE LA CLASE, NO EL DE LA BD.
@OneToMany(mappedBy = "maquinaModelo")
@NotFound(action = NotFoundAction.IGNORE)
private List<RelacionMaquinaModeloGastoIndirecto> relacionMaquinaModeloGastoIndirectoCollection;
public MaquinaModelo() {
}
public MaquinaModelo(Integer id) {
this.id = id;
}
public MaquinaModelo(Integer id, String modelo, int anio) {
this.id = id;
this.modelo = modelo;
this.anio = anio;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof MaquinaModelo)) {
return false;
}
MaquinaModelo other = (MaquinaModelo) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "MaquinaModelo{" + "id=" + id + ", modelo=" + modelo + ", anio=" + anio + ", costo=" + costo + ", depreciacion=" + depreciacion + ", relacionMaquinaModeloGastoIndirectoCollection=" + '}';
}
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
this.modelo = modelo;
}
public int getAnio() {
return anio;
}
public void setAnio(int anio) {
this.anio = anio;
}
public List<RelacionMaquinaModeloGastoIndirecto> getRelacionMaquinaModeloGastoIndirectoCollection() {
return relacionMaquinaModeloGastoIndirectoCollection;
}
public void setRelacionMaquinaModeloGastoIndirectoCollection(List<RelacionMaquinaModeloGastoIndirecto> relacionMaquinaModeloGastoIndirectoCollection) {
this.relacionMaquinaModeloGastoIndirectoCollection = relacionMaquinaModeloGastoIndirectoCollection;
}
public BigDecimal getCosto() {
return costo;
}
public void setCosto(BigDecimal costo) {
this.costo = costo;
}
public BigDecimal getDepreciacion() {
return depreciacion;
}
public void setDepreciacion(BigDecimal depreciacion) {
this.depreciacion = depreciacion;
}
}
我可以在MaquinaModelo
中执行CRUD操作,并以此格式显示来自List<RelacionMaquinaModeloGastoIndirecto>
的数据:
<form th:action="${maquinaModelo.id>0}? @{/costos/admin/maquinaModelo/modificar}:@{/costos/admin/maquinaModelo}" method="post" >
<div class="form-group">
<input type="hidden" name="id";
th:value="${maquinaModelo.id}">
<div class="input-group">
<span class="input-group-addon">Modelo</span>
<input th:type="text"
th:value="${maquinaModelo.modelo}"
name="modelo"
class="form-control input-lg mayusculas">
<div th:if="${#fields.hasErrors('maquinaModelo.modelo')}" th:errors="${maquinaModelo.modelo}" class="alert alert-danger"></div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Año</span>
<input th:type="text"
th:value="${maquinaModelo.anio}"
name="anio"
class="form-control input-lg mayusculas">
<div th:if="${#fields.hasErrors('maquinaModelo.anio')}" th:errors="${maquinaModelo.anio}" class="alert alert-danger"></div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Costo</span>
<input th:type="text"
th:value="${maquinaModelo.costo}"
name="costo"
class="form-control input-lg mayusculas">
<div th:if="${#fields.hasErrors('maquinaModelo.costo')}" th:errors="${maquinaModelo.costo}" class="alert alert-danger"></div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Depreciación</span>
<input th:type="text"
th:value="${maquinaModelo.depreciacion}"
name="depreciacion"
class="form-control input-lg mayusculas">
<div th:if="${#fields.hasErrors('maquinaModelo.depreciacion')}" th:errors="${maquinaModelo.depreciacion}" class="alert alert-danger"></div>
</div>
</div>
<div class="row">
<div class="col-xs-12 text-right">
<a type="button" class="btn btn-danger BOTON_LIMPIAR" href="/index"> <span class=" glyphicon glyphicon-remove"></span> Cancelar</a>
<button type="submit" class="btn btn-success"> <span class="glyphicon glyphicon-floppy-save"></span> <span th:text="${maquinaModelo.id>0}? @{Modificar}:@{Guardar}"></span></button>
</div>
</div>
<div class="form-group col-xs-12 ">
<div class="input-group">
<span class="input-group-addon">Gastos indirectos</span>
<select class="form-control input-lg lista-articulos" >
<option th:each="gasto:${gastosIndirectos}"
th:value="${gasto.idGastoIndirecto}"
th:text="${gasto.nombreGasto}"> </option>
</select>
</div>
<a class="btn btn-success btn-cargar-articulo"> + Añadir gasto</a>
</div>
<hr>
<h1>Elementos de gasto indirecto</h1>
<div th:each="costo, i:${maquinaModelo.relacionMaquinaModeloGastoIndirectoCollection}" >
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" th:text="${costo.gastoIndirecto.nombreGasto}" ></span>
<input th:type="text"
th:value="${maquinaModelo.relacionMaquinaModeloGastoIndirectoCollection[__${i.index}__].consumoHora}"
th:name="${maquinaModelo.relacionMaquinaModeloGastoIndirectoCollection[__${i.index}__]}"
class="form-control input-lg mayusculas">
<!-- <div th:if="${#fields.hasErrors('maquinaModelo.depreciacion')}" th:errors="${maquinaModelo.depreciacion}" class="alert alert-danger"></div> -->
</div>
</div>
</div>
</form>
但是,当我提交表单时,列表为空。我怎样才能让它发挥作用?
@Controller
@RequestMapping(value ="/costos/**")
public class CostosController {
//....
@RequestMapping(value = "admin/maquinaModelo/modificar/{id}", method = RequestMethod.GET)
public ModelAndView maquinaModeloModificar(@PathVariable("id") int id){
ModelAndView mav = new ModelAndView();
if (maquinaModeloService.exist(id)) {
MaquinaModelo maquina = maquinaModeloService.find(id);
mav.addObject("maquinaModelo", maquina);
} else {
mav.addObject("error", "El modelo no existe: "+id);
mav.addObject("maquinaModelo", new MaquinaModelo());
}
mav.addAllObjects(gds.costos_MaquinaModelo(gds.getUser()));
mav.setViewName("costos/maquinaModelo");
return mav;
}
@RequestMapping(value = "admin/maquinaModelo/modificar", method = RequestMethod.POST)
public ModelAndView maquinaModeloModificar(
MaquinaModelo maquinaModelo,
BindingResult br
){
ModelAndView mav = new ModelAndView();
if (br.hasErrors()) {
mav.setViewName("costos/maquinaModelo");
mav.addObject("maquinaModelo", maquinaModelo);
} else {
if (maquinaModeloService.update(maquinaModelo)) {
mav.addObject("success", "El modelo se ha modificado con exito.");
mav.addObject("maquinaModelo", new MaquinaModelo());
} else {
mav.addObject("error", "Algo paso y no se pudo guardar el modelo.");
mav.addObject("maquinaModelo", maquinaModelo);
}
}
mav.addAllObjects(gds.costos_MaquinaModelo(gds.getUser()));
mav.setViewName("costos/maquinaModelo");
return mav;
}
}