(7)OGNL2:RequestAware前端设置list、set、map中每个属性,如何填写,如何获取

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>
posted @ 2017-11-07 08:32  测试开发分享站  阅读(88)  评论(0编辑  收藏  举报