引入標簽 <%@taglib prefix="s" uri="/struts-tags" %> 參考項目 值棧是對應每個請求對象的一套內存數(shù)據(jù)的封裝,Struts2 會給每個請求創(chuàng)建一個新的值棧。 值棧能夠線程安全地為每個請求提供公共的數(shù)據(jù)存取服務。
OGNL 引入
OGNL 訪問 ValueStack 數(shù)據(jù) <s:property value=”account” />OGNL 是對象圖導航語言 Object-Graph Navigation Language 的縮寫,它是一種功能強大的表達式語言。 OGNL 訪問 ActionContext 數(shù)據(jù) 訪問某個范圍下的數(shù)據(jù)要用# #parameters 請求參數(shù) request.getParameter(...); #request 請求作用域中的數(shù)據(jù) request.getAttribute(...); #session 會話作用域中的數(shù)據(jù) session.getAttribute(...); #application 應用程序作用域中的數(shù)據(jù) application.getAttribute(...); #attr 按照 page request.getAttribute() session application 順序查找值 Action層 import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class HelloAction extends ActionSupport{ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { ActionContext actionContext=ActionContext.getContext(); // 獲取狹義上的值棧 ValueStack valueStack=actionContext.getValueStack(); valueStack.set("name", "張三(valueStack)"); valueStack.set("age", 11); Map<String, Object> session=actionContext.getSession(); session.put("name", "王五(session)"); session.put("age", 13); Map<String, Object> application=actionContext.getApplication(); application.put("name", "趙六(application)"); application.put("age", 14); student=new Student("小扒", 12); students=new ArrayList<Student>(); students.add(new Student("老九",13)); students.add(new Student("老十",14)); studentMap=new HashMap<String,Student>(); studentMap.put("goodStudent", new Student("學霸",20)); studentMap.put("badStudent", new Student("學渣",19)); return SUCCESS; } } JSP <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <% request.setAttribute("name", "李四(request)"); request.setAttribute("age", "12"); %> </head> <body> 獲取狹義上的值棧數(shù)據(jù): <s:property value="name"/> <s:property value="age"/><br/> 請求參數(shù): <s:property value="#parameters.name"/> <s:property value="#parameters.age"/><br/> request: <s:property value="#request.name"/> <s:property value="#request.age"/><br/> session: <s:property value="#session.name"/> <s:property value="#session.age"/><br/> application: <s:property value="#application.name"/> <s:property value="#application.age"/><br/> attr取值: <s:property value="#attr.name"/> <s:property value="#attr.age"/><br/> ognl訪問javaBean對象: <s:property value="student.name"/> <s:property value="student.age"/><br/> ognl訪問List集合: <s:property value="students[0].name"/> <s:property value="students[0].age"/><br/> <s:property value="students[1].name"/> <s:property value="students[1].age"/><br/> ognl訪問Map: <s:property value="studentMap['goodStudent'].name"/> <s:property value="studentMap['goodStudent'].age"/><br/> <s:property value="studentMap['badStudent'].name"/> <s:property value="studentMap['badStudent'].age"/><br/> </body> </html> OGNL 訪問靜態(tài)方法和屬性 package com.mon; public class MyStatic { public static final String str="Java知識"; public static String printUrl(){ System.out.println("http://www."); return "http://www."; } } Struts.XML <!-- 訪問靜態(tài)屬性要提取的常量 --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> JSP 訪問靜態(tài)屬性: <s:property value="@com.mon.MyStatic@str"/><br/> 訪問靜態(tài)方法:<s:property value="@com.mon.MyStatic@printUrl()"/> |
|
來自: 擎天豬mpnlajkd > 《Struts》