Struts2学习第3天--OGNL、EL、值栈




JAVA中的OGNL:
1 调用对象的方法:

2 访问对象的静态方法:

3 获取OGNLContext、Root中的数据。

User:

4 访问Context:

关键还是在Struts2环境中的使用:





并没有打印 静态方法的值,因为Struts2默认关闭了。

再次刷新后发现有值了。






编写demo



debug启动 打断点发现: root和context均在这里面。




修改demo返回值



运行:





方式1:




方式2:




如果有多个user 默认展示栈顶的!!!


没有set get方法 无法查看 但是已经在栈顶了。


获取值栈的数据:





在debug里可以找到:






request中找不到 就自动去值栈中寻找。









修改crm


jsp中去掉jstl 引入struts标签库 然后不用foreach循环 改用s标签 。

今日代码:

package com.itheima.struts2.valuestack;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* ValueStack内部结构
* @author ZWT
*
*/
public class ValueStackDemo1 extends ActionSupport{
@SuppressWarnings("unused")
@Override
public String execute() throws Exception {
//获得值栈
ValueStack valueStack = ActionContext.getContext().getValueStack();
return SUCCESS;
}
}
package com.itheima.struts2.valuestack;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* 获得ValueStack对象
* @author ZWT
*
*/
public class ValueStackDemo2 extends ActionSupport{
@SuppressWarnings("unused")
@Override
public String execute() throws Exception {
//获得值栈
ValueStack valueStack1 = ActionContext.getContext().getValueStack();
//2
ValueStack valueStack2 = (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
//一个action实例只会创建一个valuestack
System.out.println(valueStack1 == valueStack2 );
return NONE;
}
}
package com.itheima.struts2.valuestack;
import org.apache.struts2.ServletActionContext;
import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* 操作ValueStack
* @author ZWT
*
*/
public class ValueStackDemo3 extends ActionSupport{
private User user;
public User getUser() {
return user;
}
@SuppressWarnings("unused")
@Override
public String execute() throws Exception {
user = new User("张飞","222");
return SUCCESS;
}
}
package com.itheima.struts2.valuestack;
import org.apache.struts2.ServletActionContext;
import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* 操作ValueStack :方式2 调用值栈中的方法实现
* @author ZWT
*
*/
public class ValueStackDemo4 extends ActionSupport{
@SuppressWarnings("unused")
@Override
public String execute() throws Exception {
ValueStack valueStack1 = ActionContext.getContext().getValueStack();
//使用push(Object) set(String key, Object obj)
User user = new User("张飞","56");
//user现在在栈顶位置
valueStack1.push(user);
//创建map集合 map压入栈顶
valueStack1.set("name", "关羽");
return SUCCESS;
}
}
package com.itheima.struts2.valuestack;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* 获取值栈的数据
* @author ZWT
*
*/
public class ValueStackDemo5 extends ActionSupport{
@SuppressWarnings("unused")
@Override
public String execute() throws Exception {
ValueStack valueStack1 = ActionContext.getContext().getValueStack();
//向值栈中保存一个对象 获取出来
User user = new User("张飞","123");
valueStack1.push(user);
//保存集合
List<User> list = new ArrayList<User>();
list.add(new User("aaa","111"));
list.add(new User("bbb","222"));
list.add(new User("ccc","333"));
valueStack1.set("list", list);
//往context中存
ServletActionContext.getRequest().setAttribute("name", "www");
ServletActionContext.getRequest().getSession().setAttribute("name", "eee");
ServletActionContext.getServletContext().setAttribute("name", "rrr");
return SUCCESS;
}
}
<?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> <package name="demo1" extends="struts-default" namespace="/"> <action name="valueStackDemo1" class="com.itheima.struts2.valuestack.ValueStackDemo1"> <result>/demo1/success.jsp</result> </action> <action name="valueStackDemo2" class="com.itheima.struts2.valuestack.ValueStackDemo2"> </action> <action name="valueStackDemo3" class="com.itheima.struts2.valuestack.ValueStackDemo3"> <result>/demo1/success.jsp</result> </action> <action name="valueStackDemo4" class="com.itheima.struts2.valuestack.ValueStackDemo4"> <result>/demo1/success.jsp</result> </action> <action name="valueStackDemo5" class="com.itheima.struts2.valuestack.ValueStackDemo5"> <result>/demo1/success2.jsp</result> </action> </package> </struts>
<?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> <!-- 配置Struts2的常量 --> <constant name="struts.action.extension" value="action"/> <!--开启静态方法访问 --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <include file="com/itheima/struts2/valuestack/struts_demo1.xml"></include> </struts>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!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>
</head>
<body>
<h1>OGNL在Struts2环境中的入门</h1>
<h3>调用对象的方法</h3>
<s:property value="'struts'.length()"/>
<h3>调用对象的静态方法</h3>
<s:property value="@java.lang.Math@random()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!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>
</head>
<body>
<h1>查看值栈的内部结构</h1>
<s:debug></s:debug>
<!-- 方式1获取 利用action在值栈中的特性 -->
<%-- <s:property value="user.username"/>
<s:property value="user.password"/> --%>
<!-- 方式2获取 利用valuestack本身方法 -->
<s:property value="username"/>
<s:property value="password"/>
<s:property value="name"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!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>
</head>
<body>
<h1>查看值栈的内部结构</h1>
<s:debug></s:debug>
<!--获取对象的属性 -->
<s:property value="username"/>
<s:property value="password"/>
<!-- 获取集合当中的数据 -->
<s:property value="list[0].username"/>
<s:property value="list[0].password"/>
<s:property value="list[1].username"/>
<s:property value="list[1].password"/>
<s:property value="list[2].username"/>
<s:property value="list[2].password"/>
<!-- 获取context中的数据 -->
<s:property value="#request.name"/>
<s:property value="#session.name"/>
<s:property value="#application.name"/>
<hr>
${ username }
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!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>
</head>
<body>
<h1>#号的用法</h1>
<h3>用途1:获取context的数据</h3>
<%
request.setAttribute("name","李兵");
%>
<s:property value="#request.name"/>
<h3>构建map集合</h3>
<!-- list i只要定义 context中也有-->
<s:iterator var="i" value="{'aa','bb','cc'}">
<s:property value="i"/>--<s:property value="#i"/><br>
</s:iterator>
<!-- map -->
<s:iterator var="entry" value="#{'aa':'11','bb':'22','cc':'33' }">
<s:property value="key"/>--<s:property value="value"/><br>
<s:property value="#entry.key"/>--<s:property value="#entry.value"/><br>
</s:iterator>
<hr>
<!-- value值 和 外面的‘男’ 一样就用list 不然用map -->
<%-- 性别:<input type="radio" name="sex1" value="男">男
<input type="radio" name="sex1" value="女">女
<hr>
<s:radio list="{'男','女'}" name="sex2" label="姓名" /> --%>
<hr>
性别:<input type="radio" name="sex1" value="1">男
<input type="radio" name="sex1" value="2">女
<hr>
<s:radio list="#{'1':'男','2':'女'}" name="sex2" label="姓名" />
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!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>
</head>
<body>
<h1>%号的用法</h1>
<h3></h3>
<%
request.setAttribute("name", "张飞");
%>
<s:property value="#request.name"/><br>
姓名:<s:textfield name="name" value="%{#request.name}"></s:textfield><br>
<s:property value="%{'#request.name'}"/><br>
</body>
</html>

浙公网安备 33010602011771号