zoukankan      html  css  js  c++  java
  • Struts2(五) ognl与valuestatck

    一、OGNL表示式使用 和 值栈(valuestack)----重点

    1、定义:OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目。 Struts2框架使用OGNL作为默认的表达式语言。OGNL 是一种比EL 强大很多倍的语言。

           * xwork 提供 OGNL表达式

           * ognl-3.0.5.jar    

    OGNL 提供五大类功能

       1)支持对象方法调用,如xxx.doSomeSpecial();

       2)支持类静态的方法调用和值访问

       3)访问OGNL上下文(OGNL context)和ActionContext; (重点 操作ValueStack值栈 )

       4)支持赋值操作和表达式串联

       5)操作集合对象。

    2、使用OGNL访问 对象方法 和 静态方法
    3、 访问OGNL上下文(OGNL context)和ActionContext
     
    1、 值栈在开发中应用 
    主流应用 : 值栈 解决 Action 向 JSP 传递 数据问题

    1)消息 String类型数据

           this.addFieldError("msg", "字段错误信息");

           this.addActionError("Action全局错误信息");

           this.addActionMessage("Action的消息信息");

    * fieldError 针对某一个字段错误信息 (常用于表单校验)、actionError (普通错误信息,不针对某一个字段 登陆失败)、 actionMessage 通用消息 

    * 在jsp中使用 struts2提供标签 显示消息信息

           <s:fielderror fieldName="msg"/>

           <s:actionerror/>

           <s:actionmessage/>

    2)数据 (复杂类型数据)

           使用值栈valueStack.push(products);

    哪些数据默认会放入到值栈 ???

           a)每次请求,访问Action对象 会被压入值栈 ------- DefaultActionInvocation 的 init方法 stack.push(action);

           * Action如果想传递数据给 JSP,只有将数据保存到成员变量,并且提供get方法就可以了

           b)ModelDriven 接口 有一个单独拦截器

           <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

           在拦截器中 ,将model对象 压入了值栈 stack.push(model);

           * 如果Action 实现ModelDriven接口,值栈默认栈顶对象 就是model对象

     
    2、 值栈的数据 通过EL访问
    3、 OGNL表达式 常见使用

    #、 % 、$ 符号使用

    1) # 的 使用

    用法一  # 代表 ActionContext.getContext() 上下文

    <s:property value="#request.name" /> 
    ActionContext().getContext().getRequest().get("name");
      #request
      #session
      #application
      #attr
      #parameters

    用法二 : 不写# 默认在 值栈中root中进行查找

    <s:property value="name" /> 在root中查找name属性 
    * 查询元素时,从root的栈顶元素 开始查找, 如果访问指定栈中元素    <s:property value="[1].name" />  访问栈中第二个元素name属性 
    * 访问第二个元素对象 <s:property value="[1].top" />

    用法三 :进行投影映射 (结合复杂对象遍历 )

    1)集合的投影(只输出部分属性
       <h1>遍历集合只要name属性</h1>
        <s:iterator value="products.{name}" var="pname"> 
            <s:property value="#pname"/>
        </s:iterator>
    2)遍历时,对数据设置条件 
        <h1>遍历集合只要price大于1500商品</h1>
        <s:iterator value="products.{?#this.price>1500}" var="product"> 
            <s:property value="#product.name"/> --- <s:property value="#product.price"/>    
        </s:iterator>
    3)综合
       <h1>只显示价格大于1500 商品名称</h1>
        <s:iterator value="products.{?#this.price>1500}.{name}" var="pname"> 
            <s:property value="#pname"/>
        </s:iterator> 

    用法四: 使用#构造map集合

    经常结合 struts2 标签用来生成 select、checkbox、radio
        <h1>使用#构造map集合 遍历</h1>
        <s:iterator value="#{'name':'aaa','age':'20', 'hobby':'sport' }" var="entry">
            key : <s:property value="#entry.key"/> , value:  <s:property value="#entry.value"/> <br/>
        </s:iterator>

    2) %的使用

    用法一: 结合struts2 表单表单使用, 通过%通知struts, %{}中内容是一个OGNL表达式,进行解析

    <s:textfield name="username" value="%{#request.username}"/>

       用法二: 设置ognl表达式不解析 %{'ognl表达式'}

    <s:property value="%{'#request.username'}"/>

    3)$的使用

       用法一 :用于在国际化资源文件中,引用OGNL表达式
    在properties文件 msg=欢迎您, ${#request.username}
        在页面
            <s:i18n name="messages">
                <s:text name="msg"></s:text>
            </s:i18n>
            * 自动将值栈的username 结合国际化配置信息显示

    用法二 :在Struts 2配置文件中,引用OGNL表达式

    <!-- 在Action 提供 getContentType方法 -->
            <param name="contentType">${contentType}</param>        
           *  ${contentType} 读取值栈中contentType数据,在Action提供 getContentType 因为Action对象会被压入值栈,
              contentType是Action属性,从值栈获得

    结论: #使用ognl表达式获取数据,% 控制ognl表达式是否解析 ,$ 用于配置文件获取值栈的数据

    二、struts2 标签库 
    1、 通用标签库 的学习
    <s:property> 解析ognl表达式,设置默认值,设置内容是否HTML转义
    <s:set> 向四个数据范围保存数据 
    <s:iterator> 遍历值栈中数据 
    <s:if> <s:elseif> <s:else> 进行条件判断 -------- elseif 可以有多个 
    <s:url> 进行URL重写(追踪Session ) ,结合s:param 进行参数编码 
        *   <s:url action="download" namespace="/" var="myurl">
                <s:param name="filename" value="%{'MIME协议简介.txt'}"></s:param>
            </s:url>
            <s:property value="#myurl"/>
    <s:a> 对一个链接 进行参数编码 
        * <s:a action="download" namespace="/" >下载MIME协议简介.txt
                <s:param name="filename" value="%{'MIME协议简介.txt'}"></s:param>
          </s:a>
    2、 UI标签库的学习 (Form标签)
    使用struts2 form标签 好处 : 支持数据回显 , 布局排班
    <s:form> 表单标签 
        <s:form action="regist" namespace="/" method="post" theme="xhtml">  ---   theme="xhtml" 默认布局样式
    <s:textfield> 生成 <input type="text" >    
    <s:password > 生成 <input type="password" >
    
    <s:submit type="submit" value="注册"/> 生成 <input type="submit" >
    <s:reset type="reset" value="重置" />    生成 <input type="reset" >
    <s:textarea> 生成 <textarea> 多行文本框
    <s:checkboxlist> 生成一组checkbox
        * 使用ognl构造 Map (看到值和提交值 不同时)
        * <s:checkboxlist list="#{'sport':'体育','read':'读书','music':'音乐' }" name="hobby"></s:checkboxlist>
    <s:radio> 生成一组radio
        * 使用 ognl构造 List  (看到内容和提交值 相同时)
        * <s:radio list="{'男','女'}" name="gender"></s:radio>
    <s:select> 生成一个<select> 
        * <s:select list="{'北京','上海','南京','广州'}" name="city"></s:select>
    
    ============= struts2 开发 密码框 默认不回显 
          <s:password name="password" id="password" showPassword="true"/>
    3、 页面元素主题设置

    方式一 :<s:textfield name="username"  label="用户名“ theme="simple"></s:textfield> 只对当前元素有效

    方式二 :<s:form  action="" method="post" namespace="/ui“theme="simple"> 对form中所有元素有效

    方式三 : struts.xml

         <constant name="struts.ui.theme" value="simple"></constant>修改默认主题样式,页面所有元素都有效

    优先级 : 方式一 > 方式二> 方式三

    三、struts2中的防止表单重复提交

    表单重复提交 危害:刷票、 重复注册、带来服务器访问压力(拒绝服务)

    1、在jsp 通过 <s:token /> 生成令牌号

    生成表单隐藏域,将令牌号保存到Session

    2、 通过struts2 提供 tokenIntercetor 拦截器 完成请求中令牌号 和 session中令牌号 比较

    <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/> 
            <action name="token" class="cn.itcast.struts2.TokenAction">
                <result>/index.jsp</result>
                <!-- 重新定义拦截器 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="token"></interceptor-ref>
            </action>

    3、当表单重复提交时,token拦截器自动跳转 result name="invalid.token"

     通过 <s:actionError/> 显示错误信息

     
    四、struts2中ajax插件
  • 相关阅读:
    1046 Shortest Distance (20 分)(模拟)
    1004. Counting Leaves (30)PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
    1041 Be Unique (20 分)(hash散列)
    1036 Boys vs Girls (25 分)(查找元素)
    1035 Password (20 分)(字符串处理)
    1044 Shopping in Mars (25 分)(二分查找)
    onenote使用小Tip总结^_^(不断更新中...)
    1048 Find Coins (25 分)(hash)
    三个故事
    领导者的举止
  • 原文地址:https://www.cnblogs.com/shelly0307/p/10765301.html
Copyright © 2011-2022 走看看