在我目前的春季项目中,我有一个具有此输入字段的表单:
<div class="panel panel-default">
<div class="panel-heading">
<form:label path="${entry.key}">${entry.key}</form:label>
</div>
<div class="panel-body">
<form:select path="${entry.key}.id" class="form-control">
<form:options items="${values[entry.key]}" itemValue="id" itemLabel="nome"/>
</form:select>
</div>
</div>
如果用户不想选择一个选项(数据库中的atribute可以为null),我想添加一个默认值。我试试这个:
1 -
<form:option value="-" label="--Please Select"/>
2 -
<form:option value="" label="--Please Select"/>
3 -
<form:option value="" label="--Please Select" disabled="disabled"/>
但这些选项都不起作用(我收到错误
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.spring.loja.model.categoria.persistence.model.Categoria
当我运行应用程序时)。
任何人都可以告诉我实现这一目标的正确方法是什么?
更新
我项目中的2个实体类。上面的代码适用于第一个(Product),但是当我尝试插入第二个(Order)时,我得到错误TransientObjectException
。
实体类:产品
@Entity
@Table(name="produto")
public class Produto {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="nome", unique=true)
@Order(value=1)
private String nome;
@Column(name="preco")
@Order(value=2)
private Float preco;
@OneToOne
@JoinColumn(name="categoria")
@Order(value=3)
private Categoria categoria;
@Column(name="resumo", length=140)
@Order(value=4)
private String resumo;
@Column(name="descricao", length=65535)
@Order(value=5)
private String descricao;
}
实体类:订单
@Entity
@Table(name="pedido")
public class Pedido {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="cliente")
@Order(value=1)
private Cliente cliente;
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="produto")
@Order(value=2)
private Produto produto;
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="cobranca")
@Order(value=3)
private Cobranca cobranca;
}
更新2
我试试这个:
<form:select path="${entry.key}.id" class="form-control">
<form:option value="" label="--Please Select"/>
<form:options items="${values[entry.key]}"></form:options>
</form:select>
这样,当我尝试插入新值时出现错误。有了这个:
<form:select path="${entry.key}" class="form-control">
<form:option value="" label="--Please Select"/>
<form:options items="${values[entry.key]}"></form:options>
</form:select>
我只能在没有选择选项的情况下插入新项目。我试试这个实体类:
@Entity
@Table(name="pagina")
public class Pagina {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "nome", unique=true, nullable=false, length=32)
@Order(value=1)
private String nome;
@Column(name = "titulo", nullable=false, length=32)
@Order(value=2)
private String titulo;
@Column(name="resumo", length=140)
@Order(value=3)
private String resumo;
@Column(name = "descricao", length=65535)
@Order(value=4)
private String descricao;
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, optional=true)
@JoinColumn(name="pagina")
@Order(value=5)
private Pagina pagina;
@OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, optional=true)
@JoinColumn(name="produto")
@Order(value=6)
private Produto produto;
}
任何人都可以看到这里有什么问题?
答案 0 :(得分:1)
我不确定TransientObjectException的原因,但下面是如何将默认值设置为form的简单示例:select。希望这会有所帮助。
<强> JSP:强>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>example</title>
</head>
<body>
<form:form modelAttribute="myForm" method="post">
<form:select path="valueOfSelectElement">
<form:option value="default-value" label="--Please Select" />
<form:options items="${options}" itemValue="id" itemLabel="name" />
</form:select>
<input type="submit" />
</form:form>
</body>
</html>
<强>控制器:强>
package org.myorg.myapp;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping(value = "/form", method = RequestMethod.GET)
public ModelAndView showForm() {
ModelAndView mav = new ModelAndView("form");
List<IdAndName> options = Arrays.asList(
new IdAndName("id1", "name1"),
new IdAndName("id2", "name2"),
new IdAndName("id3", "name3")
);
mav.addObject("options", options);
MyForm myForm = new MyForm();
myForm.setValueOfSelectElement("default-value");
mav.addObject("myForm", myForm);
return mav;
}
@RequestMapping(value = "/form", method = RequestMethod.POST)
@ResponseBody
public String processForm(MyForm myForm) {
return myForm.getValueOfSelectElement();
}
static class MyForm {
private String valueOfSelectElement;
public String getValueOfSelectElement() {
return valueOfSelectElement;
}
public void setValueOfSelectElement(String valueOfSelectElement) {
this.valueOfSelectElement = valueOfSelectElement;
}
}
static class IdAndName {
private String id;
private String name;
public IdAndName(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
答案 1 :(得分:0)
要为您的选择表单设置默认值,请选择=&#34;选择&#34;属性到您的表单:选项标记。但我不确定你的org.hibernate.TransientObjectException是否以某种方式与
相关联