值栈是对应每一个请求对象的一套内存数据的封装,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 }
显示结果: