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"> <!-- 启动struts2框架 --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
UserEntity 实体层
package com.tjitcast.model; import java.util.Date; public class UserEntity { public Integer id; public String name; public Double score; public Boolean gender; public Date date; public UserEntity(){ } public UserEntity(Integer id, String name, Double score, Boolean gender, Date date) { this.id = id; this.name = name; this.score = score; this.gender = gender; this.date = date; } /** * @return the id */ public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the score */ public Double getScore() { return score; } /** * @param score the score to set */ public void setScore(Double score) { this.score = score; } /** * @return the gender */ public Boolean getGender() { return gender; } /** * @param gender the gender to set */ public void setGender(Boolean gender) { this.gender = gender; } /** * @return the date */ public Date getDate() { return date; } /** * @param date the date to set */ public void setDate(Date date) { this.date = date; } public String info(){ return "该方法是JavaBean中的普通方法"; } }
OgnlAction 控制器
package com.tjitcast.action; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.tjitcast.model.UserEntity; public class OgnlAction extends ActionSupport{ private String loginName; private int age; private UserEntity user; private List<String> list1; private Set<String> set1; private Map<String,String> map; private List<UserEntity> listUser; /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() */ @Override public String execute() throws Exception { this.loginName="张三"; this.age=25; user=new UserEntity(100, "李四", 88.56, true, new Date()); list1=new ArrayList<String>(); list1.add("apple"); list1.add("baner"); list1.add("ccccc"); set1=new LinkedHashSet<String>(list1); map=new HashMap<String,String>(); map.put("a","javaSE"); map.put("b", "javaEE"); map.put("c", "javaME"); listUser=new ArrayList<UserEntity>(); listUser.add(new UserEntity(1001,"王五1",85.25,true,new Date())); listUser.add(new UserEntity(1002,"王五2",82.25,true,new Date())); listUser.add(new UserEntity(1003,"王五3",90.25,true,new Date())); ActionContext context=ActionContext.getContext(); context.put("req","req中的数据"); context.getSession().put("session1", "session中的数据"); context.getApplication().put("app", "application中的数据"); ServletActionContext.getRequest().setAttribute("req2", "servlet中request中的数据"); ServletActionContext.getRequest().getSession().setAttribute("session2", "Servlet中session中的数据"); ServletActionContext.getServletContext().setAttribute("app2", "Servet的Application"); return "ognl"; } public String display(String name){ return name+"这是Action中的普通方法!!!"; } /** * @return the loginName */ public String getLoginName() { return loginName; } /** * @param loginName the loginName to set */ public void setLoginName(String loginName) { this.loginName = loginName; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the user */ public UserEntity getUser() { return user; } /** * @param user the user to set */ public void setUser(UserEntity user) { this.user = user; } /** * @return the list1 */ public List<String> getList1() { return list1; } /** * @param list1 the list1 to set */ public void setList1(List<String> list1) { this.list1 = list1; } /** * @return the set1 */ public Set<String> getSet1() { return set1; } /** * @param set1 the set1 to set */ public void setSet1(Set<String> set1) { this.set1 = set1; } /** * @return the map */ public Map<String, String> getMap() { return map; } /** * @param map the map to set */ public void setMap(Map<String, String> map) { this.map = map; } /** * @return the listUser */ public List<UserEntity> getListUser() { return listUser; } /** * @param listUser the listUser to set */ public void setListUser(List<UserEntity> listUser) { this.listUser = listUser; } }
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置后缀名称 --> <constant name="struts.action.extension" value="action,todo,go,aa,cc"></constant> <!-- 配置是否自动加载struts.xml文件 --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 是否显示更多的调试信息 --> <constant name="struts.devMode" value="true"></constant> <!-- 设置action标签的name属性支持/斜杠 --> <constant name="struts.enable.SlashesInActionNames" value="false"></constant> <!-- 支持动态方法的调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 支持ONGL静态方法的调用 --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> <package name="myPackage" namespace="/" extends="struts-default"> <action name="ognl" class="com.tjitcast.action.OgnlAction"> <result name="ognl">/ognl.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> </struts>
index.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <title>Struts2中 OGNL 的应用示例</title> </head> <body> <a href="ognl.action">Struts2 OGNL</a> </body> </html>
ognl.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@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> <title>OGNL 示例页面</title> </head> <body> OGNL访问Action中的普通属性:<s:property value="loginName"/> <s:property value="age"/><br/> OGNL访问JavaBean中的属性:<s:property value="user.name"/><br/> OGNL访问List集合对象:<s:property value="list1[2]"/><br/> OGNL访问Set集合对象:<s:property value="set1.toArray()[0]"/><br/> OGNL访问Map集合对象:<s:property value="map['b']"/> <s:property value="map.keys.toArray()[0]"/>=<s:property value="map.values.toArray()[0]"/><br/> OGNL访问List集合中的自定义对象: <s:property value="listUser[0].name"/><br/> OGNL访问JavaBean中的方法:<s:property value="user.info()"/><br/> OGNL访问Action中的方法:<s:property value="display('张三')"/><br/> OGNL访问静态的常量PI:<s:property value="@java.lang.Math@PI"/><br/> OGNL访问静态的方法:<s:property value="@@abs(100)"/><br/> OGNL访问构造方法:<s:property value="new java.util.Date()"/> <s:date name="new java.util.Date()" format="yyyy-MM-dd"/><br/> OGNL访问ActionContext中的数据 request:<s:property value="#request.req"/><br/> session:<s:property value="#session.session1"/><br/> application:<s:property value="#application.app"/><br/> OGNL访问ServletActionContext中的数据 request:<s:property value="#request.req2"/><br/> session:<s:property value="#session.session2"/><br/> application:<s:property value="#application.app2"/> attr:<s:property value="#attr.app2"/> <br/> OGNL的投影查询 查询出listUser集合中所有的用户名: <s:property value="listUser.{name}"/><br/> 查询出listUser集合中所有分数大于80的用户名: <s:property value="listUser.{?#this.score>80}.{name}"/><br/> 查询出listUser集合中所有分数大于80且性别为true的用户名: <s:property value="listUser.{?#this.score>80 && #this.gender==true}.{name}"/><br/> 查询出listUser集合中所有分数大于8080且性别为true的第一个用户名: <s:property value="listUser.{^#this.score>80 && #this.gender==true}.{name}"/><br/> 查询出listUser集合中所有分数大于8080且性别为true的最后一个用户名: <s:property value="listUser.{$#this.score>80 && #this.gender==true}.{name}"/><br/> </body> </html>