本文主要是介绍struts2的OGNL表达式(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,访问action基本属性和实体对象,
2,访问action集合/数组、访问Map、访问时运算、访问时调用方法、创建集合
3,访问action的静态属性和方法
4,访问其他5个就不说了,要加个#号引用即可
OGNLAction.java
package com.OGNL;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.opensymphony.xwork2.ActionSupport;public class OGNLAction extends ActionSupport {private Integer id=1000; //基本类型 private String name="tuke"; private User user=new User(); //实体对象 private List<String> employeeList=new ArrayList<String>(); //Listprivate Map<String, String> employeeMap=new HashMap<String, String>(); //Map public static String str="this is static attribute"; //静态属性public String execute(){ //初始化实体对象 user.setName("tuke"); user.setSex("nan"); user.setAge("18");//初始化集合数据 employeeList.add("胡东"); employeeList.add("李成"); employeeList.add("张宇"); //初始化Map数据 employeeMap.put("黄药师", "工资5000"); employeeMap.put("刘寒", "工资8000"); employeeMap.put("江山", "工资7000"); return "success"; } //静态方法public static String getstatic(){return "this is action static result";}public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User getUser() { return user; } public void setUser(User user) { this.user = user; }public List<String> getEmployeeList() {return employeeList;}public void setEmployeeList(List<String> employeeList) {this.employeeList = employeeList;}public Map<String, String> getEmployeeMap() {return employeeMap;}public void setEmployeeMap(Map<String, String> employeeMap) {this.employeeMap = employeeMap;} }
showresult.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></head> <body><h1>OGNL访问演示</h1><h2>1,基本属性</h2><h3>ID:<s:property value="id"/></h3><h3>姓名:<s:property value="name"/></h3><h2>2,Action中的对象</h2><h3>user的姓名:<s:property value="user.name"/></h3><h3>user的性别:<s:property value="user.sex"/></h3><h3>user的年龄:<s:property value="user.getAge()"/></h3><h2>3,list</h2> <h3>集合中的员工:<s:property value="employeeList[1]"/></h3><h2>4,Map</h2> <h3>Map中的员工工资:<s:property value="employeeMap.黄药师"/></h3> <h2>5,访问时运算</h2> <h3>My Name:<s:property value="'My name is '+name"/></h3> <h2>6,访问时调用方法</h2> <h3>MY NAME:<s:property value="name.toUpperCase()"/></h3> <h2>7,创建集合</h2> <h3>创建集合:<s:property value="{'a','b','c'}"/></h3> <h3>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h3> <h2>8,创建Map</h2> <h3>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h3> <h3>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h3><h2>9,静态方法和属性</h2> <h3>静态方法:<s:property value="@com.OGNL.OGNLAction@getstatic()"/></h3><h3>静态属性:<s:property value="@com.OGNL.OGNLAction@str"/></h3></body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><!-- 访问action的静态方法和属性,一定要加上这两句常量 --><constant name="struts.devMode" value="true"/><constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant><package name="pp" extends="struts-default"><action name="ognlaction" class="com.OGNL.OGNLAction"><result name="success">/showresult.jsp</result></action></package>
</struts>
这篇关于struts2的OGNL表达式(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!