所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)
第一节:值栈简介
值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。
值栈能够线程安全地为每个请求提供公共的数据存取服务。
第二节:OGNL 引入
OGNL 是对象图导航语言Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。
OGNL 访问ValueStack 数据
注:这里要添加struts的标签库
OGNL 访问ActionContext 数据
访问某个范围下的数据要用#
#parameters 请求参数request.getParameter(...);
#request 请求作用域中的数据request.getAttribute(...);
#session 会话作用域中的数据session.getAttribute(...);
#application 应用程序作用域中的数据application.getAttribute(...);
#attr 按照page request session application 顺序查找值
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6
7
8
9
10
11 success.jsp
12
13
14
15
HelloAction.java
1 packagecom.wishwzp.action;2
3 importjava.util.Map;4
5 importcom.opensymphony.xwork2.ActionContext;6 importcom.opensymphony.xwork2.ActionSupport;7 importcom.opensymphony.xwork2.util.ValueStack;8
9 public class HelloAction extendsActionSupport{10
11 /**
12 *13 */
14 private static final long serialVersionUID = 1L;15
16
17
18 @Override19 public String execute() throwsException {20 //获取ActionContext
21 ActionContext actionContext=ActionContext.getContext();22 //获取狭义上的值栈23 //值栈是用来存储数据的
24 ValueStack valueStack=actionContext.getValueStack();25 valueStack.set("name", "张三(valueStack)");26 valueStack.set("age", 11);27
28 Map session=actionContext.getSession();29 session.put("name", "王五(session)");30 session.put("age", 13);31
32 Map application=actionContext.getApplication();33 application.put("name", "赵六(application)");34 application.put("age", 14);35 returnSUCCESS;36 }37 }
success.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10 request.setAttribute("name","李四(request)");11 request.setAttribute("age","12");12 %>
13
14
15 获取狭义上的值栈数据:
16
17 请求参数:
18
19 request:
20
21 session:
22
23 application:
24
25 attr取值:
26
27
28
url:http://localhost:8080/Struts2Chap04/hello?name=ssss&age=23
结果:
第三节:OGNL 访问复杂对象
1,访问javaBean 对象;
2,访问集合对象;
3,访问Map 对象;
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6
7
8
9
10
11 success.jsp
12
13
14
15
Student.java
1 packagecom.wishwzp.model;2
3 public classStudent {4
5 privateString name;6 private intage;7
8 publicStudent() {9 super();10 //TODO Auto-generated constructor stub
11 }12 public Student(String name, intage) {13 super();14 this.name =name;15 this.age =age;16 }17
18 publicString getName() {19 returnname;20 }21 public voidsetName(String name) {22 this.name =name;23 }24 public intgetAge() {25 returnage;26 }27 public void setAge(intage) {28 this.age =age;29 }30
31
32 }
HelloAction.java
1 packagecom.wishwzp.action;2
3 importjava.util.ArrayList;4 importjava.util.HashMap;5 importjava.util.List;6 importjava.util.Map;7
8 importcom.wishwzp.model.Student;9 importcom.opensymphony.xwork2.ActionContext;10 importcom.opensymphony.xwork2.ActionSupport;11 importcom.opensymphony.xwork2.util.ValueStack;12
13 public class HelloAction extendsActionSupport{14
15 /**
16 *17 */
18 private static final long serialVersionUID = 1L;19
20 //访问javaBean对象,并get...和set...
21 privateStudent student;22
23 //访问集合对象,并get...和set...
24 private Liststudents;25
26 //访问Map对象,并get...和set...
27 private MapstudentMap;28
29 public MapgetStudentMap() {30 returnstudentMap;31 }32
33 public void setStudentMap(MapstudentMap) {34 this.studentMap =studentMap;35 }36
37 public ListgetStudents() {38 returnstudents;39 }40
41 public void setStudents(Liststudents) {42 this.students =students;43 }44
45 publicStudent getStudent() {46 returnstudent;47 }48
49 public voidsetStudent(Student student) {50 this.student =student;51 }52
53 @Override54 public String execute() throwsException {55
56 //javaBean对象
57 student=new Student("小扒", 12);58
59 //集合对象
60 students=new ArrayList();61 students.add(new Student("老九",13));62 students.add(new Student("老十",14));63
64 //Map对象
65 studentMap=new HashMap();66 studentMap.put("goodStudent", new Student("学霸",20));67 studentMap.put("badStudent", new Student("学渣",19));68 returnSUCCESS;69 }70
71 }
success.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11 ognl访问javaBean对象:
12
13
14 ognl访问List集合:
15
16
17
18
19 ognl访问Map:
20
21
22
23
24
url访问:http://localhost:8080/Struts2Chap04/hello
结果:
第四节:OGNL 访问静态方法和属性
1,访问静态属性;
2,访问静态方法;需要在struts.xml加上
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6
7
8
9
10
11
12
13
14
MyStatic.java
1 packagecom.wishwzp.common;2
3 public classMyStatic {4
5 //静态属性
6 public static final String str="Struts2开心学习";7
8 //静态方法
9 public staticString printUrl(){10 System.out.println("http://www.baidu.com");11 return "http://www.baidu.com";12 }13 }
ognl_static.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11 访问静态属性:
12 访问静态方法:
13
14
url:http://localhost:8080/Struts2Chap04/ognl_static.jsp
结果: