struts2之OGNL和struts2标签库

来源:互联网 发布:手机浏览器查看源码 编辑:程序博客网 时间:2024/05/29 14:44

OGNL简介: 

  (1)OGNLObject Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目。

     struts2框架默认就支持Ognl表达式语言(所以struts必须引用的包:ognl.jar);

  (2)struts2的ognl的作用:页面取值使用;

  (3)OGNL和EL的区别:

     EL表达式语言:用于页面取值,jsp页面取值的标准(默认可以直接使用,应用范围更加广泛);

       OGNL表达式语言:struts2标签默认支持的表达式语言,必须配置struts标签使用,不能离开struts2标签直接使用;

  (4)OGNL有一个上下文(Context)概念,[OgnlContext对象:OgnlContext对象是ognl表达式语言的核心];

     其实上下文就是一个MAP结构,它实现了  java.utils.Map 的接口。


用一个OGNL和struts标签结合的实例演示一下struts的标签的强大功能:

1:第一还是引入struts2核心包,省略

2:创建一个实体类,用于测试显示在页面的信息,如User.java,源码如下所示:

 1 package com.bie.ognl; 2 /**  3 * @author BieHongLi  4 * @version 创建时间:2017年3月7日 下午8:45:27  5 *  6 */ 7 public class User { 8  9     private int id;10     private String name;11     public int getId() {12         return id;13     }14     public void setId(int id) {15         this.id = id;16     }17     public String getName() {18         return name;19     }20     public void setName(String name) {21         this.name = name;22     }23     public User(int id, String name) {24         super();25         this.id = id;26         this.name = name;27     }28     29     30 }

 3:创建完成实体类之后就可以开始开发action,如OgnlAction.java,源码如下所示:

 1 package com.bie.ognl; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport;10 11 /** 12 * @author BieHongLi 13 * @version 创建时间:2017年3月7日 下午8:43:00 14 * 15 */16 public class OgnlAction extends ActionSupport{17 18     19     private static final long serialVersionUID = 1L;20     @Override21     public String execute() throws Exception {22         //测试迭代标签23         List<User> list=new ArrayList<User>();24         Map<Integer, User> map=new HashMap<Integer, User>();25         26         //初始化27         for(int i=1;i<11;i++){28             User user=new User(i,"张三"+i);29             30             list.add(user);31             map.put(user.getId(), user);32         }33         34         //保存list信息35         ActionContext.getContext().getContextMap().put("list", list);36         //保存map信息37         ActionContext.getContext().getContextMap().put("map", map);38         39         40         return "success";41     }42     43 }

4:开发完action,配置struts.xml文件,这里使用主次配置,所以这里配置ognl.xml,然后在struts.xml文件中引入ognl.xml配置文件即可:

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4     "http://struts.apache.org/dtds/struts-2.0.dtd"> 5   6 <struts> 7      8    <package name="ognlpackage" extends="struts-default"> 9            <action name="ognl" class="com.bie.ognl.OgnlAction">10                <result name="success">ognl.jsp</result>11            </action>12    </package>13    14    15 </struts>

5:最后书写ognl.jsp显示页面,用于显示信息,源码如下所示:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>ognl显示信息的页面</title> 9 10 <style type="text/css">11     .odd{background-color:pink;}12     .even{background-color:yellow;}13 </style>14 </head>15 <body>16 17 <h1>1:list显示信息</h1>18 <table cellpadding="5" cellspacing="10">19     <tr>20         <th>编号</th>21         <th>姓名</th>22         <th>奇偶行</th>23     </tr>24     <s:iterator value="#request.list" var="user" status="us">25     <tr class=<s:property value="#us.even?'even':'odd'"/>>26         <td><s:property value="#user.id"/></td>27         <td><s:property value="#user.name"/><br/></td>28         <td><s:property value="#us.even" /></td>29     </tr>30     </s:iterator>31 </table>32 <h1>1:迭代map信息</h1>33 <table cellpadding="5" cellspacing="10">34     <tr>35         <th>编号</th>36         <th>编号2</th>37         <th>姓名</th>38         <th>奇偶行</th>39     </tr>40     <s:iterator value="#request.map" var="Muser" status="mu">41     <tr class=<s:property value="#us.even?'even':'odd'"/>>42         <td><s:property value="#Muser.value.id"/></td>43         <!-- 迭代编号的两种方式 -->44         <td><s:property value="#Muser.key"/></td>45         <td><s:property value="#Muser.value.name"/><br/></td>46         <td><s:property value="#mu.even" /></td>47     </tr>48     </s:iterator>49 </table>50 </body>51 </html>

运行效果如下所示:

 

 

 

革命尚未成功,OGNL ko , go!!!

0 0