1.集合的投影(过滤)有以下三种方式:
a.“?#”:投影(过滤)所有符合条件的集合,如:users.{?#this.age > 19};
b.“^#”:投影(过滤)第一个符合条件的元素,如:users.{^#this.age > 19};
c.“$#”:投影(过滤)最后一个符合条件的元素,如:users.{$#this.age > 19} 。
2.“this”表示集合中的元素;
3.投影(过滤)操作返回的是一个集合,可以使用索引取得集合中指定的元素,如:users.{?#this.age > 19}[0]。
实例:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="
http://java.sun.com/xml/ns/javaee
"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
">
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"
http://struts.apache.org/dtds/struts-2.0.dtd
">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK" />
<package name="user" namespace="/" extends="struts-default">
<action name="user" class="cn.edu.ahau.mgc.struts2.action.UserAction">
<result>/addSuccess.jsp</result>
</action>
</package>
</struts>
UserAction.java:
package cn.edu.ahau.mgc.struts2.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.edu.ahau.mgc.struts2.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String userName;
private User user;
private List<User> users = new ArrayList<User>();
private Set<User> usersSet = new HashSet<User>();
private Map<String, User> usersMap = new HashMap<String, User>();
public UserAction() {
users.add(new User("1", 19));
users.add(new User("2", 20));
users.add(new User("3", 21));
usersSet.add(new User("1", 19));
usersSet.add(new User("2", 20));
usersSet.add(new User("3", 21));
usersMap.put("u1", new User("1", 19));
usersMap.put("u2", new User("2", 20));
usersMap.put("u3", new User("3", 21));
}
public String execute() {
return SUCCESS;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String test() {
return "Action Method";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public Set<User> getUsersSet() {
return usersSet;
}
public void setUsersSet(Set<User> usersSet) {
this.usersSet = usersSet;
}
public Map<String, User> getUsersMap() {
return usersMap;
}
public void setUsersMap(Map<String, User> usersMap) {
this.usersMap = usersMap;
}
}
User.java:
package cn.edu.ahau.mgc.struts2.model;
public class User {
private String userName;
private int age;
private User firend;
public User() {
}
public User(String userName, int age) {
super();
this.userName = userName;
this.age = age;
}
public User getFirend() {
return firend;
}
public void setFirend(User firend) {
this.firend = firend;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User(" + this.userName + ", " + this.age + ")";
}
public String hello() {
return "Hello World!";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
index.jsp:
<%@ page language="java" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>ResultParam</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="user">user</a><br />
</body>
</html>
addSuccess.jsp:
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Dynamic</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
User Add Success! <br />
投影(过滤)所有符合条件的集合:users.{?#this.age > 19} --> <s:property value="users.{?#this.age > 19}" /><br />
投影(过滤)所有符合条件的集合中指定的元素:users.{?#this.age > 19}[0] -->
<s:property value="users.{?#this.age > 19}[0]" /><br />
投影(过滤)第一个符合条件的元素:users.{^#this.age > 19} --> <s:property value="users.{^#this.age > 19}" /><br />
投影(过滤)最后一个符合条件的元素:users.{$#this.age > 19} --> <s:property value="users.{$#this.age > 19}" /><br />
<s:debug></s:debug>
</body>
</html>
相关推荐
struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用
struts2标签和OGNL表达式struts2标签和OGNL表达式
Struts2的标签库及OGNL表达式语言
NULL 博文链接:https://takeme.iteye.com/blog/1668255
ognl表达式 ognl表达式 ognl表达式 ognl表达式
struts2标签和OGNL表达式.pdf
NULL 博文链接:https://chaoyi.iteye.com/blog/2158731
本案例提供ognl使用详解,测试通过,只需解压放入自己WEB项目中,执行struts_ognl包内java文件即可(未提供jia包,若需要可以联系留言发至邮箱),若测试不好可以联系本人提供指导. Struts 2默认的表达式语言是OGNL...
struts2中ognl表达式的用法,详细介绍了ognl表达式与struts2基本标签的结合。
struts2-OGNL表达式测试代码
本资源适合刚接触Struts和Ognl的小伙伴交流学习,欢迎吐槽。
struts2 OGNL,struts2 表达式语言,Struts2 中OGNL表达式的用法,Struts2 #,表达式语言的好处,Struts2 $,struts2 井号,星号,百分号
最近的项目开发中使用了struts2的框架,但是好多人都不清楚struts2的标签的具体的用法,要想完全掌握struts2标签的用法,那么你必须先精通了struts2中OGNL的用法。struts2中很多东西其实理解就行,但是惟独OGNL虽然...
主要介绍了Struts2 通过ognl表达式实现投影,具有一定参考价值,需要的朋友可以了解下。
ognl表达式在struts中的用法解析
struts2OGNL表达式ActionContext及valuesStack.pdf
OGNL是对象图导航语言(即该表达式访问的是对象及对象的属性)
从官网checkout下来的源码,如果大家觉的区官网麻烦可以直接下载这个
NULL 博文链接:https://johnson2132.iteye.com/blog/720769
1、 理解Struts2的类型转换类型 2、 理解局部类型转换器与全局类型转换器的区别 3、 掌握Struts2的自定义类型转换 4、 理解OGNL表达式语言