如何使用隐藏字段在wicket中存储数据模型
我有一个实体,名为Product。它有两个属性:unit(字节)和unitName(字符串)。单元属性映射到数据库上。Ex:0:Kg;1:g;。。。。我想当输入一个有效的单位时,单位属性被存储;除非将其保存为unitName 产品如何使用隐藏字段在wicket中存储数据模型,wicket,Wicket,我有一个实体,名为Product。它有两个属性:unit(字节)和unitName(字符串)。单元属性映射到数据库上。Ex:0:Kg;1:g;。。。。我想当输入一个有效的单位时,单位属性被存储;除非将其保存为unitName 产品 public class Product implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = Genera
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private int productId;
@Column(name = "product_name")
private String productName;
@Column(name = "unit")
private Byte unit;
@Transient
private String unitName;
}
public class UnitConverter implements IConverter<Byte> {
private static final long serialVersionUID = 4798262219257031818L;
public UnitConverter() {
}
@Override
public Byte convertToObject(String value, Locale locale) {
return Text.isEmpty(value) ? 0 : UtilCommon.getTaniCode(value);
}
@Override
public String convertToString(Byte value, Locale locale) {
return (value == null || value==0 ) ? "" : UtilCommon.getTaniName(value);
}
}
在unit文本字段中,我使用UnitConvert
UnitConvert
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private int productId;
@Column(name = "product_name")
private String productName;
@Column(name = "unit")
private Byte unit;
@Transient
private String unitName;
}
public class UnitConverter implements IConverter<Byte> {
private static final long serialVersionUID = 4798262219257031818L;
public UnitConverter() {
}
@Override
public Byte convertToObject(String value, Locale locale) {
return Text.isEmpty(value) ? 0 : UtilCommon.getTaniCode(value);
}
@Override
public String convertToString(Byte value, Locale locale) {
return (value == null || value==0 ) ? "" : UtilCommon.getTaniName(value);
}
}
公共类UnitConverter实现IConverter{
私有静态最终长serialVersionUID=4798262219257031818L;
公共单位转换器(){
}
@凌驾
公共字节转换器对象(字符串值、区域设置){
返回Text.isEmpty(值)?0:UtilCommon.getTaniCode(值);
}
@凌驾
公共字符串convertToString(字节值、区域设置){
return(value==null | | value==0)?“”:UtilCommon.gettanName(value);
}
}
我只想到希登菲尔德会这么做,但我不知道怎么做。
有人知道如何使用或任何东西可以帮助我。非常感谢因此,据我所知,您希望将
模型
的输入保存到不同的数据库属性,具体取决于之前的某些检查。您可以在表单.onSubmit()方法中执行此操作
一个非常简单的实现可以如下所示:
public ProductPanel(String id, final IModel<Object> productModel) {
super(id, productModel);
// we're putting the productModel into the constructor.
// Therefore it's guaranteed to be detached
// -> it's okay to have it with final modifier.
IModel<String> formModel = Model.of("");
Form<String> form = new Form<String>("form", formModel) {
@Override
protected void onSubmit() {
super.onSubmit();
String productName = getModelObject();
Object realProduct = productModel.getObject();
if (isAcceptableUnit(productName)) {
realProduct.setUnit(parseUnit(productName));
} else {
realProduct.setUnitName(productName);
}
layer.saveProduct();
}
};
add(form);
TextField<String> productName = new TextField<String>("textField", formModel);
form.add(productName);
}
private boolean isAcceptableUnit(String productName) {
// your logic to determine if it's okay to cast to byte here...
return true;
}
private byte parseUnit(String productName) {
// your logic to parse the String to byte here...
return 0;
}
公共产品面板(字符串id,最终IModel产品模型){
super(id,productModel);
//我们正在将productModel放入构造函数中。
//因此,它保证是分离的
//->可以使用最终修改器。
IModel formModel=模型的(“”);
表单=新表单(“表单”,表单模型){
@凌驾
受保护的void onSubmit(){
super.onSubmit();
字符串productName=getModelObject();
Object realProduct=productModel.getObject();
if(iAcceptableUnit(productName)){
setUnit(parseUnit(productName));
}否则{
realProduct.setUnitName(产品名称);
}
layer.saveProduct();
}
};
添加(表格);
TextField productName=新的TextField(“TextField”,formModel);
form.add(产品名称);
}
专用布尔值isAcceptableUnit(字符串产品名称){
//你的逻辑决定是否可以在这里强制转换到字节。。。
返回true;
}
专用字节解析单元(字符串产品名称){
//您在此处将字符串解析为字节的逻辑。。。
返回0;
}
由于我不确定您提供的代码片段是否只是为了简单起见,或者实际上是为了代码片段,因此需要添加一些注释:
您应该尽量避免声明db对象可序列化。如果您使用普通的Model
对象来保存DTOs,wicket实际上会对它们进行序列化,您将无法对它们进行任何操作(至少在hibernate中是这样)。
数据库对象应使用LoadableDetachableModel
并将主键保存在其load()
方法中加载实体。
这将使您现在能够通过使用CompoundPropertyModel
等直接处理这些对象(这有其优点和缺点,我在这里将不详细解释)
在您的情况下,我会在表单中添加一个
模型
,让服务器决定如何处理输入并将其映射到实际的域对象。您可以使用org.apache.wicket.markup.html.form.HiddenField
作为wicket中的任何其他表单字段。那么问题是什么呢?