我有这个控制器:
@RequestMapping(value="admin/departamento", method = RequestMethod.POST)
public ModelAndView departamentoGuardar(
@Valid Departamento departamento ,
BindingResult br
){
ModelAndView mav = new ModelAndView();
mav.setViewName("costos/departamento");
return mav;
}
......它正在发挥作用。
但是,如果我构建一个空的存储库类:
@Repository("departamentoRepository")
public interface DepartamentoRepository extends JpaRepository<Departamento, Integer>{
}
我的控制器在放置String
并且只接受int
时会抛出此信息:
org.springframework.beans.TypeMismatchException:无法转换 类型'java.lang.String'的值为必需的类型 'com.carrduci.gestionycontrol.model.Departamento';嵌套异常是 org.springframework.core.convert.ConversionFailedException:失败 从类型[java.lang.String]转换为类型[java.lang.Integer] 价值'asdf';嵌套异常是java.lang.NumberFormatException:For 输入字符串:“asdf”at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:80) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:52) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:692) 〜[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttributeFromRequestValue(ServletModelAttributeMethodProcessor.java:141) 〜[弹簧webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE
我的班级:
@Entity
@Table(name = "departamento")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Departamento.findAll", query = "SELECT d FROM Departamento d")})
public class Departamento implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id_departamento")
private Integer idDepartamento;
@Size(max = 20)
@Column(name = "departamento")
private String departamento;
public Departamento() {
}
public Departamento(Integer idDepartamento) {
this.idDepartamento = idDepartamento;
}
public Integer getIdDepartamento() {
return idDepartamento;
}
public void setIdDepartamento(Integer idDepartamento) {
this.idDepartamento = idDepartamento;
}
public String getDepartamento() {
return departamento;
}
public void setDepartamento(String departamento) {
this.departamento = departamento;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idDepartamento != null ? idDepartamento.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 Departamento)) {
return false;
}
Departamento other = (Departamento) object;
if ((this.idDepartamento == null && other.idDepartamento != null) || (this.idDepartamento != null && !this.idDepartamento.equals(other.idDepartamento))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.carrduci.gestionycontrol.model.Departamento[ idDepartamento=" + idDepartamento + " ]";
}
}
<th:block th:include="fragments/encabezados :: encabezados"></th:block>
<div class="container">
<div class="row">
<div class="col-md-offset-4 col-sm-offset-0">
<div class="row">
<div class="col-sm-12"> <h4>Departamento</h4></div>
</div>
<div class="row">
<div class="col-sm-12 col-md-8 ">
<form th:action="${departamento.idDepartamento>0}?
@{/costos/admin/departamento/modificar}:
@{/costos/admin/departamento}"
method="post" th:object="${departamento}">
<div class="row">
<div class="form-group col-xs-12">
<input type="hidden" th:field="*{idDepartamento}">
<div class="input-group">
<span class="input-group-addon">Nombre</i></span>
<input class="form-control input-lg" type="text" th:field="*{departamento}">
<div class="alert alert-danger" th:if="${#fields.hasErrors('departamento')}" th:errors="*{departamento}" ></div>
</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="${departamento.idDepartamento>0}?
@{Modificar}:
@{Guardar}">
</span>
</button>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-8 ">
<hr>
<th:block th:include="fragments/tabla :: tabla"></th:block>
</div>
</div>
</div>
</div>
</div>
<th:block th:include="fragments/pieDePagina :: pieDePagina"></th:block>