Struts2中的OGNL详解Word格式.docx

上传人:b****6 文档编号:17260290 上传时间:2022-11-29 格式:DOCX 页数:18 大小:23.87KB
下载 相关 举报
Struts2中的OGNL详解Word格式.docx_第1页
第1页 / 共18页
Struts2中的OGNL详解Word格式.docx_第2页
第2页 / 共18页
Struts2中的OGNL详解Word格式.docx_第3页
第3页 / 共18页
Struts2中的OGNL详解Word格式.docx_第4页
第4页 / 共18页
Struts2中的OGNL详解Word格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

Struts2中的OGNL详解Word格式.docx

《Struts2中的OGNL详解Word格式.docx》由会员分享,可在线阅读,更多相关《Struts2中的OGNL详解Word格式.docx(18页珍藏版)》请在冰豆网上搜索。

Struts2中的OGNL详解Word格式.docx

在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:

reg.agerange=国际化资源信息:

年龄必须在${min}同${max}之间。

在Struts2框架的配置文件中引用OGNL表达式,例如:

1.<

validators>

2. 

field 

name="

intb"

>

3. 

field-validator 

type="

int"

4. 

param 

min"

10<

/param>

5. 

max"

100<

6. 

message>

BAction-test校验:

数字必须为${min}为${max}之间!

/message>

7. 

/field-validator>

8. 

/field>

9.<

/validators>

下面通过代码对OGNL有更深的了解:

action类OgnlAction.java:

1.package 

com.tjcyjd.test.action;

3.import 

java.util.Date;

4.import 

java.util.LinkedList;

5.import 

java.util.List;

7.import 

javax.servlet.http.HttpServletRequest;

9.import 

org.apache.struts2.ServletActionContext;

10.import 

org.apache.struts2.convention.annotation.Action;

11.import 

org.apache.struts2.convention.annotation.Namespace;

12.import 

org.apache.struts2.convention.annotation.ParentPackage;

13.import 

org.apache.struts2.convention.annotation.Result;

14.import 

org.apache.struts2.convention.annotation.Results;

15.import 

org.springframework.stereotype.Controller;

16. 

17.import 

com.opensymphony.xwork2.ActionContext;

18.import 

com.opensymphony.xwork2.ActionSupport;

19. 

20.@Controller 

