Struts2标签语法

    添加时间:2013-7-29 点击量:

    本文翻译自struts2文档,增长了本身的测试项目组


    测试景象


    浏览器是Firefox22.0


    tomcat6


    标签语法


    创建动态输入域


    struts2的标签用来显示动态数据。创建一个输入域显示属性值,我们将传递字符串postalCode到textfield标签。



    <s:textfield name=postalCode/>





    若是在value stack中有一个属性postalCode,那么它的值将被set到输入域。
    当这个输入域提交到框架后,控件的值将被回填到postalCode属性中。


    The tags are designed to display dynamic data. To create a input field that
    displays the property postalCode, wed pass the String postalCode to the
    textfield tag.


    有事,我们动态的传递数据给标签。例如,我们用一个标签显示输入域的内容,同时我们从应用消息资料中获得标签的内容。按照此请求,框架将申明标签属性中的表达式,从而使我们可以在运行时动态的将数据插入到标签属性中。转义表达式就是%{
    ... }。任何表达式中的文本都将被策画。


    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 the applications 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.


    应用表达式设置标签值


    表达式说话(OGNL)使得我们可以调用办法并策画属性的值。getText()办法由ActionSupport类供给,ActionSupport是大多半Action类的父类。只要Action在(值)栈里,我们就可以经由过程表达式调用它所有办法,包含getText()。



    <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 an expression, including getText.


    非字符型属性


    HTTP和谈是基于文本的,但一些标签中包含有非文本类的属性,例如bool或者int类型。为了非字符型属性可以直接应用,框架策画所有非字符型类型作为表达式。在这种景象下,你不须要应用转义标识即%{ ... }。 (然则,若是你对峙这么做,框架将会忽视这些内容)


    例如jsp页面中输入



      <body>

        <h3>Ognl Test</h3>

        <s:property value=%{50}/><!-- 这一项中的50是作为字符类型的吗答案是否定的看下面 -->

        </br>

        <s:property value=%{12}/>

        </br>

        <s:property value=50/><!-- 这一项中的50经过策画吗答案是必然的并且是被认为整型看下面 -->

        </br>

        <s:property value=12/>

        <br>

        <s:property value=%{true}/><!-- 这一项中的表达式认为true是一个字符串而不是bool类型 -->

        </br>

        <s:if test=%{true}><!-- 在这里true被认为是bool类型进行断定 -->

        test result is true

        </s:if>

        </br>

        <s:if test=true><!-- 如许也是正确的 -->

        test result is true

        </s:if>

      </body>







    获得的成果是


    50
    
    2
    50
    2
    true
    test result is true
    test result is true







    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.)


    下面的标签中,因为属性multiple 映射到一个bool属性,框架不会把这个值作为字符串应用,这里的值会被作为表达式进行策画并主动转换为一个bool类型。



    <s: 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.


    因为很轻易忘怀那些属性时字符型或者非字符型,是以你仍可以应用转义标识,以下两种体式格式都是正确的。



    <s: key=state.label name=state multiple=true/>

    <s: key=state.label name=state multiple=%{true}/>







    而下面的内容则是经由过程属性来策画表达式的bool值,两种体式格式都正确



    <s: key=state.label name=state multiple=allowMultiple/>

    <s: key=state.label name=state multiple=%{allowMultiple}/>







    Since its easy to forget which attributes are String and which are non-String, you can still use the escape notation.


    值是对象(Value is Object)


    在大多半景象下,标签的name属性告诉框架该当填充哪一个属性的值,是以标签的value这个属性是主动填充的。然则,若是须要对value直接设置值,那么请重视value是一个对象而非字符串


    因为value不是字符串,无论什么值传递给value都被作为表达式进行策画-而不是字符串的字面值。


    下面的例子中,数字都是被作为对象策画后才进行显现的。



    <s:property value=%{12}/>


    <s:property value=50/>



     






    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.


    若是一个文本域标签的value属性输入一个值ca,那么框架会寻找属性名称getCa,凡是,这种并不是我们想要的,我们做的是给文本域的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/>


    正确的用法



    <s:textfield key=state.label name=state value=%{ca} />







    其他办法


    下面这种在官方文档中提到,但经测试成果是不正确的



    <s:textfield key=state.label name=state value=ca/>


    似乎下面的写法也是正确的,但官方文档中并未提到,不建议如许做。



    <s:textfield key=state.label name=state value=ca/>







    以上获得的成果分别如下



    总结


    标签属性在进行策画式应用以下3项原则


    1.所有字符串属性的类型应用%{ ... }进行转义


    2.所有非字符串属性的类型不会被转义,但策画时直接作为表达式的(一项目组)


    3.第2项的例外是,非字符按属性应用%{}进行转义,那么标示符将被忽视掉,而此中的内容会被策画。


    例如



    <!-- 在这里true被认为是bool类型进行断定,也就是转义标示符”%{}”被忽视掉了,但true被正确的策画了 -->

    <s:if test=%{true}>

        test result is true

    </s:if>







    请重视关于altSyntax的选项,当标签属性value在进行策画式它可以改变的。拜见AltSyntax的内容。


    Boiled down, the tag attributes are evaluated using three rules.



    1. All String attribute types are parsed for the %{ ... } notation.

    2. All non-String attribute types are not parsed, but evaluated directly as an expression

    3. 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.


    Please remember about altSyntax option that can change when value is evaluated as an expression - Alt Syntax


    禁用的属性名称



    parameters 



    application 



    session 



    struts 



    request 



    servletRequest 



    1. servletResponse







    下面的代码将失效



    <s:iterator value=parameters/>








    public class MyAction {

     

        private String[] parameters;

     

        public String[] getParameters() {

            return parameters;

        }

    }







    表达式说话标识







    <p>Username: ¥{user.username}</p>


    所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
    分享到: