【发布时间】:2014-10-16 06:53:13
【问题描述】:
我想要什么:我想从List 集合中检索值。
我正在练习/学习 struts 2 框架。但是,我对OGNL 的行为感到困惑。
这些是我的文件:
Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hr>
<s:action name="one" />
<s:property value="list_fruits[0]" />
</body>
</html>
MyAction.java
package abc;
import java.util.*;
import org.apache.struts2.dispatcher.RequestMap;
import com.opensymphony.xwork2.*;
public class MyAction extends ActionSupport {
private List list_fruits;
public List getList_fruits() {
return list_fruits;
}
public void setList_fruits(List list_fruits) {
this.list_fruits = list_fruits;
}
public String doOne() {
list_fruits = new ArrayList();
list_fruits.add("banana");
list_fruits.add("apple");
list_fruits.add("mango");
/*RequestMap rm = (RequestMap) ActionContext.getContext().get("request");
rm.put("req_scope", list_fruits);*/
return "sendToOne";
}
}
one.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>ONE.JSP</h1>
<br>
<s:property value="list_fruits[0]" />
</body>
</html>
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="devMode" value="true" />
<package name="vns" extends="struts-default">
<action name="one" class="abc.MyAction" method="doOne">
<result name="sendToOne">/one.jsp</result>
</action>
</package>
</struts>
我遇到以下行为:
Case1:当我将这个(下面的)代码放入index.jsp 时,我得到NO 值打印。
<s:action name="one"/>
<s:property value="list_fruits[0]"/>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Case2:当我把这个(下面的)代码放在index.jsp 中时,值会从one.jsp 打印出来,因为在这个场景中我包含了属性executeResult=”true”
<s:action name="one" executeResult="true"/>
<s:property value="list_fruits[0]"/> <!-- still NOT printed here, but gets printed from one.jsp -->
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
案例 3:当我将这个(下面的)代码放入 MyAction.java 和 index.jsp 时,值会打印在屏幕上 (index.jsp)。
MyAction.java
RequestMap rm=(RequestMap)ActionContext.getContext().get("request");
rm.put("req_scope", list_fruits);
index.jsp
<s:action name="one"/> <!-- removed executeResult="true" -->
<s:property value="#request.req_scope[0]"/>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
由于 one.jsp 而不是由于 index.jsp 而打印 Case2 值
我想知道为什么在 Case1 中没有打印任何值,而在 Case2 和 Case3 中则没有这样的问题。为什么会这样?谁能指导我?
【问题讨论】:
-
您必须提供值或列表引用以将值打印到
并且在场景 1 中,您没有设置动作标签的属性以显示执行结果,也没有设置根据 OGNL 正确列出参考,因此您没有得到结果。 -
在场景1中,我在
<s:property>的value属性中给出了列表的引用。只是没有给出布尔值executeResult,'因为它不是强制性的。 Scene3 也是一样,那为什么打印在scene3 而不是scene1? -
范围,范围和范围。它没有打印在索引中,因为它不存在。顺便说一句,OGNL 根本不相关。
-
你能告诉我范围在这里是如何工作的吗?我的意思是为什么 case2 没有范围问题,而 case1 由于“范围”而受到影响。范围在这里如何工作。
-
@AleksandrM ,我想问 2 个问题:Ist:在 case1 中是否输入了
value stack中的列表。 IInd 我是否从 case2 中的值堆栈中获取值。我被卡住了,请澄清一下。
标签: java jsp struts2 valuestack