OGNL在Struts2中的应用Word文档下载推荐.docx
- 文档编号:21725437
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:17
- 大小:29.72KB
OGNL在Struts2中的应用Word文档下载推荐.docx
《OGNL在Struts2中的应用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《OGNL在Struts2中的应用Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
调用上一步返回的结果字符串的toCharArray()方法。
提取返回的结果数组的第一个字符。
获取字符的numericValue属性,该字符是一个Character对象,Character类有一个getNumericValue()方法。
调用结果Integer对象的toString()方法。
上面的例子只是用来得到一个对象的值,OGNL也可以用来去设置对象的值。
当把上面的表达式传入Ognl.setValue()方法将导致InappropriateExpressionException,因为链的最后的部分(toString())既不是一个属性的名字也不是数组的某个元素。
了解了上面的语法基本上可以完成绝大部分工作了。
OGNL表达式
1)常量:
字符串:
“hello”字符:
‘h’数字:
除了像java的内置类型int,long,float和double,Ognl还有如例:
0.01B,相当于java.math.BigDecimal,使用’b’或者’B’后缀。
100000H,相当于java.math.BigInteger,使用’h’或’H’后缀。
2)属性的引用例如:
user.name
3)变量的引用例如:
#name
4)静态变量的访问使用@class@field
5)静态方法的调用使用@class@method(args),如果没有指定class那么默认就使用java.lang.Math.
6)构造函数的调用例如:
newjava.util.ArrayList();
其它的Ognl的表达式可以参考Ognl的语言手册。
OGNL的性能
OGNL,或者说表达式语言的性能主要又两方面来决定,一个就是对表达式的解析(Parser),另一个是表达式的执行,OGNL采用javaCC来完成parser的实现,在OGNL2.7中又对OGNL的执行部分进行了加强,使用javasisit来JIT(Just-In-Time)的生成bytecode来完成表达式的执行。
Ognl给这个功能的名字是:
OGNLExpressionCompilation。
基本的使用方法是:
SimpleObjectroot=newSimpleObject();
OgnlContextcontext=
(OgnlContext)Ognl.createDefaultContext(null);
Nodenode=
(Node)OpileExpression(context,root,"
user.name"
);
StringuserName=(String)node.getAccessor().get(context,root);
实践证明:
OGNL非常接近java直接调用的时间。
表达式语言主要有以下几大好处:
1)避免(MyType)request.getAttribute()和myBean.getMyProperty()之类的语句,使页面更简洁;
2)支持运算符(如+-*/),比普通的标志具有更高的自由度和更强的功能;
3)简单明了地表达代码逻辑,使用代码更可读与便于维护。
Struts2中的表达式语言
Struts2支持以下几种表达式语言:
1)OGNL(Object-GraphNavigationLanguage),可以方便地操作对象属性的开源表达式语言;
2)JSTL(JSPStandardTagLibrary),JSP2.0集成的标准的表达式语言;
3)Groovy,基于Java平台的动态语言,它具有时下比较流行的动态语言(如Python、Ruby和Smarttalk等)的一些起特性;
4)Velocity,严格来说不是表达式语言,它是一种基于Java的模板匹配引擎,具说其性能要比JSP好。
Struts2默认的表达式语言是OGNL,原因是它相对其它表达式语言具有下面几大优势:
支持对象方法调用,如xxx.doSomeSpecial();
支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名|
值名],例如:
@java.lang.String@format('
foo%s'
'
bar'
)或@tutorial.MyConstant@APP_NAME;
支持赋值操作和表达式串联,如price=100,discount=0.8,calculatePrice(),这个表达式会返回80;
访问OGNL上下文(OGNLcontext)和ActionContext;
操作集合对象。
Struts2中ONGL的使用示例
index.html
Html代码
1.<
span
style="
font-size:
large;
"
>
<
html>
2.
head>
3.
meta
http-equiv="
Content-Type"
content="
text/html;
charset=UTF-8"
/>
4.
refresh"
0;
url=ognl.action"
5.
/head>
6.<
/html>
/span>
User.java
Java代码
package
com.javacrazyer.web.action;
3.import
java.util.Date;
6.public
class
User
{
7.
private
Integer
id;
8.
String
loginname;
9.
Double
score;
10.
Boolean
gender;
11.
Character
cha;
12.
Date
birthday;
13.
14.
public
User(){}
15.
16.
User(Integer
id,
loginname,
score,
gender,
17.
cha,
birthday)
18.
this.id
=
19.
this.loginname
20.
this.score
21.
this.gender
22.
this.cha
23.
this.birthday
24.
}
25.
getId()
26.
return
27.
28.
void
setId(Integer
id)
29.
30.
31.
getLoginname()
32.
33.
34.
setLoginname(String
loginname)
35.
36.
37.
getScore()
38.
39.
40.
setScore(Double
score)
41.
42.
43.
getGender()
44.
45.
46.
setGender(Boolean
gender)
47.
48.
49.
getCha()
50.
51.
52.
setCha(Character
cha)
53.
54.
55.
getBirthday()
56.
57.
58.
setBirthday(Date
59.
60.
61.
62.
info()
63.
[birthday="
+
birthday
cha="
cha
gender="
64.
gender
id="
id
loginname="
loginname
65.
score="
score
]"
;
66.
67.}<
OGNLAction.java
4.import
java.util.ArrayList;
5.import
6.import
java.util.HashMap;
7.import
java.util.LinkedHashSet;
8.import
java.util.List;
9.import
java.util.Map;
10.import
java.util.Set;
12.import
org.apache.struts2.ServletActionContext;
14.import
com.opensymphony.xwork2.ActionContext;
15.import
com.opensymphony.xwork2.ActionSupport;
17.public
OGNLAction
extends
ActionSupport
static
final
long
serialVersionUID
-2554018432709689579L;
pwd;
user;
Set<
String>
courseSet;
List<
list;
Map<
String,String>
map;
User>
userList;
execute()
throws
Exception{
xkkkkkkkkkkkkkkkkkkkkkkkk"
this.user
new
User(123,
wrr"
88.9,
true,
'
B'
Date());
this.courseSet
LinkedHashSet<
();
this.courseSet.add("
corejava"
JSP/Servlet"
S2SH"
this.list
ArrayList<
(this.courseSet);
this.map
HashMap<
String,
this.map.put("
x"
xxx"
y"
yyy"
z"
zzz"
ActionContext
context
ActionContext.getContext();
context.put("
uname"
cheney"
inte"
Integer.valueOf(888888));
user2"
xxk"
Date()));
this.userList
this.userList.add(new
User(1,
zs"
48.9,
D'
User(2,
ls"
68.1,
C'
User(3,
ww"
78.2,
false,
User(4,
zl"
88.3,
A'
//-----------------------------------------------------------------
//推荐方式:
不会跟Servlet
API耦合
reqAtt"
往ActionContext中put的属性"
context.getSession().put("
sesAtt"
往ActionContext.getSession()中put的属性"
context.getApplication().put("
appAtt"
往ActionContext.getApplication()中put的属性"
ServletActionContext.getRequest().setAttribute("
reqAtt2"
Request作用域中的属性"
ServletActionContext.getRequest().getSession().setAttribute("
sesAtt2"
Session作用域中的属性"
ServletActionContext.getServletContext().setAttribute("
appAtt2"
Application作用域中的属性"
SUCCESS;
67.
68.
69.
getAppName(){
70.
这是OGNL的使用示例代码"
71.
72.
73.
74.
75.
76.
77.
78.
79.
getPwd()
80.
81.
82.
setPwd(String
pwd)
83.
this.pwd
84.
85.
86.
getUser()
87.
88.
89.
90.
setUser(User
user)
91.
92.
93.
94.
getCourseSet()
95.
96.
97.
98.
setCourseSet(Set<
S
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- OGNL Struts2 中的 应用