我想要的是什么:我想从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>
我遇到以下行为:
案例1 :当我将此(下面)代码放在index.jsp
中时,我会打印 否 值。
<s:action name="one"/>
<s:property value="list_fruits[0]"/>
= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
案例2 :当我将此(下面)代码放在index.jsp
中时,该值会从one.jsp
打印出来,因为在此场景中我包含了属性executeResult=”true”
< / p>
<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中,没有这样的问题。为什么会这样?谁能指导我?
答案 0 :(得分:1)
AleksandrM已经回复了你,但是你说了
仍不清楚。 :(请解释。
您可以将其用于:
executeResult="true"
获取结果; 调用Action并将其推送到具有var
属性的主页ValueStack,然后使用#
引用它:
<s:action name="one" var="instance" />
<s:property value="#instance.list_fruits[0]"/>
调用在请求/会话范围内设置内容的Action,然后使用#attr
或#session
;
但是你可以在服务器这一方面做任何事情,并且更好。
这就是为什么你不应该使用<s:action/>
标签:它打破了大多数S2框架约定和机制,这是一个不好的做法(或者至少它不是一个好的)。