OGNL表达式
来源:互联网 发布:舒伯特 幻想曲 知乎 编辑:程序博客网 时间:2024/06/04 20:14
OGNL表达式
OGNL的作用
OGNL的操作对象
ValueStack:ValueStack中默认就是Action的对象,以及Action对象中的属性。
ActionCotnext上下文。
A:Request作用域
B:Session作用域
C:Application作用域
D:Parameter,Request的请求参数
Ognl表达式访问或者定义不同对象时,需要用到不同的符号。
无符号
# 访问ActionContext对象时,需要用到的符号。
{} OGNL中用于定义集合时
#{} OGNL中用于定义Map
%{} OGNL中,将字符串转义成OGNL表达式,并输出转化后的结果
${} struts.xml中,获取ValueStack里成员变量的值。
1:OGNL表达式访问成员变量与成员方法
public int add(int x,int y) {
System.out.println("调用到add这个成员方法");
return x + y;
}
public String accessMember()throwsException {
this.userBean =new UserBean();
this.userBean.setUserid("1");
this.userBean.setUsername("admin");
return "succ";
}
省略UserBean
Jsp:
userBean = <s:propertyvalue="userBean"/>
userid = <s:propertyvalue="userBean.userid"/>
username = <s:propertyvalue="userBean['username']"/>
username = <s:propertyvalue="userBean.getUsername()"/>
<s:propertyvalue="userBean.setUserid(222)"/>
userid = <s:propertyvalue="userBean.userid"/>
add = <s:propertyvalue="add(10,20)"/>
2:输出静态变量与静态方法
public static final double PI = 3.1415;
public static String getSystemTime() {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
public StringaccessStatic() throws Exception {
return "succ";
}
调用静态方法前提:struts.xml中开启常量 = struts.ognl.allowStaticMethodAcces
Jsp:
PI = <s:propertyvalue="@com.action.OgnlAction@PI"/>
系统时间 = <s:propertyvalue="@com.action.OgnlAction@getSystemTime()"/>
3:输出集合
private Map<String, Object>map;
private List<UserBean>userList;
private Set<String> strSet;
//省略set,get
public String accessCollection()throws Exception {
/**
* 集合初始化
*/
this.strSet =new LinkedHashSet<String>();
this.strSet.add("aa");
this.strSet.add("bb");
this.strSet.add("cc");
this.strSet.add("dd");
this.userList =new ArrayList<UserBean>(6);
for (int i = 1; i <= 5; i++) {
UserBean userBean = new UserBean();
userBean.setUserid(String.valueOf(i));
userBean.setUsername("用户名_" + i);
this.userList.add(userBean);
}
this.map =new LinkedHashMap<String,Object>();
this.map.put("key_1","value_1");
this.map.put("key_2", OgnlAction.getSystemTime());
this.map.put("key_3",this.strSet);
this.map.put("key_4",this.userList);
return "succ";
}
Jsp:
strSet = <s:propertyvalue="strSet"/>
大小Size = <s:propertyvalue="strSet.size()"/>
删除元素= <s:propertyvalue="strSet.remove('bb')"/>
strSet = <s:propertyvalue="strSet"/>
userList = <s:propertyvalue="userList"/>
大小 = <s:propertyvalue="userList.size()"/>
第2个用户 = <s:propertyvalue="userList.get(1)"/> username = <s:propertyvalue="userList.get(1).username"/>
删除元素 = <s:propertyvalue="userList.remove(2)"/>
大小 = <s:propertyvalue="userList.size()"/>
map = <s:propertyvalue="map"/>
size = <s:propertyvalue="map.size()"/>
key_1 = <s:propertyvalue="map.key_1"/>
key_2 = <s:propertyvalue="map['key_2']"/>
key_3 = <s:propertyvalue="map.get('key_3')"/>
key_4 = <s:propertyvalue="map.key_4.get(1).username"/>
4:作用域与请求参数
public String accessScope()throws Exception {
this.userList =new ArrayList<UserBean>(6);
for (int i = 1; i <= 5; i++) {
UserBean userBean = new UserBean();
userBean.setUserid(String.valueOf(i));
userBean.setUsername("用户名_" + i);
this.userList.add(userBean);
}
this.request.setAttribute("req_key","Request作用域");
this.session.setAttribute("session_key",this.userList);
this.servletContext.setAttribute("app_key", OgnlAction.getSystemTime());
// 1:第一种:后台转换的解决。
// 页面中使用param对象或者EL表达式,取出来仍然是乱码。
// String param_b =request.getParameter("param_b");
// param_b = newString(param_b.getBytes("ISO-8859-1"), "UTF-8");
// this.request.setAttribute("param_b_xx",param_b);
// 2:第二种,改Tomcat的GET的URL编码
// server.xml中配置。ConnectorconnectionTimeout="20000" port="8080"
// protocol="HTTP/1.1"redirectPort="8443" URIEncoding="UTF-8"
// 3:写过滤器
return "succ";
}
Jsp:
req_key= <s:propertyvalue="#request.req_key"/> ${requestScope.req_key}
session_key = <s:propertyvalue="#session.session_key"/>
app_key = <s:propertyvalue="#application.app_key"/> ${applicationScope.app_key}
请求参数 =
param_a = <s:propertyvalue="#parameters.param_a"/> ${param.param_a}
param_b = <s:propertyvalue="#parameters.param_b"/> ${param.param_b}
param_b = <s:propertyvalue="#request.param_b_xx"/>
5:ValueStack与ActionContext
publicString accessValueStack()throwsException {
// ServletActionContext servletActionContext
ActionContext context = ActionContext.getContext();
context.put("context_key","这是放到ActionContext中的值");
/**
* 操作ValueStack
*/
ValueStack valueStack =context.getValueStack();
/**
*setValue只能设置Action中存在的属性的值。
*/
valueStack.setValue("xx", OgnlAction.getSystemTime());
/**
*set可以设置与增加Action中不存在的属性的值。
*/
valueStack.set("key_1", "value_1");
valueStack.set("key_2", "value_2");
valueStack.set("xx", "这是通过Set增加的");
//valueStack.push(arg0);
return "succ";
}
context_key= <s:propertyvalue="#context_key"/>
key_1 = <s:propertyvalue="key_1"/>
key_2 = <s:propertyvalue="key_2"/>
栈顶中的xx = <s:propertyvalue="xx"/>
Action中属性的XX = <s:propertyvalue="[1].xx"/> 通过下标的方式获取栈中的元素。
- OGNL表达式
- ognl表达式
- OGNL表达式
- OGNL表达式 #%$
- OGNL表达式
- OGNL表达式
- ognl表达式
- Ognl表达式
- ognl表达式
- OGNL表达式
- OGNL表达式
- OGNL表达式
- ognl表达式
- OGNL表达式
- OGNL表达式 #%$
- ognl表达式
- ognl表达式
- Ognl表达式
- 恢复出厂设置后Launcher数据错乱
- eclipse+svn+VisualSVN Server
- 浅谈SEO优化
- Linux系统Time_Wait状态的端口快速释放
- Redis主从复制,读写分离,主从切换
- OGNL表达式
- 线性表(List) 二
- 关于 Android 进程保活,你所需要知道的一切
- Android 性能分析工具dumpsys的使用
- js强大函数replace详解,利用其做模板方法与数据绑定
- 用Nodejs连接MySQL
- Visual Studio 2010 打包C#项目程序
- Android中postDelayed方法的使用
- Java实现几种常见排序方法