Struts2 值栈和OGNL
来源:互联网 发布:sql删除某列数据 编辑:程序博客网 时间:2024/05/02 03:01
值栈和ognl
1. 值栈是ActionContext的一个对象,它是栈结构,特征:先进后出,struts2中值栈存放的数据是Action对象。
2. ognl表达式(Object Graph Navigation Language 对象图形导航语言),他的主要优点是:可以访问OGNL上下文和ActionContext。
publicstaticvoid main(String[]args) {
//数据有常用的数据和不常用的数据----常用的一般是"小数据"(如本案例
//中的user),不常用的一般是"大数据"
//表达式:常用的数据是直接取,不常用的数据(ActionContext中数据)加#取
Map<String,Object> map=new HashMap<String,Object>();
map.put("username","张三");
map.put("age", 30);
User user=new User();
user.setAge(10);
user.setUsername("李四");
Object obj=null;
try {
obj=Ognl.getValue("#username",map, user); //取的不常用数据
} catch (OgnlExceptione) {
e.printStackTrace();
}
System.out.println(obj); //输出结果为张三
}
注意:值栈中的数据理解为“小数据”,ActionContext中的数据理解为“大数据”,小数据又叫根对象,对应getValue的第三个参数。
3. struts2中使用ognl表达式是通过struts2标签来取值的。
在jsp中导入struts2的标签库
<%@taglibprefix="s"uri="/struts-tags"%>
注意:要使用struts2的标签那么要通过struts2过滤器来启动,如果过滤器的配置为*.action结尾时,不能直接访问jsp页面,需要通过action跳转。如果过滤器配置为/*时,可以直接访问jsp页面,Struts2推荐不直接访问jsp页面,推荐使用action来跳转页面,因为如果直接到jsp页面会暴露地址。
在jsp页面通过struts2标签获得值栈的数据和ActionContext对象中的数据写法不同,因为值栈是直接把Action对象(本案例中的Action对象是指LoginAction对象)的属性暴露,所以访问值栈中的user的值写法如下:
用户名:<s:propertyvalue="user"/>
若访问ActionContext中对象的值写法如下:
用户名:<s:propertyvalue="#session.user"/>
如果session没有设置值,则无法取到
Action页面:
publicclass LoginAction{
private Stringuser;
private Stringpwd;
HttpServletRequest request;
public String execute(){
if(user.equals("zys")&&pwd.equals("12345")){
System.out.println("登录成功");
HttpServletRequest request=ServletActionContext.getRequest();
request.getSession().setAttribute("user", user);
System.out.println("user="+request.getParameter("user"));
return"success";
}else{
System.out.println("登录失败");
return"failure";
}
}
public String getUser() {
returnuser;
}
publicvoid setUser(Stringuser) {
this.user =user;
}
public String getPwd() {
returnpwd;
}
publicvoid setPwd(Stringpwd) {
this.pwd =pwd;
}
}
结论:使用ognl表达式访问action属性时,直接访问,访问actionContext对象值需要加#号。
- Struts2-值栈和OGNL
- Struts2 值栈和ognl
- Struts2 值栈和OGNL
- Struts2中的值栈和OGNL
- Struts2值栈和ognl表达式
- Struts2值栈和ognl表达式
- OGNL和Struts2中的值栈
- Struts2之 OGNL表达式和值栈
- Struts2值栈和OGNL表达式
- struts2--值栈以及OGNL
- Struts2的值栈-OGNL
- Struts2-OGNL与值栈
- 《研磨struts2》第七章 值栈和OGNL 之 7.2 OGNL
- 《研磨struts2》第七章 值栈和OGNL 之 7.3 使用OGNL访问复杂对象
- Struts2.x教程(二) OGNL和值栈
- 走进Struts2(五)— 值栈和OGNL
- Struts2基础学习(七)—值栈和OGNL
- Struts2中关于值栈和OGNL表达式的理解
- 连接
- materialRefreshLayout 下拉刷新
- CentOS 7.0关闭默认防火墙启用iptables防火墙
- 数组与矩阵---转圈打印矩阵
- 项目开发注意
- Struts2 值栈和OGNL
- Lua语法小贴士(十五)upvalue
- RabbitMQ的安装,配置,监控
- Dubbo分布式,微服务架构解决方案,jvm虚拟机,Redis持久化
- 代码函数调用关系图
- 移动运营商订制短信卡片无法点击
- webflow1.06升级到2.4.4
- js中获取键盘事件
- [CMake] 第三方库使用示例