21.@Namespace("

/test"

22.@ParentPackage("

struts-default"

23.@Results( 

@Result(name 

"

success"

location 

/other_test/showognl.jsp"

), 

24. 

fail"

/bbs/admin_login.jsp"

25. 

input"

}) 

26.public 

class 

OgnlAction 

extends 

ActionSupport 

27. 

private 

static 

final 

long 

serialVersionUID 

-1494290883433357310L;

28. 

List<

Person>

persons;

29. 

30. 

@Action("

ognlTest"

31. 

public 

String 

ognlTest() 

throws 

Exception 

32. 

// 

获得ActionContext实例,以便访问Servlet 

API 

33. 

ActionContext 

ctx 

ActionContext.getContext();

34. 

存入application 

35. 

ctx.getApplication().put("

application信息"

);

36. 

保存session 

37. 

ctx.getSession().put("

seesion信息"

38. 

保存request信息 

39. 

HttpServletRequest 

request 

ServletActionContext.getRequest();

40. 

request.setAttribute("

request信息"

41. 

为persons赋值 

42. 

persons 

new 

LinkedList<

();

43. 

Person 

person1 

Person();

44. 

person1.setName("

pla1"

45. 

person1.setAge(26);

46. 

person1.setBirthday(new 

Date());

47. 

persons.add(person1);

48. 

49. 

person2 

50. 

person2.setName("

pla2"

51. 

person2.setAge(36);

52. 

person2.setBirthday(new 

53. 

persons.add(person2);

54. 

55. 

person3 

56. 

person3.setName("

pla3"

57. 

person3.setAge(16);

58. 

person3.setBirthday(new 

59. 

persons.add(person3);

60. 

61. 

return 

SUCCESS;

62. 

63. 

64. 

65. 

getPersons() 

66. 

67. 

68. 

69. 

void 

setPersons(List<

persons) 

70. 

this.persons 

71. 

72.} 

jsp页面showognl.jsp:

%@ 

page 

language="

java"

contentType="

text/html;

charset=utf-8"

pageEncoding="

utf-8"

%>

3.<

taglib 

prefix="

s"

uri="

/struts-tags"

4.<

!

DOCTYPE 

html 

PUBLIC 

-//W3C//DTD 

XHTML 

1.0 

Transitional//EN"

http:

//www.w3.org/TR/ 

xhtml1/DTD/xhtml1-transitional.dtd"

6.<

xmlns="

//www.w3.org/1999/xhtml"

8.<

head>

9. 

10. 

title>

Struts2 

OGNL 

演示<

/title>

11. 

12.<

/head>

13. 

14.<

body>

15. 

h3>

访问OGNL上下文和Action上下文<

/h3>

17. 

18. 

-使用OGNL访问属性值-->

20. 

p>

parameters:

property 

value="

#parameters.msg"

/p>

21. 

22. 

request.msg:

#request.msg"

23. 

session.msg:

#session.msg"

26. 

application.msg:

#application.msg"

attr.msg:

#attr.msg"

hr 

用于过滤和投影(projecting)集合<

年龄大于20<

ul>

-判断年龄-->

iterator 

persons.{?

20}"

li>

name"

年龄:

age"

/li>

/s:

iterator>

/ul>

姓名为pla1的年龄:

}.{age}[0]"

构造Map<

set 

foobar"

#{'

}"

The 

value 

of 

key 

foo1"

is 

#foobar['

]"

h4>

%符号的用法<

/h4>

不使用%:

url 

使用%:

%{#foobar['

]}"

72. 

req"

scope"

73. 

request.getSession().setAttribute("

sess"

session 

74. 

request.getSession().getServletContext().setAttribute("

app"

75. 

aplication 

76. 

77. 

1.通过ognl表达式获取 

属性范围中的值 

78. 

br>

79. 

#request.req"

80. 

br 

81. 

#session.sess"

82. 

83. 

#application.app"

84. 

85. 

hr>

86. 

87. 

2.通过<

span 

style="

background-color:

#fafafa;

ognl表达式创建list 

集合 

,并且遍历出集合中的值 

88. 

89. 

list"

{'

eeeee'

'

ddddd'

ccccc'

bbbbb'

aaaaa'

set>

90. 

#list"

var="

o"

91. 

-- 

${o 

}<

br/>

-->

92. 

93. 

94. 

95. 

96. 

97. 

98. 

3.通过ognl表达式创建Map 

99. 

100. 

map"

101. 

1'

2'

3'

4'

5'

102. 

#map"

103. 

${o.key 

}->

${o.value 

104. 

#o.key"

->

#o.value"

105. 

key"

value"

106. 

107. 

108. 

109. 

110. 

4.通过ognl表达式 

进行逻辑判断 

111. 

112. 

if 

test="

aa'

in 

aaa'

bbb'

113. 

aa 

在 

集合{'

}中;

114. 

if>

115. 

else>

116. 

不在 

117. 

118. 

119. 

#request.req 

not 

120. 

不 

集合list中;

121. 

122. 

123. 

124. 

125. 

126. 

127. 

128. 

5.通过ognl表达式 

的投影功能进行数据筛选 

129. 

130. 

list1"

{1,2,3,4,5}"

131. 

#list1.{?

#this>

2}"

132. 

#list.{?

2}:

在list1集合迭代的时候,从中筛选出当前迭代对象>

2的集合进行显示 

133. 

134. 

135. 

136. 

137. 

6.通过ognl表达式 

访问某个类的静态方法和值 

138. 

139. 

@java.lang.Math@floor(32.56)"

140. 

141. 

@com.rao.struts2.action.OGNL1Action@aa"

142. 

143. 

144. 

145. 

7.ognl表达式 

迭代标签 

详细 

146. 

147. 

list2"

148. 

bb'

cc'

dd'

ee'

ff'

gg'

hh'

ii'

jj'

149. 

table 

border="

1"

150. 

tr>

151. 

td>

索引 

/td>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1