struts2总结(二)--Result和OGNL表达式

4 Result

Result是Action执行结束之后返回的结果,result包含name和type属性,前者是返回的字符串,后者指定跳转的类型,默认为dispatcher。

4.1 Result常用的四中类型

  1. dispatcher, 从一个Action里面服务器内部跳转到一个页面中, r1–>r1.jsp
  2. chain, 从一个Action里面服务器内部跳转到另一个Action中,r2–>r2.jsp
  3. redirect, 从一个Action里面客户端重定向到一个页面中, r3->r1->r1.jsp
  4. redirectAction, 从一个Action里面客户端重定向到另一个Action中, r4->r2->r2.jsp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <struts>
    <constant name="struts.devMode" value="true" />
    <package name="resultTypes" namespace="/r" extends="struts-default">
    <action name="r1">
    <result type="dispatcher">/r1.jsp</result>
    </action>
    <action name="r2">
    <result type="redirect">/r2.jsp</result>
    </action>
    <action name="r3">
    <result type="chain">r1</result>
    </action>
    <action name="r4">
    <result type="redirectAction">r2</result>
    </action>
    </package>
    </struts>

4.2 全局结果集

1
2
3
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>

在其他任何Action中,不需要单独设置,所有返回mainpage字符串的Action会跳转到main.jsp页面。但是如果在某一个Action定义了mainpage字符串的跳转,全局跳转对这个acting失效。action结果集的优先级高于全局结果集。

4.3 动态结果(了解)

动态结果集表示在struts配置的时候,Action根据类型的不同,动态的决定返回的页面。首先需要在Action中定义两个成员变量,type和r, 其中type用于接收页面传入的参数,r用于保存Action传出的结果。
struts.xml为:

1
2
3
4
5
6
7
8
9
10
<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>

index.jsp:

1
2
3
4
5
6
7
8
9
<body>
动态结果
一定不要忘了为动态结果的保存值设置set get方法
<ol>
<li><a href="user/user?type=1">返回success</a></li>
<li><a href="user/user?type=2">返回error</a></li>
</ol>
</body>

UserAction:

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
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";
}
}

4.4 传递参数

除了request,session,application来向页面传值之外,还可以通过ValueStack和ActionContext。

  • ValueStack是一个接口, ValueStack vs = context.getValueStack()将值从Action传到页面,ActionContext是一个类,ActionContext context = ActionContext.getContext()来传值,在页面中农需要<s:debug />标签来查看结果。
  • 当Action进行跳转的时候,struts2框架会自动的把这个Action对象本身分别放到ValueStack和ActionContext中去。然后struts2将这两个对象传递给页面,在页面中可以通过这两个对象来拿到之前放进去的值。也可以在Action中拿到这两个对象手动放值:
    1
    2
    3
    4
    5
    ActionContext ac = ActionContext.getContext();
    ValueStack vs = ac.getValueStack();
    ac.put("ac","ac");
    User user = new User();
    vs.push(user);

4.4.1 ValueStack

  1. 把一个对象放到值栈之后,文名从这个值栈中是拿不到这个对象的,但是文名可以直接拿到这个对象的属性及其属性值。
  2. 从值栈中拿值的时候,是从值栈的property name这一列来拿的,拿到的是property value之一列的值。
  3. 所以通过值栈来传递参数到页面需要将参数封装到一个对象中,将这个对象放到值栈中。
  4. 生命周期为request
  5. 创建一个新的值栈对象后,都会把这个对象放到ActionCon中去。

4.4.2 ActionContext

  1. 向ActionContext中放值的时候是通过k-v的形式存放的,String-Object键值对,取值同样是通过key拿到value。
  2. struts2框架默认向这个对象里面存放的对象很多,包括request,session,application,ValueStack,parameter等
  3. 每次请求都会创建一个新的ActionContext对象

4.4.3 取值方式

  1. jsp内置对象取值

    1
    2
    3
    <%=request.getAttribute("name") %><br>
    <%=session.getAttribute("name") %><br>
    <%=application.getAttribute("name") %><br>
  2. jstl标签+EL表达式

    1
    2
    3
    4
    5
    6
    ${requestScope.MyName }<br>
    ${requestScope.name } <br>
    ${sessionScope.MyName } <br>
    ${applicationScope.MyName } <br>
    ${MyName } <br>
    ${name } <br>
  3. struts2标签+OGNL表达式<s:property value="OGNL"

  • 从ValueStack中取值:这个value属性的值name,是一个OGNL表达式,表示取一个名字为name的property的值。从ValueStack中取值,如果存在重名的,默认取最上面的值。

    1
    2
    3
    4
    <s:property value="name"/>
    <s:property value="user"/>
    <s:property value="user.id"/>
    <s:property value="user.name"/>
  • 从ActionContext中取值 #

    1
    2
    3
    4
    5
    6
    <s:property value="#action.name"/><br>
    <s:property value="#msg"/><br>
    <s:property value="#user"/><br>
    <s:property value="#user.id"/><br>
    <s:property value="#user.name"/><br>
    <s:property value="#user.age"/><br>

5 OGNL表达式

OGNL:Object-Graph Navigation Language,图对象导航语言,是struts2中默认的表达式语言。

5.1 访问普通方法、属性、构造方法

1
2
3
4
5
6
7
8
9
<li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li>
<li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li>
<li>访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/></li>
<li>访问值栈中对象的普通方法:<s:property value="password.length()"/></li>
<li>访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" /></li>
<li>访问值栈中action的普通方法:<s:property value="m()" /></li>
<hr />
<li>访问普通类的构造方法:<s:property value="new com.struts2.ognl.User(8)"/></li>
<hr />

5.2 访问静态方法、属性

struts2默认的<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>为false,需要配置成true才可以访问静态方法、属性

1
2
3
<li>访问静态方法:<s:property value="@com.struts2.ognl.S@s()"/></li>
<li>访问静态属性:<s:property value="@com.struts2.ognl.S@STR"/></li>
<li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li>

5.3 访问集合元素:

1
2
3
4
5
6
7
8
9
10
11
<li>访问List:<s:property value="users"/></li>
<li>访问List中某个元素:<s:property value="users[1]"/></li>
<li>访问List中元素某个属性的集合:<s:property value="users.{age}"/></li>
<li>访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li>
<li>访问Set:<s:property value="dogs"/></li>
<li>访问Set中某个元素:<s:property value="dogs[1]"/></li>
<li>访问Map:<s:property value="dogMap"/></li>
<li>访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li>
<li>访问Map中所有的key:<s:property value="dogMap.keys"/></li>
<li>访问Map中所有的value:<s:property value="dogMap.values"/></li>
<li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>

5.4 投影和过滤

  • “?#”:过滤所有符合条件的集合
  • “^#”:过滤第一个符合条件的元素
  • “$#”:过滤最后一个符合条件的元素
  • 返回的是一个集合,可以使用索引取得集合中指定的元素。
1
2
3
4
<li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>
<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>

5.5 区分 %{} 和 ${}

  • %{}
    %{}的主要作用是告诉标签的处理类将它包含的字符串按照OGNL表达式处理,类似eval函数
  • ${}
    ${}的主要用于两方面:
    • 用于国际化资源文件中
    • 用于Struts2配置文件中
Donate comment here