Arrays Struts2将表单数据填充到Map的数组中<;字符串,对象>;
动作类Arrays Struts2将表单数据填充到Map的数组中<;字符串,对象>;,arrays,forms,jsp,struts2,ognl,Arrays,Forms,Jsp,Struts2,Ognl,动作类 public class ProductAction extends ActionSupport implements Preparable { private Document product; } 模型 如果我将Document->properties更改为Map 但由于其他数据类型的原因,我希望将文档->属性保留为Map 如何解决此问题,我希望表单数据填充为String,而不是String[] 我没有多个同名的文本字段。试试这个 <s:textfield name=
public class ProductAction extends ActionSupport implements Preparable {
private Document product;
}
模型
如果我将Document->properties更改为Map
但由于其他数据类型的原因,我希望将文档->属性保留为Map
如何解决此问题,我希望表单数据填充为String
,而不是String[]
我没有多个同名的文本字段。试试这个
<s:textfield name="product.properties.SUPPLIER"
value="%{product.properties.SUPPLIER[0]}"
label="%{getText('label.SUPPLIER')}" size="40" />
这将检索数组中的第一项修改模型并引入使用Map的方法,但将数据保存到底层Map,如下所示
public class Document {
private Map<String, Object> properties;
public Map<String, String> getStringProperties() {
//logic to populate Map<String, String>
return new HashMap<String, String>();
}
public void setStringProperties(Map<String, String> stringProperties) {
properties.putAll(stringProperties);
}
}
<s:textfield name="product.stringProperties.SUPPLIER"
value="%{product.stringProperties.SUPPLIER}"
label="%{getText('label.SUPPLIER')}" size="40" />
公共类文档{
私人地图财产;
公共映射getStringProperties(){
//填充映射的逻辑
返回新的HashMap();
}
公共void SetStringProperty(映射stringProperties){
属性。putAll(stringProperties);
}
}
在jsp中,使用如下所示的stringProperties
public class Document {
private Map<String, Object> properties;
public Map<String, String> getStringProperties() {
//logic to populate Map<String, String>
return new HashMap<String, String>();
}
public void setStringProperties(Map<String, String> stringProperties) {
properties.putAll(stringProperties);
}
}
<s:textfield name="product.stringProperties.SUPPLIER"
value="%{product.stringProperties.SUPPLIER}"
label="%{getText('label.SUPPLIER')}" size="40" />
解决方案:我们可以将类型转换器应用于bean或模型 为此,使用比编写
类型转换器要容易得多
@Element( value = java.lang.String.class )
private Map<String, Object> properties;
@Element(value=java.lang.String.class)
私人地图财产;
要显示什么,键还是值。了解如何在struts2中使用索引属性。我想将私有映射属性传递给dao层进行保存,但映射数组导致了一个问题,dao层只接受映射。为什么在同一映射中保留不同的数据类型?因为这是处理数据的最简单方法,在那里我可以得到日期、字符串、整数等答复。我在jsp中没有问题,但实际的问题是我不希望在action Map中使用数组。
@Element( value = java.lang.String.class )
private Map<String, Object> properties;