OGNL特殊符号的使用

---------------------siwuxie095

   

   

   

   

   

   

   

# 的使用

   

   

1、使用 # 获取 context 中的数据

   

「值栈分为 root 和 context 两部分」

   

   

   

2、如:向 Request 域放值,在页面中使用 OGNL 表达式获取值

   

1)编写 Action

   

   

   

@Override

public String execute() throws Exception {

HttpServletRequest request=ServletActionContext.getRequest();

request.setAttribute("reqName", "reqValue");

return SUCCESS;

}

   

   

   

2)编写页面

   

   

   

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!-- 引入 Struts2 标签库 -->

<%@ taglib uri="/struts-tags" prefix="s"%>

   

<!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>OGNL</title>

</head>

<body>

   

<!--

因为是获取 context 中的数据,所以写 OGNL 表达式时

要先添加特殊符号 #,再添加 context 中的 Key 名称,

最后添加放到域对象中的数据的名称

#request.reqName

注意:context Map 结构,即 Key-Value

-->

<s:property value="#request.reqName"></s:property>

</body>

</html>

   

   

   

   

   

   

   

% 的使用

   

   

1、不能直接在 Struts2 的表单标签中写 OGNL 表达式,会识别

不出来,只有加上 % 后才能识别

   

   

2、如:向 Request 域放值,在页面中使用 OGNL 表达式获取值

   

1)编写 Action(同上)

   

   

2)编写页面

   

   

   

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!-- 引入 Struts2 标签库 -->

<%@ taglib uri="/struts-tags" prefix="s"%>

   

<!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>OGNL</title>

</head>

<body>

<!-- % 强制解析 OGNL 表达式 -->

<s:textfield name="username" value="%{#request.reqName}"></s:textfield>

<!-- % 强制不解析 OGNL 表达式 -->

<s:textfield name="username" value="%{'#request.reqName'}"></s:textfield>

</body>

</html>

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/7358889.html