(7)OGNL2:RequestAware前端设置list、set、map中每个属性,如何填写,如何获取
2017-11-07 08:32
435 查看
public class UserAction extends ActionSupport implements SessionAware,RequestAware,ServletRequestAware{ /* * ActionContext只是设置设置数据,获取数据 * struct2提供一种简单使用session的方式,使用SessionAware接口来访问存储于ActionContext中的数据,该接口通过使用IoC/DI来为Action注入session Map,就 * 可以在程序中直接使用这个map操作数据了。 * 类似的,有RequestAware来获取包装请求对象的attribute中的值的Map * 有ApplicationAware来获取包装请求对象的ServletContext对象的attribute中的值的Map * 有ParameterAware来获取包装请求对象的参数中的值的Map */ /* * ActionContext仅仅是获取数据,实际开发是不够的,有时,希望根据功能需要,在Action中必须能获取servlet相关的api,比如要操作cookie,这个时候需要ServletActionContext *也有ServletResponseAware */ /* * ActionContext与ServletActionContext有些重复,都能获取到web对象的数据,通常ActionContext主要负责值的操作 * ServletActionContext:主要负责获取servlet对象。 * 优先使用ActionContext,当其功能不能满足时,采用ServletActionContext */ private String username; private User user; private List<User> userList; private Map<String,User> userMap; private Map<String,Object> session; private Map<String,Object> request; private HttpServletRequest request1; public String login() throws Exception { return "login"; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("用户名为:"+username); ActionContext c=ActionContext.getContext(); c.getSession().put("username", "session中的username"); c.getApplication().put("account", "application中的account"); /* * 使用ActionContext来访问Session中的数据,在struct2中,在程序里面访问的是Map,而非HttpSession * 原因:struct2框架与web相关的很多对象重新进行了包装,比如HttpSession对象就被包装在了一个Map对象中,里面存放着Session中的数据,提供这个map给Action使用 * 而不用Action直接和底层的HttpSession打交道,也正是因为框架的包装,让Action可以完全的和web层解耦 * 注意:ActionContext不能在普通的Java应用程序中使用。因为ActionContext包装的都是Web的数据,若没有web的环境和相应的数据,会抛出异常。 * 访问其他web对象的值也是与此类似的,访问的都是包装的map。 * 若想要在普通的Java应用中运行并测试Action,可以使用SessionAware 的方式来访问HttpSession。 */ return "success"; } public String execute1() throws Exception { System.out.println("域对象的username:"+user.getUsername()); user.setUsername("在action中修改了username"); return "success"; } public String execute2() throws Exception { System.out.println("集合中的对象属性:"+userList.get(0).getUsername()); return "success"; } public String execute3() throws Exception { System.out.println("集合中的对象属性:"+userMap.get("um").getUsername()); return "success"; } public String execute4() throws Exception { System.out.println(session.put("sessionKey", "sessionValue")); System.out.println(request.put("requestKey", "requestValue")); request1.setAttribute("request1", "request1的属性值"); request1.getSession().setAttribute("request2", "request2的属性值"); return "success"; } } //私有属性都是有set、get方法,这里省略 //有implements的方法,这里省略
login.jsp:填写信息页面
<body> 基本属性:<br/> <form action="ognl/welcome"> 用户名:<input type="text" name="username"> <input type="submit" value="提交"> </form> <hr/> 域对象:<br/> <form action="ognl/welcome1!execute1"> 用户名:<input type="text" name="user.username"> 密 码:<input type="text" name="user.password"> <input type="submit" value="提交"> </form> <hr/> 访问list:<br/> <form action="ognl/welcome2!execute2"> 用户名:<input type="text" name="userList[0].username"> 密 码:<input type="text" name="userList[0].password"> <input type="submit" value="提交"> </form> <hr/> 访问map:<br/> <form action="ognl/welcome3!execute3"> 用户名:<input type="text" name="userMap['um'].username"><!-- 通过ognl访问map时,通常的格式是:Map名称['键的名称'] --> 密 码:<input type="text" name="userMap['um'].password"> <input type="submit" value="提交"> </form> <hr/> 访问session:<br/> <form action="ognl/welcome4!execute4"> <input type="submit" value="提交"> </form> </body>
welcome.jsp
<body> 欢迎你!<s:property value="username"/><br/> 请求参数中的用户名:<s:property value="#parameters.username"/><br/> 请求属性中的用户名:<s:property value="#request.username"/><br/> 会话属性中的用户名:<s:property value="#session.username"/><br/> 应用属性中的用户名:<s:property value="#application.username"/><br/> attr中中的用户名:<s:property value="#attr.username"/><br/> <hr/> 访问静态方法和静态属性<br/> 访问静态属性,在类中,静态属性应该为public: <s:property value="@com.action.MyStatic@staticTest"/><br/> 访问静态方法: <s:property value="@com.action.MyStatic@testMethod()"/><br/> <hr/> 访问域对象:<br/> 访问域对象中的用户名:<s:property value="user.username"/> <hr/> 访问list:<br/> 访问list中的对象中的用户名:<s:property value="userList[0].username"/> <!-- 访问list时候,只需指明索引即可访问list中的指定元素 --><br/> 也可以访问List中的方法:<s:property value="userList.isEmpty" /> <s:debug></s:debug> <hr/> 访问map:<br/> 访问map中的对象中的用户名:<s:property value="userMap['um'].username"/> <!-- 访问list时候,只需指明索引即可访问list中的指定元素 --><br/> 也可以访问map中的方法:<s:property value="userMap.size" /> <hr/> 访问ActionContext中的session、request:<br/> 访问session中的对象:<s:property value="#session['sessionKey']"/> <br/> 访问request中的对象:<s:property value="#request['requestKey']"/> <br/> <hr/> 访问ServletActionContext中的session、request:<br/> 访问request中的对象:<s:property value="#request['request1']"/> <br/> 访问session中的对象:<s:property value="#session['request2']"/> <br/> <s:debug></s:debug> </body>
相关文章推荐
- (6)OGNL--1:访问值栈中的普通属性、对象属性、普通方法、静态方法、自写set、map、list
- 如何获取windows DPI设置值(Set Custom Text Size)
- 在spring 中如何注入map,set,list,property等参数
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Play Framework常用标签list,set,如何遍历list、map类型数据
- spring 配置文件中如何注入map list set等类型
- Spring 普通属性注入(int ,String, List, Map, Set, String[])
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Flex中如何通过给openItems属性,设置一个XMLList或Array对象来打开Tree的一个节点
- 随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value
- spring设置构造,name,ref,map,set,list赋值
- PHP中的__get()和__set()方法获取设置私有属性
- Spring基于xml配置文件对list、set、map集合的属性注入
- Nginx前端设置反向代理,后端Apache如何获取访客的真实IP,结合PHP
- Flex中如何通过给openItems属性设置一个XMLList或Array对象来打开Tree的一个节点的例子
- 如何在Java中选择Map/List/Set
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- 如何用th:attr标签在thymeleaf模板中设置属性-原标题:How To Set Attributes in Thymeleaf Template using th:attr?
- 为何使用EL表达式能够访问valueStack中对象的属性、采用OGNL表达式创建List/Map集合对象
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件