zoukankan      html  css  js  c++  java
  • struts2&获取请求&数据封装&EL表达式(OGNL表达式)获取值栈

    原生的Servlet的API

    直接获取对象request,和response

        public class RequestActionDemo2 extends ActionSupport{
            @Override
            public String execute() throws Exception {
                // 接收参数:
                HttpServletRequest req = ServletActionContext.getRequest();
                Map<String,String[]> map = req.getParameterMap();
                for (String key : map.keySet()) {
                    String[] value = map.get(key);
                    System.out.println(key+"   "+value[0]);
                }
            
                // 向域中存入数据:
                req.setAttribute("requestName", "张三");
                // 向session中存入数据:
                req.getSession().setAttribute("sessionName", "李四");
                // 向application中保存:
                ServletActionContext.getServletContext().setAttribute("applicationName", "王五");
                return SUCCESS;
            }
        }

    完全解耦合的方式获取servlet对象

    不再直接获取request,而是直接由struts2框架管理,我们直接从struts2的上下文对象中获取参数
       
    public class RequestActionDemo1 extends ActionSupport{
        @Override
        public String execute() throws Exception {
            // 接收表单的参数:
            // 使用的是Struts2中的一个对象ActionContext对象.
            ActionContext actionContext = ActionContext.getContext();
            // 接收参数:
            Map<String,Object> paramsMap = actionContext.getParameters();
            for (String key : paramsMap.keySet()) {
                String[] value = (String[]) paramsMap.get(key);
                System.out.println(key+"    "+value[0]);
            }
            
            // 向request中存入数据  request.setAttribute(String name,Object value);
            actionContext.put("requestName", "张三");
            // 向session中存入数据 request.getSession().setAttribute(String name,Object value);
            actionContext.getSession().put("sessionName", "李四");
            // 向application中存入数据 this.getServletContext().setAttribute(String name,Object value);
            actionContext.getApplication().put("applicationName", "王五");
            
            return SUCCESS;
        }
    }



    数据封装两种方式

    属性驱动和模型驱动,一般开发使用模型驱动,除了个别参数没有javabean使用属性驱动
       
    数据封装使用到了struts2值栈的概念,将数据存储在root栈,在请求后台,后台获取值栈对象,
    从中取出数据(后台也可以存储到值栈中前端使用OGNL表达式从值栈中取值),下面都是使用OGNL表达式在root栈中取值,省略了[0].top

    [值栈概念&OGNL表达式&struts2标签库][1]

    属性驱动

    一. 在action中编写属性的set请输入代码

    页面:

        <h1>Struts2的属性驱动中:set方法的方式</h1>
        <form action="${ pageContext.request.contextPath }/strutsDemo1.action" method="post">
            名称:<input type="text" name="name"><br/>
            年龄:<input type="text" name="age"><br/>
            生日:<input type="text" name="birthday"><br/>
            <input type="submit" value="提交">
        </form>
    Action:
       
    public class StrutsDemo1 extends ActionSupport{
        // 接收参数:
        private String name;
        private Integer age;
        private Date birthday;
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }



    二. 在action编写javabean属性get和set方法,页面中使用表达式的方式进行参数的封装
    页面:
       
        <h1>Struts2的属性驱动中:OGNL表达式的方式</h1>
        <form action="${ pageContext.request.contextPath }/strutsDemo2.action" method="post">
            名称:<input type="text" name="user.name"><br/>
            年龄:<input type="text" name="user.age"><br/>
            生日:<input type="text" name="user.birthday"><br/>
            <input type="submit" value="提交">
        </form>
    Action:
       
    public class StrutsDemo2 extends ActionSupport{
    
        private User user;
        // 必须提供get方法.
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
            
        public String save(){
            String username = user.username;
            ...
        }
    }

    模型驱动

    使用模型驱动的方式进行参数的封装
    页面:
     
        <h1>Struts2的模型驱动驱动中:模型驱动的方式</h1>
        <form action="${ pageContext.request.contextPath }/strutsDemo3.action" method="post">
            名称:<input type="text" name="name"><br/>
            年龄:<input type="text" name="age"><br/>
            生日:<input type="text" name="birthday"><br/>
            <input type="submit" value="提交">
        </form>



    Action:
       
    public class StrutsDemo3 extends ActionSupport implements ModelDriven<User>{
        // 模型驱动使用的对象.
        private User user = new User();// 必须手动new
        @Override
        public User getModel() {
            return user;
        }
        …
    }


    封装到List集合中

    页面:
       
        <form action="${ pageContext.request.contextPath }/strutsDemo4.action" method="post">
            名称:<input type="text" name="list[0].name"><br/>
            年龄:<input type="text" name="list[0].age"><br/>
            生日:<input type="text" name="list[0].birthday"><br/>
            名称:<input type="text" name="list[1].name"><br/>
            年龄:<input type="text" name="list[1].age"><br/>
            生日:<input type="text" name="list[1].birthday"><br/>
            <input type="submit" value="提交">
        </form>



    Action:
       
    public class StrutsDemo4 extends ActionSupport{
        private List<User> list;
        
        public List<User> getList() {
            return list;
        }
    
        public void setList(List<User> list) {
            this.list = list;
        }
    
        @Override
        public String execute() throws Exception {
            for (User user : list) {
                System.out.println(user);
            }
            return NONE;
        }
    }


    封装数据到Map集合

    页面:
       
        <h1>批量插入用户:封装到Map集合</h1>
        <form action="${ pageContext.request.contextPath }/strutsDemo5.action" method="post">
            名称:<input type="text" name="map['one'].name"><br/>
            年龄:<input type="text" name="map['one'].age"><br/>
            生日:<input type="text" name="map['one'].birthday"><br/>
            名称:<input type="text" name="map['two'].name"><br/>
            年龄:<input type="text" name="map['two'].age"><br/>
            生日:<input type="text" name="map['two'].birthday"><br/>
            <input type="submit" value="提交">
        </form>
    


    Action:
       
    public class StrutsDemo5 extends ActionSupport {
        private Map<String,User> map;
        
        public Map<String, User> getMap() {
            return map;
        }
    
        public void setMap(Map<String, User> map) {
            this.map = map;
        }
    
        @Override
        public String execute() throws Exception {
            for (String key : map.keySet()) {
                User user = map.get(key);
                System.out.println(key+"    "+user);
            }
            return NONE;
        }
    }


    EL(OGNL)表达式获取值栈数据

    OGNL表达式获取数据格式(向值栈中存储数据一样的格式,前面加#就是从Context值栈获取数据(默认是map集合)):
    [0]  获取到ContextRoot值栈中的栈顶(0表示栈顶,取第二个到最后从1开始 [1])到最后的所有数据
    [0].top 获取到ContextRoot栈顶的数据(可以省略默认去栈顶的数据)
    [0].top.name 后面就是和EL表达式一样javabean导航([0].top获取到栈顶的对象.name获取到对象中的name属性)
    EL从值栈获取数据原理:
    1、EL 表达式本身是用于获取域对象中的值

    2、域对象中值的存取
    (1)向域对象中放值使用setAttribute() 方法
    (2)从域对象中获取值使用 getAttribute() 方法

    3、Struts2 底层增强了Request 对象的 getAttribute() 方法 该增强是在Struts2 核心过滤器的 doFilter() 方法中做的,在其中调用wrapRequest()方法

    4、从Request 域中获取值
    (1)如果能获取到,直接返回
    (2)如果获取不到,会到值栈中把值获取出来,放到域对象中

    其他特殊符号

    #号的使用(两个作用从context值栈中取数据,创建map集合):
        获取context的数据
            <s:property value=”#request.name”/>
        用于构建一个Map集合:使用struts的UI标签的时候.
            <s:iterator value="#{'aaa':'111','bbb':'222','ccc':'333' }" var="entry">
            <s:property value="key"/>---<s:property value="value"/><br/>
            <s:property value="#entry.key"/>---<s:property value="#entry.value"/><br/>
            </s:iterator>
        <s:radio list="#{'1':'男','2':'女' }" name="sex"></s:radio>
    %号的使用:
    %强制解析OGNL表达式
       
    <s:textfield name="name" value="%{#request.name}"/>
    %强制不解析OGNL表达式
       
    <s:property value="%{'#request.name'}"/>

    $号的使用:
    在配置文件中使用OGNL表达式
        在struts的配置文件中使用.XML文件 或者 是属性文件.
  • 相关阅读:
    iOS沙盒路径
    iOS第三方语音-讯飞语音
    iOS第三方分享-ShareSDK
    iOS第三方语音-微信语音
    iOS-xib(自定义UITableViewCell)
    UITableView点击背景
    OC开发_Storyboard——UIApplication和网络活动指示器
    OC开发_Storyboard——绘制和视图
    OC开发_Storyboard——block和动画
    OC开发_Storyboard——NaviationController简单例子
  • 原文地址:https://www.cnblogs.com/sybk/p/10004733.html
Copyright © 2011-2022 走看看