值棧是對應每一個請求對象的一套內存數據的封裝,Struts2會給每一個請求建立一個新的值棧,值棧可以線程安全的爲每一個請求提供公共的數據存取服務。html
(1)基本數據:前端
OGNL 是對象圖導航語言 Object-GraphNavigationLanguage 的縮寫,它是一種功能強大的表達式語言。 java
OGNL 訪問 ValueStack 數據 <s:propertyvalue=」account」/>安全
OGNL 訪問 ActionContext 數據 session
訪問某個範圍下的數據要用# app
#parameters 請求參數 request.getParameter(...);
#request 請求做用域中的數據 request.getAttribute(...);
#session 會話做用域中的數據 session.getAttribute(...);
#application 應用程序做用域中的數據 application.getAttribute(...);
#attr 按照 page request session application 順序查找值
咱們以例子理解這部份內容,設置HelloAction:jsp
1 public class HelloAction extends ActionSupport{ 2 private static final long serialVersionUID = 1L; 3 @Override 4 public String execute() throws Exception { 5 //狹義上的值棧
6 ActionContext actionContext=ActionContext.getContext(); 7 ValueStack valueStack=actionContext.getValueStack(); 8 valueStack.set("name", "張三(ValueStack)"); 9 valueStack.set("age", 11); 10 //session中的值
11 Map<String, Object> session=actionContext.getSession(); 12 session.put("name","王五(session)"); 13 session.put("age","13"); 14 //application中的內容
15 Map<String, Object> application=actionContext.getApplication(); 16 application.put("name", "趙六(application)"); 17 application.put("age","14"); 18 return SUCCESS; 19 } 20 }
Struts.xml文件的配置:ide
1 <struts>
2 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
3 <package name="manage" namespace="/" extends="struts-default">
4 <action name="hello" class="com.java1234.Action.HelloAction">
5 <result name="success" >success.jsp</result>
6 </action>
7 </package>
8 </struts>
前端頁面success.jspui
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <%@taglib prefix="s" uri="/struts-tags" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <title>Insert title here</title>
9 </head>
10 <%
11 request.setAttribute("name", "李四(request)"); 12 request.setAttribute("age", "12"); 13 %>
14 <body>
15 值棧 獲取的數據:<s:property value="name"/>
16 <s:property value="age"/>
17 <br/>
18 前臺傳遞的數據:<s:property value="#parameters.name"/>
19 <s:property value="#parameters.age"/>
20 <br/>
21 request中的數據:<s:property value="#request.name"/>
22 <s:property value="#request.age"/>
23 <br/>
24 session中的數據:<s:property value="#session.name"/>
25 <s:property value="#session.age"/>
26 <br/>
27 application的數據: <s:property value="#application.name"/>
28 <s:property value="#application.age"/>
29 <br/>
30 attr取值 : <s:property value="#attr.name"/>
31 <s:property value="#attr.age"/>
32 <br/>
33 </body>
34 </html>
首先,是取值方式<s:property value="方式"/>,this
①值棧 直接取 好比說是name age 就能夠使用這種方式 value=」name」 value=」age」
②page頁面傳遞的數據 好比說是name age 使用這種方式 value="#parameters.name」 value="#parameters.age」
③requset 設置的值 使用的方式 value="#request.name" value="#request.age"
④session設置的值使用的方式 value="#session.name" value="#session.age"
⑤application設置的值使用的方式 value="#application.name" value="#application.age"
以後attr的取值方式是按照 page request session applicaiton這個順序取得。
例如:attr獲取的是request的值
Mystatic類:
1 public class MyStatic { 2
3 public static final String str="yxs"; 4
5 public static String printUrl(){ 6
7 System.out.println("http://www.yxs.com"); 8
9 return "http://www.yxs.com"; 10
11 } 12
13 }
Static.jsp直接訪問:
1 <body>
2 訪問靜態屬性: <s:property value="@com.java1234.common.MyStatic@str"/><br/>
3 訪問靜態方法:<s:property value="@com.java1234.common.MyStatic@printUrl()"/>
4 </body>
結果:
咱們以javaBean對象爲例:Student類
1 public class Student { 2 private String name; 3 private int age; 4 public Student() { 5 super(); 6 // TODO Auto-generated constructor stub
7 } 8 public Student(String name, int age) { 9 super(); 10 this.name = name; 11 this.age = age; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public int getAge() { 20 return age; 21 } 22 public void setAge(int age) { 23 this.age = age; 24 } 25 }
Success.jsp文件:
1 <html>
2 <body>
3 ognl的javaBean值: <s:property value="student.name"/>
4 <s:property value="student.age"/>
5 <br/>
6 ognl的List集合: <s:property value="students[0].name"/>
7 <s:property value="students[0].age"/>
8 <br/>
9 <s:property value="students[1].name"/>
10 <s:property value="students[1].age"/>
11 <br/>
12 <s:property value="students[2].name"/>
13 <s:property value="students[2].age"/>
14 <br/>
15 ognl的Map: <s:property value="studentMap['goodStudent'].name"/>
16 <s:property value="studentMap['goodStudent'].age"/>
17 <br/>
18 <s:property value="studentMap['badStudent'].name"/>
19 <s:property value="studentMap['badStudent'].age"/>
20 <br/>
21 </body>
22 </html>
HelloAction文件代碼:
1 public class HelloAction extends ActionSupport{ 2 private static final long serialVersionUID = 1L; 3 private Student student;//javaBean
4 private List<Student>students;//list
5 private Map<String,Student>studentMap;//Map
6 public Student getStudent() { 7 return student; 8 } 9
10 public void setStudent(Student student) { 11 this.student = student; 12 } 13
14 public List<Student> getStudents() { 15 return students; 16 } 17
18 public void setStudents(List<Student> students) { 19 this.students = students; 20 } 21
22 public Map<String, Student> getStudentMap() { 23 return studentMap; 24 } 25
26 public void setStudentMap(Map<String, Student> studentMap) { 27 this.studentMap = studentMap; 28 } 29
30 @Override 31 public String execute() throws Exception { 32 // TODO Auto-generated method stub
33
34 students=new ArrayList<Student>(); 35 student=new Student("小八",12); 36 students.add(new Student("小酒",13)); 37 students.add(new Student("小石",14)); 38 students.add(new Student("十一",15)); 39 studentMap=new HashMap<String,Student>(); 40 studentMap.put("goodStudent", new Student("學霸",20)); 41 studentMap.put("badStudent", new Student("學渣",19)); 42 return SUCCESS; 43 } 44 }
顯示結果: