Struts2框架学习(三) 数据处理

Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理。

 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言。

 

 值栈:ValueStack一种数据结构,操作数据的方式为:先进后出

 OGNL : Object-GraphNavigation Language(对象图形导航语言)将多个对象的关系使用一种树形的结构展现出来,更像一个图形,那么如果需要对树形结构的节点数据进行操作,那么可以使用 对象.属性 的方式进行操作,OGNL技术底层采用反射实现。

 

 

一:数据的提交方式

1,<form>表单提交

[html]  view plain  copy
 print ?
  1. <form action="/user/login_Login.action" method="post" >  
  2.     用户代码:<inputnameinputname="usercode" type="text" ><br>  
  3.     用户密码:<inputnameinputname="userpswd" type="password"><br>  
  4.               <inputtypeinputtype="submit" value="登录">  
  5. </form>  


2,url的方式  

[html]  view plain  copy
 print ?
  1. http://localhost.egov.com/user/login_Login.action?usercode=admin&userpswd=admin  
 

3,超链接

[html]  view plain  copy
 print ?
  1. <a href="/user/delete.action?usercode=admin">删除</a>    


4,异步提交

 ajax异步提交数据

 

二:数据存储

 数据的存储依赖于框架提供的拦截器功能,拦截器可以对请求进行拦截,获取所有的请求参数,循环遍历设置到值栈中。框架默认将被请求的Action对象存放到值栈的栈顶。Struts2框架提供三种方式将参数存入值栈


1,属性驱动模式

 Action中需要提供参数名称的set方法,框架会通过拦截器将请求参数获取到之后,会循环遍历将参数设置到值栈(栈顶对象)中。

[html]  view plain  copy
 print ?
  1. <input name="usercode" type="text" >  
  2. Ognl.setValue("usercode",action,"admin");//此时根对象是action  
  3.    


2,模型驱动模式

 必须要实现模型驱动接口,属于侵入性开发方式,不推荐使用

 需要定义数据模型的类,将属性封装到数据模型类中,Action中只需要定义模型对象的类型属性(必须创建对象赋值),模型对象的get/set属性并不是必须的。

 Action类需要实现ModelDriven接口,重写getModel()方法。

[html]  view plain  copy
 print ?
  1. <inputnameinputname="usercode" type="text" ><br>表单依然是模型对象的属性名称作为参数名称  
  2. Ognl.setValue("usercode",user,"admin");//此时根(栈顶)对象是user对象  
  3.    

3,域驱动模式

 与属性驱动的原理是类似,也是通过参数拦截器,将请求参数获取后循环设置到值栈中。

 在Action对象中定义模型对象属性,并提供get/set方法;

 在表单元素中增加模型对象属性的名称;

[html]  view plain  copy
 print ?
  1. <inputnameinputname="user.usercode" type="text" >  
  2. Ognl.setValue("user.usercode",action,"admin");//此时根对象是action  
  3.    

 

三:数据的传递

 框架将Http对象和包装后的Map集合对象都存放到ActionContext上下文对象集合中。所以可以根据ActionContext来获取想要使用的对象。

 

1,获取HTTP对象

[html]  view plain  copy
 print ?
  1. ActionContext ac = ActionContext.getContext(); //上下文对象相当于request范围  
  2. HttpServletRequest request =(HttpServletRequest)ac.get(StrutsStatics.HTTP_REQUEST);  
  3.   
  4. HttpSession session = request.getSession(false);  
  5.   
  6. ServletContext application = session.getServletContext();  
  7. ServletContext application = ac.get(StrutsStatics.SERVLET_CONTEXT);  
  8.   
  9. HttpServletRequest request = ServletActionContext.getRequest(); (推荐使用)  
  10. HttpServletResponse response =ServletActionContext.getResponse();  
  11.   
  12. Action类实现ServletRequestAware,或ServletResponseAware (属于侵入性开发方式,不推荐使用)  
  13.   
  14. private HttpServletRequest request  ; //set注入  
  15. private HttpServletResponse response ;  
  16.   
  17. @Override  
  18. public void setServletRequest(HttpServletRequest request) {  //实现该方法,该方法由框架调用,传递参数。  
  19.      this.requestrequest ;  
  20. }  
  21.   
  22. @Override  
  23. public void setServletResponse(HttpServletResponse response) {  
  24.     this.responseresponse ;  
  25. }  


2,获取Map集合

[html]  view plain  copy
 print ?
  1. ActionContext ac = ActionContext.getContext();  
  2.   
  3. Mapsession = ac.getSession();  
  4. Mapsession2 =(Map)ac.get("session");  
  5. Mapsession3=(Map)ac.get(ActionContext.SESSION);          
  6.   
  7. Mapapplication = ac.getApplication()  
  8. Mapapplication = ac.get(ActionContext.APPLICATION);  
  9. Mapapplication = ac.get("application");  


3, 获取值栈对象以及参数集合对象

[html]  view plain  copy
 print ?
  1. ActionContext ac = ActionContext.getContext();  
  2. ValueStack vs = ac.getValueStack();  
  3.   
  4. Map paramts = ac.getParameters();  

四:数据的显示

 用El表达式的形式,取request对象中的值

[html]  view plain  copy
 print ?
  1. ${requestScope.username }  
  2.   
  3. 而这种表达式的方式可以表示成java代码的方式:  
  4.   
  5. <%  
  6.   Stringusername =(String)request.getAttribute("username");//被重写的方法,底层是通过ognl查询值栈中的数据  
  7.   out.print(username);  
  8. %>    

 从request返回取数据,实质上底层是通过Ognl语言操作值栈中的数据。 ActionContext对象的集合和OgnlValueStack值栈的集合为同一个集合对象,所以从值栈的Map集合中取数据,就相当于从ActionContext上下文中取数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值