| « | October 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:15 评论数量:30 留言数量:0 访问次数:94693 建立时间:2006年4月26日 |

| |
|
Struts 的 html 标签 和 el 语法对map值的引用 心得体会, 软件技术
NaddyLee 发表于 2006/4/26 11:08:19 |
| 用法:
<html:text property="valuesMap(${attrDef.key})" value="${subAttr.valuesMap[attrDef.key]}" size="80"/>
EL 中:expr-a.identifier-b 等价于 expr-a["identifier-b"]expr-a[expr-b]的情况下:先对 expr-a 求值为 value-a,对 expr-b 求值为 value-b;
然后:当 value-a 或 value-b = null,返回null;当 value-a 是一个Map,返回 value-a.get(value-b) 或 null;当 value-a 是 数组 或 List,强制转换 value-b 到 int,返回 value-a.get(value-b) 或 Array.get(value-a, value-b);当 value-a 是 javabean,强制转换 value-b 到 String,若 value-b 属性可读,返回 get 方法的返回值(value-a.getValue-b()),如果get方法抛出异常,返回一个错误
Struts中的Map_Backed ActionForm:public FooForm extends ActionForm { private final Map values = new HashMap(); public void setValue(String key, Object value) { values.put(key, value); } public Object getValue(String key) { return values.get(key); }}引用语法:mapname(keyname)例子:<html:text property="value(foo)"/> : getValue("foo")name = "value(foo-1)";<html:text property="<%= name %>"/> : getValue("foo-1")list_backed properties:public FooForm extends ActionForm { private final List values = new ArrayList(); public void setValue(int key, Object value) { values.set(key, value); } public Object getValue(int key) { return values.get(key); }}引用语法:listname[index]附:EL:http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.htmlThe JSP expression language unifies the treatment of the . and [] operators. expr-a.identifier-b is equivalent to expr-a["identifier-b"]; that is, the expression expr-b is used to construct a literal whose value is the identifier, and then the [] operator is used with that value.
To evaluate expr-a[expr-b], evaluate expr-a into value-a and evaluate expr-b into value-b. If either value-a or value-b is null, return null.
If value-a is a Map, return value-a.get(value-b). If !value-a.containsKey(value-b), then return null.
If value-a is a List or array, coerce value-b to int and return value-a.get(value-b) or Array.get(value-a, value-b), as appropriate. If the coercion couldn't be performed, an error is returned. If the get call returns an IndexOutOfBoundsException, null is returned. If the get call returns another exception, an error is returned.
If value-a is a JavaBeans object, coerce value-b to String. If value-b is a readable property of value-a, then return the result of a get call. If the get method throws an exception, an error is returned.
struts中,对map值的引用使用圆括号,如上文的valuesMap(***);
Map-backed ActionForms
The DynaActionForm classes offer the ability to create ActionForm beans at initialization time, based on a list of properties enumerated in the Struts configuration file. However, many HTML forms are generated dynamically at request time. Since the properties of these forms' ActionForm beans are not all known ahead of time, we need a new approach.
Struts allows you to make one or more of your ActionForm's properties' values a Map instead of a traditional atomic object. You can then store the data from your form's dynamic fields in that Map. Here is an example of a map-backed ActionForm class: public FooForm extends ActionForm {
private final Map values = new HashMap();
public void setValue(String key, Object value) {
values.put(key, value);
}
public Object getValue(String key) {
return values.get(key);
}
}
In its corresponding JSP page, you can access objects stored in the values map using a special notation: mapname(keyname). The parentheses in the bean property name indicate that:
The bean property named mapname is indexed using Strings (probably backed by a Map), and that
Struts should look for get/set methods that take a String key parameter to find the correct sub-property value. Struts will, of course, use the keyname value from the parentheses when it calls the get/set methods.
Here is a simple example: <html:text property="value(foo)"/>
This will call the getValue method on FooForm with a key value of "foo" to find the property value. To create a form with dynamic field names, you could do the following:
<%
for (int i = 0; i < 10; i++) {
String name = "value(foo-" + i + ")";
%>
<html:text property="<%= name %>"/>
<br/>
<%
}
%>
Note that there is nothing special about the name value. Your map-backed property could instead be named property, thingy, or any other bean property name you prefer. You can even have multiple map-backed properties on the same bean.
In addition to map-backed properties, you can also create list-backed properties. You do so by creating indexed get/set methods on your bean: public FooForm extends ActionForm {
private final List values = new ArrayList();
public void setValue(int key, Object value) {
values.set(key, value);
}
public Object getValue(int key) {
return values.get(key);
}
}
In your presentation pages, you access individual entries in a list-backed property by using a different special notation: listname[index]. The braces in the bean property name indicate that the bean property named listname is indexed (probably backed by a List), and that Struts should look for get/set methods that take an index parameter in order to find the correct sub-property value.
While map-backed ActionForms provide you with more flexibility, they do not support the same range of syntax available to conventional or DynaActionForms. You might have difficulty referencing indexed or mapped properties using a map-backed ActionForm. The validwhen validator (since Struts 1.2.1) also does not support map-backed ActionForms. |
|
|