zoukankan      html  css  js  c++  java
  • Struts2之Ognl用法

    OgnlAction.java

    package com.kaishengit.web;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import com.kaishengit.dao.UserDao;
    import com.kaishengit.vo.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;

    public class OgnlAction extends ActionSupport {
    private int id;
    private String address;
    private Map<String,User> userMap;
    private User user;
    private List<User> userList;

    private UserDao dao=new UserDao();

    private static final long serialVersionUID = 5838762467757449050L;

    @SuppressWarnings(
    "unchecked")
    public String execute() throws Exception{
    user
    =dao.findById("1");
    userList
    =dao.findAll();
    id
    =12;
    address
    ="China";

    userMap
    =new HashMap<String,User>();
    userMap.put(
    "tom", user);
    userMap.put(
    "jerry", dao.findById("2"));

    Map
    <String,Object> session=ActionContext.getContext().getSession();
    session.put(
    "sessionKey", "sessionValue");

    Map
    <String,Object> application=ActionContext.getContext().getParameters();
    application.put(
    "applicationKey", "applicationValue");

    Map
    <String,Object> request=(Map<String, Object>) ActionContext.getContext().get("request");
    request.put(
    "requestKey", "requestValue");


    return SUCCESS;
    }






    //-------------------------------------
    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getAddress() {
    return address;
    }

    public void setAddress(String address) {
    this.address = address;
    }


    public User getUser() {
    return user;
    }

    public void setUser(User user) {
    this.user = user;
    }

    public List<User> getUserList() {
    return userList;
    }

    public void setUserList(List<User> userList) {
    this.userList = userList;
    }

    public Map<String, User> getUserMap() {
    return userMap;
    }

    public void setUserMap(Map<String, User> userMap) {
    this.userMap = userMap;
    }


    }

    struts.xml

    <action name="ognl" class="com.kaishengit.web.OgnlAction">
    <result>ognl.jsp</result>
    </action>

    ognl.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    </head>

    <body>
    ognl.jsp.....................
    <br>
    编号:
    <s:property value="id"/><br>
    地址:
    <s:property value="address"/><br>
    User对象:
    <s:property value="user"/><br>
    userName:
    <s:property value="user.userName"/><br>
    userPwd:
    <s:property value="user.userPwd"/><br>
    userList集合:
    <s:property value="userList"/><br>
    list size:
    <s:property value="userList.size"/><br>
    第一个对象:
    <s:property value="userList[0]"/><br>
    投影
    <br>
    取得List中所有的姓名:
    <s:property value="userList.{userName}"/><br>
    取得List中第一个对象的姓名:
    <s:property value="userList.{userName}[0]"/>
    输出List中姓名为tom的所有用户对象:
    <br>
    <s:property value="userList.{?#this.userName=='tom'}"/><br>
    输出List中姓名为tom的第一个用户对象:
    <br>
    <s:property value="userList.{^#this.userName=='tom'}"/><br>
    输出List中姓名为tom的最后一个用户对象:
    <br>
    <s:property value="userList.{$#this.userName=='tom'}"/><br>

    OGNL操作Map集合--------------------
    <br>
    输出Map中的key:
    <s:property value="userMap.keys"/><br>
    输出Map中的value:
    <s:property value="userMap.values"/><br>
    输出Map中的数量:
    <s:property value="userMap.size"/><br>
    输出key(tom)对应的value:
    <s:property value="userMap['tom'].{id}[0]"/><br>---
    判断Map是否为empty:
    <s:property value="userMap.isEmpty"/><br>
    动态创建List和Map ---------------------------
    <br>
    动态创建List:
    <s:property value="{'aa','bb','cc'}"/>
    动态创建Map:
    <s:property value="#{'aa':'aa','bb':'bb'}"/>
    <br>
    获取request session Application中的值--------------------------
    <br>
    获取Session中的值:
    <br>
    <s:property value="#session.sessionKey"/><br>
    获取reuqst中的值:
    <br>
    <s:property value="#request.requestKey"/><br>
    获取Application中的值:
    <br>
    <s:property value="#application.applicationKey"/>
    </body>
    </html>

  • 相关阅读:
    了解语言学习的四个阶段,孩子的英语学习更从容
    MySQL 修改字段类型或长度
    js中退出语句break,continue和return 比较
    PHP中unset,array_splice删除数组中元素的区别
    php判断一个数组是另一个数组的子集
    PHP实现一维数组转二维数组的方法
    基于 HTML5 WebGL 的 3D 网络拓扑图
    SQL 2008下载地址以及全新安装详细过程
    php从数组中取出一段 之 array_slice
    Linux命令:cp (copy)复制文件或目录
  • 原文地址:https://www.cnblogs.com/archie2010/p/1945193.html
Copyright © 2011-2022 走看看