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>

原文地址:https://www.cnblogs.com/archie2010/p/1945193.html