打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
值栈和OGNL使用
引入标签
<%@taglib prefix="s" uri="/struts-tags" %>

参考项目

值栈  
值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。
值栈能够线程安全地为每个请求提供公共的数据存取服务。

OGNL 引入
 OGNL 是对象图导航语言 Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。
OGNL 访问 ValueStack 数据 <s:property value=”account” />
OGNL
访问 ActionContext 数据
访问某个范围下的数据要用
#
#parameters
请求参数 request.getParameter(...)
#request 请求作用域中的数据 request.getAttribute(...)
#session 会话作用域中的数据 session.getAttribute(...)
#application 应用程序作用域中的数据 application.getAttribute(...)
#attr 按照 page  request.getAttribute()  session  application 顺序查找值  

Action层
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class HelloAction extends ActionSupport{
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
ActionContext actionContext=ActionContext.getContext();
// 获取狭义上的值栈
ValueStack valueStack=actionContext.getValueStack();
valueStack.set("name", "张三(valueStack)");
valueStack.set("age", 11);
Map<String, Object> session=actionContext.getSession();
session.put("name", "王五(session)");
session.put("age", 13);
Map<String, Object> application=actionContext.getApplication();
application.put("name", "赵六(application)");
application.put("age", 14);
student=new Student("小扒", 12); 
        
students=new ArrayList<Student>(); 
students.add(new Student("老九",13)); 
students.add(new Student("老十",14)); 

studentMap=new HashMap<String,Student>(); 
studentMap.put("goodStudent", new Student("学霸",20)); 
studentMap.put("badStudent", new Student("学渣",19)); 
return SUCCESS;
}
}

JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html 
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
request.setAttribute("name", "李四(request)");
request.setAttribute("age", "12");
%>
</head>
<body>

获取狭义上的值栈数据:
<s:property value="name"/>
<s:property value="age"/><br/>

请求参数:
<s:property value="#parameters.name"/>
<s:property value="#parameters.age"/><br/>

request:  
<s:property value="#request.name"/>
<s:property value="#request.age"/><br/>

session:  
<s:property value="#session.name"/>
<s:property value="#session.age"/><br/>

application:
<s:property value="#application.name"/>
<s:property value="#application.age"/><br/>

attr取值:
<s:property value="#attr.name"/>
<s:property value="#attr.age"/><br/>

ognl访问javaBean对象:
<s:property value="student.name"/>
<s:property value="student.age"/><br/>

ognl访问List集合:
<s:property value="students[0].name"/>
<s:property value="students[0].age"/><br/>
<s:property value="students[1].name"/>
<s:property value="students[1].age"/><br/>

ognl访问Map:
<s:property value="studentMap['goodStudent'].name"/>
<s:property value="studentMap['goodStudent'].age"/><br/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/><br/>

</body>
</html>

OGNL 访问静态方法和属性
package com.java.common;
public class MyStatic {
public static final String str="Java知识";
public static String printUrl(){
System.out.println("http://www.java.com");
return "http://www.java.com";
}
}  

Struts.XML
  <!-- 访问静态属性要提取的常量 -->
  <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 

JSP
访问静态属性: <s:property value="@com.java.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.java.common.MyStatic@printUrl()"/>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Struts中OGNL与EL表达式
Ognl表达式基本原理和使用方法
5、struts2值栈、命名参数与OGNL应用开发
struts2 OGNL详解
Struts2 ONGL表达式
OGNL 摘要
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服