zoukankan      html  css  js  c++  java
  • struts2的配置文件为什么可以使用${}符号?

    转自:https://www.cnblogs.com/sharpest/p/6030265.html

    一.#符号的用途一般有三种。

     

    “#”主要有三种用途:

    1. 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性:

    parameters 包含当前HTTP请求参数的Map #parameters.id[0]作用相当于request.getParameter ("id")

    request 包含当前HttpServletRequest的属性(attribute)的Map #request.userName相当于request.getAttribute("userName")

    session 包含当前HttpSession的属性(attribute)的Map #session.userName相当于session.getAttribute("userName")

    application 包含当前应用的ServletContext的属性(attribute)的Map #application.userName相当于application.getAttribute("userName")

    attr 用于按request > session > application顺序访问其属性(attribute) #attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为止

    2)用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}。

    3)用来构造Map,例如示例中的#{’foo1′:’bar1′, ’foo2′:’bar2′}。

     

    二.%符号

    %符号的用途是在标志的属性为字符串类型时,转换为计算OGNL表达式的值。如下面的代码所示:
    构造Map
    <s:set name=”foobar” value=”#{’foo1′:’bar1′, ‘foo2′:’bar2′}” />

    The value of key “foo1″ is <s:property value=”#foobar['foo1']” />

    不使用%:<s:url value=”#foobar['foo1']” />

    使用%:<s:url value=”%{#foobar['foo1']}” />

    三.$符号

    $符号主要有两个方面的用途。

    在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。

    在Struts 2框架的配置文件中引用OGNL表达式,例如下面的代码片断所示:

    <validators>

    <field name=”intb”>

    <field-validator type=”int”>

    <param name=”min”>10</param>

    <param name=”max”>100</param>

    <message>BAction-test校验:数字必须为${min}为${max}之间!</message>

    </field-validator>

    </field>

    </validators>

     

    2、动态的结果集(dynamic result)

    <struts>

    <constant name="struts.devMode" value="true" />

    <package name="user" namespace="/user" extends="struts-default">

    <action name="user" class="com.bjsxt.struts2.user.action.UserAction">

      <result>${r}</result>

    </action>

    </package>

    </struts>

    注:${}:作用是用于在配置文件中从Value stack(值栈)中取值。

    例如:${r} 表示从Value stack(值栈)中取rAction的(成员属性)的值。注意这个成员属性必需存在

    get/set方法。(注:此处的${}不是el表达式。)

    Action类中指定了r的值。这样就动态确定了Result的值

    public class UserAction extends ActionSupport {

    private int type;

    private String r;

    public String getR() {

    return r;

    }

    public void setR(String r) {

    this.r = r;

    }

    public int getType() {

    return type;

    }

    public void setType(int type) {

    this.type = type;

    }

    @Override

    public String execute() throws Exception {

    if(type == 1) r="/user_success.jsp";

    else if (type == 2) r="/user_error.jsp";

    return "success";

    }

    }

    四、@符号

    package com. wjt276.struts2.ognl;

    public class S {

    public static String STR = "STATIC STRING";

    public static String s() {

    return "static method";

    }

    }

    <li>访问静态方法:<s:property value="@com.wjt276.struts2.ognl.S@s()"/></li>

    <li>访问静态属性:<s:property value="@com.wjt276.struts2.ognl.S@STR"/></li>

    <li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li> // 两个@符号

    小结:

    $ # % @

    a) $ 用于i18n和Struts配置文件

    b) # 取得ActionContext的值

    c) % 将原本的文本属性解析为ognl,对于本来就是ognl的属性不起作用

    d) @ 用于静态方法或属性的调用

  • 相关阅读:
    如何写一个使用Web Service的IOS应用
    iPad定制相机界面
    IOS Quartz 2D 学习(1)
    cocoa Shallow Copy与Deep Copy
    sqlite3_prepare_v2返回1
    IOS 监听相机对焦事件
    UIImageView添加响应事件无响应
    二、Mongodb常用命令
    三、Mongodb Java中的使用
    多测师肖老师__第二个月python安装和pycharm安装
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/9109209.html
Copyright © 2011-2022 走看看