标签:ognl表达式
struts.xml
<!--OGNL表达式 --> <package name="Ognl" extends="struts-default"> <action name="getOgnl" class="com.cb.Ognl" method="TestOgnl"> <result name="list">/WEB-INF/page/Ognl.jsp</result> </action> </package>
Ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ taglib uri="/struts-tags" prefix="s"%><!--使用Struts2标签 --> <% request.setAttribute("name", "陈冰"); request.getSession().setAttribute("age", 25); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘Ognl.jsp‘ starting page</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> <!--Ognl表达式输出request属性的值 --> <s:property value="#request.name" /> <br>------------------------------<br> <!--Ognl表达式输出session中属性的值 --> <s:property value="#session.age" /> <br>------------------------------<br> <s:property value="#session.Phone" /> <br>------------------------------<br> <!--Ognl表达式输出值栈中的属性的值 --> <s:property value="Email" /> <br>------------------------------<br> <!--Ognl表达式构造list集合 --> <s:set var="list" value="{‘广西‘,‘科技‘,‘大学‘}" scope="session"></s:set> <!--s:iterator迭代特点:把当前迭代对象存放在值栈的栈顶中 --> <s:iterator value="#session.list"> <!--value为 NULL输出值栈栈顶的值 --> <s:property /> </s:iterator> <br>------------------------------<br> <!--Ognl表达式构造Map集合 --> <s:set var="map" value="#{‘小张‘:‘XZ‘,‘小李‘:‘XL‘,‘小陈‘:‘XC‘}" scope="request"></s:set> <!--s:iterator迭代特点:把当前迭代对象存放在值栈的栈顶中 --> <s:iterator value="#request.map"> <s:property value="key" />:<s:property value="value" /> </s:iterator> <br>------------------------------<br> <!--输出后台Map集合值 --> <s:property value="#request.key1" /> <s:property value="#request.key2" /> <s:property value="#request.key3" /> <br>------------------------------<br> <!--Ognl表达式判断某个元素是否在某个集合中 --> <s:if test="‘小张‘ in {‘小张‘,‘小李‘,‘小陈‘}"> 在集合中 </s:if> <s:else> 不在集合中 </s:else> <br>------------------------------<br> <!--结合Action,输出价格大于60的书本 --> <s:iterator value="books.{?#this.bookPrice>60}"> 书名:<s:property value="bookName"/>,价格:<s:property value="bookPrice"/> <br> </s:iterator> </body> </html>
Book.java
package com.cb; public class Book { private Integer bookId; private String bookName; private float bookPrice; public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public float getBookPrice() { return bookPrice; } public void setBookPrice(float bookPrice) { this.bookPrice = bookPrice; } //构造器 public Book(Integer bookId, String bookName, float bookPrice) { this.bookId = bookId; this.bookName = bookName; this.bookPrice = bookPrice; } }
Ognl.java
package com.cb; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class Ognl { private String Email; // book private List<Book> books; public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public String TestOgnl() { this.Email = "654839007@qq.com"; ActionContext.getContext().getSession().put("Phone", "15818331379"); Map<String, Integer> maps = new HashMap<String, Integer>(); maps.put("key1", 1); maps.put("key2", 2); maps.put("key3", 3); for (Map.Entry<String, Integer> entry : maps.entrySet()) { ActionContext.getContext().put(entry.getKey(), entry.getValue());// 往request中注值 } books = new ArrayList<Book>(); books.add(new Book(1, "Java", 78)); books.add(new Book(2, "Extjs", 35)); books.add(new Book(3, "Html", 45)); books.add(new Book(4, "Jsp", 88)); return "list"; } }
标签:ognl表达式
原文地址:http://cblovey.blog.51cto.com/10052824/1657332