java struts2值栈ognl_Struts2的值栈和OGNL牛逼啊
Struts2的值棧和OGNL牛逼啊
一 值棧簡介:
值棧是對應(yīng)每個請求對象的一套內(nèi)存數(shù)據(jù)的封裝,Struts2會給每個請求創(chuàng)建一個新的值棧,值棧能夠線程安全的為每個請求提供公共的數(shù)據(jù)存取服務(wù)。
二 OGNL介紹:
(1)基本數(shù)據(jù):
OGNL 是對象圖導(dǎo)航語言O(shè)bject-GraphNavigationLanguage的縮寫,它是一種功能強大的表達(dá)式語言。
OGNL 訪問ValueStack數(shù)據(jù)
OGNL 訪問ActionContext數(shù)據(jù)
訪問某個范圍下的數(shù)據(jù)要用#
#parameters 請求參數(shù) request.getParameter(...);
#request 請求作用域中的數(shù)據(jù) request.getAttribute(...);
#session 會話作用域中的數(shù)據(jù) session.getAttribute(...);
#application 應(yīng)用程序作用域中的數(shù)據(jù) application.getAttribute(...);
#attr 按照 page request session application 順序查找值
我們以例子理解這部分內(nèi)容,設(shè)置HelloAction:
1 public class HelloAction extendsActionSupport{2 private static final long serialVersionUID = 1L;3 @Override4 public String execute() throwsException {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 session=actionContext.getSession();12 session.put("name","王五(session)");13 session.put("age","13");14 //application中的內(nèi)容
15 Map application=actionContext.getApplication();16 application.put("name", "趙六(application)");17 application.put("age","14");18 returnSUCCESS;19 }20 }
Struts.xml文件的配置:
1
2
3
4
5 success.jsp
6
7
8
前端頁面success.jsp
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
Insert title here9
10
11 request.setAttribute("name","李四(request)");12 request.setAttribute("age","12");13 %>
14
15 值棧 獲取的數(shù)據(jù):
16
17
18 前臺傳遞的數(shù)據(jù):
19
20
21 request中的數(shù)據(jù):
22
23
24 session中的數(shù)據(jù):
25
26
27 application的數(shù)據(jù):
28
29
30 attr取值 :
31
32
33
34
首先,是取值方式,
①值棧 直接取 比如說是name age就可以使用這種方式value=”name”??value=”age”
②page頁面?zhèn)鬟f的數(shù)據(jù) 比如說是name age使用這種方式value="#parameters.name”??value="#parameters.age”
③requset設(shè)置的值 使用的方式?value="#request.name"????value="#request.age"
④session設(shè)置的值使用的方式?value="#session.name"??????value="#session.age"
⑤application設(shè)置的值使用的方式value="#application.name"???value="#application.age"
之后attr的取值方式是按照page request session applicaiton這個順序取得。
例如:attr獲取的是request的值
(2)OGNL訪問靜態(tài)方法和屬性
Mystatic類:
1 public classMyStatic {2
3 public static final String str="yxs";4
5 public staticString printUrl(){6
7 System.out.println("http://www.yxs.com");8
9 return "http://www.yxs.com";10
11 }12
13 }
Static.jsp直接訪問:
1
2 訪問靜態(tài)屬性:
3 訪問靜態(tài)方法:
4
結(jié)果:
(3)OGNL 訪問復(fù)雜對象
我們以javaBean對象為例:Student類
1 public classStudent {2 privateString name;3 private intage;4 publicStudent() {5 super();6 //TODO Auto-generated constructor stub
7 }8 public Student(String name, intage) {9 super();10 this.name =name;11 this.age =age;12 }13 publicString getName() {14 returnname;15 }16 public voidsetName(String name) {17 this.name =name;18 }19 public intgetAge() {20 returnage;21 }22 public void setAge(intage) {23 this.age =age;24 }25 }
Success.jsp文件:
1
2
3 ognl的javaBean值:
4
5
6 ognl的List集合:
7
8
9
10
11
12
13
14
15 ognl的Map:
16
17
18
19
20
21
22
HelloAction文件代碼:
1 public class HelloAction extendsActionSupport{2 private static final long serialVersionUID = 1L;3 private Student student;//javaBean
4 private Liststudents;//list
5 private MapstudentMap;//Map
6 publicStudent getStudent() {7 returnstudent;8 }9
10 public voidsetStudent(Student student) {11 this.student =student;12 }13
14 public ListgetStudents() {15 returnstudents;16 }17
18 public void setStudents(Liststudents) {19 this.students =students;20 }21
22 public MapgetStudentMap() {23 returnstudentMap;24 }25
26 public void setStudentMap(MapstudentMap) {27 this.studentMap =studentMap;28 }29
30 @Override31 public String execute() throwsException {32 //TODO Auto-generated method stub
33
34 students=new ArrayList();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();40 studentMap.put("goodStudent", new Student("學(xué)霸",20));41 studentMap.put("badStudent", new Student("學(xué)渣",19));42 returnSUCCESS;43 }44 }
顯示結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的java struts2值栈ognl_Struts2的值栈和OGNL牛逼啊的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git 本地推送本地仓库到远程
- 下一篇: excel查重复_智学网怎么登录 智学网