`

近期使用Struts中logic标签的一些总结

阅读更多
1、假设存在如下类:
public class PropertyDesc
{
   private int type;//确定值是以文本框显示还是列表显示
   private String propertyName;
   private String propertyValue;
   private List<EntryDesv> propertyList;

   ……
}

public class EntryDesc
{
   private int value;
   private String UIDisplay;
   
   ……
}

2、将多个PropertyDesc对象放进HashMap中,方式如下:
Map<Integer, PropertyDesc> map = new HashMap<Integer, PropertyDesc>();

map.put(1, propertyDesc1);
map.put(2, propertyDesc2);
map.put(3, propertyDesc3);
……

request.setAttribute("map",map);


3、在页面可使用如下方式呈现:

<logic:iterate id="attr" indexId = "index" name="map">
   <logic:equal name="attr" property="value.attrType" value="6">
      <select size="1" name="select">
<logic:iterate id="list" name="attr" property="value.attrList">
<option selected value="<bean:write name="list" property="value"/>">
<bean:write name="list" property="UIDisplay"/>
</option>
<option value="<bean:write name="list" property="value"/>">
<bean:write name="list" property="UIDisplay"/>
</option>
    </logic:iterate>
    </select>
</logic:equal>

<logic:notEqual name="attr" property="value.attrType" value="6">
<input name="txtName" type="text" value="<bean:write name="attr" property="value.attrValue"/>"/>
</logic:notEqual>
</logic:iterator>


补充:
logic:iterator迭代HashMap

1、HashMap中存放普通的包装对象
Map<Integer, String> map=new HashMap<Integer, String>();
int index = 0;
map.put(++index, "Lily");
map.put(++index, "Lucy");

request.setAttribute("map", map);

<logic:iterate iterate id="var" name="map">
  <bean:write name="var" property="key"/>
  <bean:write name="var" property="value"/>
</logic:iterate>


2、HashMap中存放自定义的对象
//描述属性的对象,包含该对象的几个特征字段
AttributeDesc desc = new AttributeDesc(); 
Map<Integer, AttributeDesc > map=new HashMap<Integer, AttributeDesc >();
int index = 0;
map.put(++index, new String[]{"Lily", "Lucy"});

request.setAttribute("map", map);

<logic:iterate iterate id="var" name="map">
  <bean:write name="var" property="key"/>
  <bean:write name="var" property="value.属性名"/>
</logic:iterate>

3、HashMap中存放数组
int length = length of array;
String[] array = new String[length];

Map<Integer, String[]> map=new HashMap<Integer, String[]>();
int index = 0;
map.put(++index, new String[]{"Lily", "Lucy"});

request.setAttribute("map", map);

<logic:iterate id="var" name="map">
  <bean:write name="var" property="key"/>
  <logic:iterate id="str" name="var" property="value">
    <bean:write name="str"/>
  </logic:iterate>   
</logic:iterate>

4、HashMap中存放List对象
List list = new ArrayList();

Map<Integer, List> map=new HashMap<Integer, List>();
int index = 0;
list.add("lucy");

map.put(++index, list);

request.setAttribute("map", map);

<logic:iterateid="user"name="map">
  <bean:write name="user" property="key"/>
  <logic:iterate name="user" property="value>
    <bean:write name="value.属性名"/>
  </logic:iterate>   
</logic:iterate>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics