The framework provides a tag library decoupled from the view
technology. In this section, we describe each tag in general terms,
such as the attributes it supports, what the behaviors are, and so
forth. Most tags are supported in all template languages (see JSP Tags, Velocity Tags, and FreeMarker Tags),
but some are currently only specific to one language. Whenever a tag
doesn't have complete support for every language, it is noted on the
tag's reference page.
The types of tags can be broken in to two types: generic and UI.
Besides function and responsibility, the biggest difference between the
two is that the HTML tags support templates and themes.
In addition to the general tag reference, we also provide examples for
using these generic tags in each of the support languages.
Tag類型能被分成兩種:Generic和UI。除了功能和指責(zé),他們之間的最大不同在預(yù)HTML標(biāo)簽支持模板和主題。除了通用的標(biāo)簽參考之外,我們提供了例子。
Be sure to read the Tag Syntax document to learn how tag attribute syntax works.
The tags are designed to display dynamic data. To create a input
field that displays the property "postalCode", we'd pass the String
"postalCode" to the textfield tag.
標(biāo)簽被設(shè)計(jì)用來展示動(dòng)態(tài)數(shù)據(jù)。
<s:textfield name="postalCode"/>
If there is a "postalCode" property on the value stack, its value
will be set to the input field. When the field is submitted back to the
framework, the value of the control will be set back to the
"postalCode" property.
如果value棧里有一個(gè)postalCode屬性,他的值將被放到input字段,當(dāng)字段被提交回框架,值將被設(shè)置到postalCode屬性(注:有點(diǎn)服務(wù)端和客戶端綁定的味道)
Sometimes, we want to pass the dynamic data to a tag. For example,
we might want to display a label with the input field, and we might
want to obtain the label from the application's messages resources.
Accordingly, the framework will parse expressions found in the tag
attributes, so that we can merge dynamic data into the tag attributes
at runtime. The expression escape sequence is "%{ ... }". Any text
embedded in the escape sequence is evalulated as an expression.
有時(shí),我們象傳遞動(dòng)態(tài)數(shù)據(jù)到tag。例如,我們象顯示Label和input在一起,我們從系統(tǒng)的資源文件獲取Lable,框架將解析表達(dá)式,所以我們能把動(dòng)態(tài)數(shù)據(jù)整合到標(biāo)簽屬性上。表達(dá)式樣子是“%{}”.在大括弧里的表達(dá)式將被計(jì)算。
<s:textfield key="postalCode.label" name="postalCode"/>
The expression language (OGNL) lets us call methods and evaluate properties. The method getText
is provided by ActionSupport, which is the base class for most Actions.
Since the Action is on the stack, we can call any of its methods from
an expression, including getText.
Non-String Attributes
The HTTP protocol is text-based, but some tags have non-String attribute types, like bool or int. To make using non-String attributes intuitative, the framework evaulates all
non-String attributes as an expression. In this case, you do not need
to use the escape notation. (But, if you do anyway , the framework will
just strip it off.)
<s:select key="state.label" name="state" multiple="true"/>
Since the attribute multiple maps to a boolean property,
the framework does not interpret the value as a String. The value is
evaluated as an expression and automtically converted to a boolean.
Since it's easy to forget which attributes are String and which are non-String, you can still use the escape notation.
<s:select key="state.label" name="state" multiple="%{true}"/>
<s:select key="state.label" name="state" multiple="allowMultiple"/>
<s:select key="state.label" name="state" multiple="%{allowMultiple}"/>
value is an Object!
Most often, the value attribute is set automatically, since name attribute usually tells the framework which property to call to set the value. But, if there is a reason to set the value directly, be advised that value is an Object NOT a String.
Since value is not a String, whatever is passed to value is evaluated as an expression - NOT a String literal.
<s:textfield key="state.label" name="state" value="CA"/>
If a textfield is passed the value attribute "CA", the framework will look for a property named getCa.
Generally, this is not what we mean. What we mean to do is pass a
literal String. In the expression language, literals are placed within
quotes
<s:textfield key="state.label" name="state" value="%{'CA'}" />
Another approach would be to use the idiom value="'CA'", but, in this case, using the expression notation is recommended.
Boiled down, the tag attributes are evaluated using three rules.
- All String attribute types are parsed for the "%{ ... }" notation.
- All non-String attribute types are not parsed, but evaluated directly as an expression
- The exception to rule #2 is that if the non-String attribute uses the escape notion "{%{}", the notation is ignored as redundant, and the content evaluated.
Expression Language Notations
<p>Username: ${user.username}</p>
|
A JavaBean object in a standard context in Freemarker, Velocity, or JSTL EL (Not OGNL). |
<s:textfield name="username"/>
|
A username property on the Value Stack. |
<s:url id="es" action="Hello"> <s:param name="request_locale"> es </s:param> </s:url> <s:a href="%{es}">Espanol</s:a>
|
Another way to refer to a property placed on the Value Stack. |
<s:property name="#session.user.username" />
|
The username property of the User object in the Session context. |
<s:select label="FooBar" name="foo" list="#{'username':'trillian', 'username':'zaphod'}" />
|
A static Map, as in put("username","trillian"). |