Struts2學習第三天——攔截器與檔案傳輸
文件版本 | 開發工具 | 測試平臺 | 工程名字 | 日期 | 作者 | 備註 |
---|---|---|---|---|---|---|
V1.0 | 2016.06.15 | lutianfei | none |
- 內容摘要:
1.國際化(瞭解)
2.攔截器(Interceptor)—重點
3.struts2檔案上傳與下載(次重點)
4.ognl與valuestack
國際化
國際化原理,什麼是國際化 ?
- 同一款軟體 可以為不同使用者,提供不同語言介面 —- 國際化軟體
- 需要一個語言資源包(很多properties檔案,每個properties檔案 針對一個國家或者語言 ,通過java程式根據來訪者國家語言,自動讀取不同properties檔案 )
Java原生國家化操作
- 資源包編寫
- properties檔案命名 :
基本名稱_語言(小寫)_國家(大寫).properties
- 例如 :
messages_zh_CN.properties
中國中文messages_en_US.properties
美國英文
- properties檔案命名 :
- ResourceBundle 根據不同Locale(地域資訊),讀取不同國家 properties檔案
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);
struts2中國際化
- struts2中對國際化進行了封裝,我們只需要根據其提供的API進行訪問就可以。
問題1:在struts2中國際化時properties檔案的定義
- 1.全域性 : 需要通過一個常量來宣告
- 預設全域性路徑:
- 第166行:
struts.custom.i18n.resources=testmessages,testmessages2
- 對於properties配置檔案可以放置在任意位置
- struts.xml配置如下:
- 當message.properties在src下:
<constant name="struts.custom.i18n.resources" value="message">
- 當message.properties在cn.itcast.i18n.resource包下:
<constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message">
- 當message.properties在src下:
- 2.區域性
- 1.針對於action類
- 位置:與action類在同一個包下.
- 名稱:
ActionClassName.properties
- 這個配置檔案只對當前action有效。
- 2.針對於package下所有action
- 位置:在指定的包下
- 名稱:package.properties
- 3.jsp頁面臨時使用某一個properties檔案.
- 在某個jsp頁面中加入 :
<s:i18n name="cn.itcast.action.package"></s:i18n>
- 在某個jsp頁面中加入 :
- 1.針對於action類
問題2:Struts2中國際化操作在哪些位置使用
- 1.action類中使用
- 2.配置檔案中使用
<validation.xml>
- 3.在jsp頁面上使用
問題3:Struts2中操作國際化
1.在action類中使用
- 前提:action類要繼承ActionSupport類。
- getText(String name)就可以獲取配置檔案中對應名稱的值。
eg :
// I18nDemo1Action.properties 中 msg=hello world
public String execute() throws Exception {
// 得到properties檔案中資訊.
System.out.println(this.getText("msg"));
return NONE;
}
//結果就是 hello world
- 2.在validation.xml檔案中使用
<message key="名稱"/>
- eg : I18nDemo2Action-validation.xml & xxx.properites
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="nameerror"></message>
</field-validator>
</field>
</validators>
//properties
//package_en_US.properties
nameerror=name required
//package_zh_CN.properties
nameerror=\u540D\u5B57\u5FC5\u987B\u5730
//package.properties
nameerror=name required
3.在jsp頁面上使用
<s:text name="名稱">
:這裡名稱
為對應的Action名稱。- 如果沒有使用
<s:i18n name="">
來指定,會從全域性配置檔案中獲取。 - 如果要從某一個配置檔案中獲取,通過name屬性來指定
包名.配置檔名稱
eg : i18n.jsp
<s:i18n name="cn.itcast.action.package">
<s:text name="nameerror"/>
</s:i18n>
<s:text name="name" /> //全域性包:cn.itcast.i18n.resource
//由struts.xml中常量配置決定:
//<constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message"></constant>
* Struts2中國際化配置檔案中使用動態文字*
1.action中動態文字使用
- I18nDemo1Action.properties 中 : msg=hello world {0}
- I18nDemo1Action 輸出: this.getText(“msg”,new String[]{“tom”})
eg :
public String execute() throws Exception {
// 得到properties檔案中資訊.
//System.out.println(this.getText("msg"));
//動態文字
System.out.println(this.getText("msg", new String[]{"tom"}));
return NONE;
}
//結果就是 hello world tom
- 2.jsp頁面上怎樣使用
- 在i18n.jsp 中加入 :
<s:i18n name="cn.itcast.action.I18nDemo1Action">
<s:text name="msg">
<s:param>張三</s:param>
</s:text>
</s:i18n>
* 結果就是 hello world 張三.
攔截器(interceptor)
- struts2攔截器使用的是AOP思想。
AOP的底層實現就是動態代理。
攔截器採用
責任鏈
模式- 在責任鏈模式裡,很多物件由每一個物件對其下家的引用而連線起來形成一條鏈。
- 責任鏈每一個節點,都可以繼續呼叫下一個節點,也可以阻止流程繼續執行
struts2中在
struts-default.xml
檔案中聲明瞭所有的攔截器。- 而struts2框架預設使用的是
defaultStack
這個攔截器棧。 - 在這個攔截器棧中使用了18個攔截器。
- 簡單說,struts2框架在預設情況下,載入了18個攔截器。
Struts2中使用攔截器
- 使用攔截器可以做什麼?
- 可以通過使用攔截器進行控制action的訪問。例如,許可權操作。
攔截器使用方法
- 1.建立一個Interceptor 可以自定義一個類實現
com.opensymphony.xwork2.interceptor.Interceptor
在這個介面中有三個方法
- init
- destory
- intercept 真正攔截的方法。
- 在intercept方法中如果要向下繼續執行,通過其引數ActionInvocation呼叫它的invoke()方法就可以。
public class _MyInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
System.out.println("my interceptor init");
}
public String doIntercept(ActionInvocation ai) throws Exception {
System.out.println("my interceptor 攔截。。。。。");
//return ai.invoke(); // 放行
return Action.LOGIN; //"login"
}
}
- 2.宣告一個Interceptor 在struts-default.xml檔案中
- eg:
<interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
</interceptor>
- 3.在action中指定使用哪些攔截器
<interceptors>
<interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
</interceptor>
</interceptors>
- 注意 : 只要顯示宣告使用了一個攔截器。那麼預設的攔截器就不在載入。
- 如果需要繼續使用預設攔截器只需要在struts.xml中設計自己的攔截器棧。
<interceptors>
<interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
</interceptor>
<interceptor name="bookInterceptor" class="cn.itcast.intercept.BookInterceptor">
<param name="includeMethods">add,update,delete</param>
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="bookInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
分析攔截器原理
原始碼執行流程:
1.在StrutsPrepareAndExecuteFilter中查詢
- 在doFilter方法內有一句話 execute.executeAction (request, response, mapping) 執行Action操作.
2.在executeAction執行過程中會訪問Dispatcher類中的serviceAction,在這個方法中會建立一個
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false);
這就是我們的Action的代理物件
3.檢視ActionInvocation,檢視其實現類 DefaultActionInvocation.
- 在其invoke方法中
if (interceptors.hasNext()) {//判斷是否有下一個攔截器.
final InterceptorMapping interceptor = interceptors.next(); //得到一個攔截器
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
//呼叫得到的攔截器的攔截方法.將本類物件傳遞到了攔截器中。
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
}
- 通過原始碼分析,發現在DefaultActionInvocation中就是通過遞迴完成所有的攔截呼叫操作.
關於interceptor與Filter區別
- 1、攔截器是基於java反射機制的,而過濾器是基於函式回撥的。
- 2、過濾器依賴於servlet容器,而攔截器不依賴於servlet容器。
- 3、攔截器只能對Action請求起作用,而過濾器則可以對幾乎所有請求起作用。
- 4、攔截器可以訪問Action上下文、值棧裡的物件,而過濾器不能。
- 5、在Action的生命週期中,攔截器可以多次呼叫,而過濾器只能在容器初始化時被呼叫一次。
- interceptor 可以在其他環境中使用,而Filter只能在Web環境中使用。
攔截器案例
- 1.login.jsp –> LoginAction –> book.jsp
登入成功,將使用者儲存到session。
login.jsp
<body>
<s:fielderror/>
<s:actionerror/>
<form action="${pageContext.request.contextPath }/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="登入">
</form>
</body>
- LoginAction.java
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public User getModel(){
return user;
}
@Override
public String execute() throws Exception {
System.out.println("name: "+user.getUsername());
System.out.println("password: "+user.getPassword());
if("miaolu".equals(user.getUsername()) && "123".equals(user.getPassword())){
ServletActionContext.getRequest().getSession().setAttribute("user", user);
return SUCCESS;
}
else{
this.addActionError("使用者名稱或密碼錯誤");
return INPUT;
}
}
}
- book.jsp
<body>
<a href="${pageContext.request.contextPath}/book_add">book add</a><br>
<a href="${pageContext.request.contextPath}/book_update">book update</a><br>
<a href="${pageContext.request.contextPath}/book_delete">book delete</a><br>
<a href="${pageContext.request.contextPath}/book_search">book search</a><br>
</body>
- 2.在book.jsp中提供crud連結。
- 每一個連線訪問一個BookAction中一個方法。
要求:對於BookAction中的add,update,delete方法要求使用者必須登入後才可以訪問。search無要求。
BookAction.java
public class BookAction extends ActionSupport {
public String add() throws Exception{
System.out.println("book action add");
return null;
}
public String update() throws Exception{
System.out.println("book action update");
return null;
}
public String delete() throws Exception {
System.out.println("book action delete");
return null;
}
public String search() throws Exception {
System.out.println("book action search");
return null;
}
}
怎樣解決只控制action中某些方法的攔截?
- 1.建立類不在實現Interceptor介面,而是繼承其下的一個類.
MethodFilterInterceptor
- 不用在重寫intercept方法,而是重寫 doIntercept方法。
- 1.建立類不在實現Interceptor介面,而是繼承其下的一個類.
BookInterceptor.java
public class BookInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
if(user == null){
BookAction action = (BookAction) invocation.getAction();
action.addActionError("許可權不足,請先登入");
return Action.LOGIN;
}
return invocation.invoke();
}
}
- 2.在struts.xml檔案中宣告
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="bookInterceptor" class="test.intercept.BookInterceptor">
<param name="includeMethods">add,update,delete</param>
<param name="excludeMethods">search</param>
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="bookInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name = "login">/login.jsp</result>
</global-results>
<action name ="login" class="test.action.LoginAction">
<result name="input">/login.jsp</result>
<result>/book.jsp</result>
</action>
<action name="book_*" class="test.action.BookAction" method="{1}">
<interceptor-ref name="myStack"/>
</action>
</package>
Struts2中檔案上傳與下載
檔案上傳
瀏覽器端:
- 1.method=post
- 2.
<input type="file" name="xx">
- 3.encType=”multipart/form-data”;
伺服器端:
- commons-fileupload元件
- 1.DiskFileItemFactory
- 2.ServletFileUpload
- 3.FileItem
- commons-fileupload元件
struts2中檔案上傳
- 預設情況下struts2框架使用的就是
commons-fileupload元件
. - struts2它使用了一個interceptor幫助我們完成檔案上傳操作。
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
- 預設情況下struts2框架使用的就是
在action中怎樣處理檔案上傳?
- 頁面上元件:
<input type="file" name="upload">
- 頁面上元件:
在action中要有三個屬性:
- private File upload; 如果是上傳單個檔案, upload屬性的型別就是 java.io.File, 它代表被上傳的檔案,要與jsp 頁面上的 file 標籤的名字相同。
- private String uploadContentType;表示上傳檔案的型別,格式:file元件的名稱+
ContentType
- private String uploadFileName; 表示上傳檔案的檔名,格式:file元件的名稱+
FileName
在execute方法中使用commons-io包下的FileUtils完成檔案複製.
FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
@Override
public String execute() throws Exception {
System.out.println("上傳檔案的型別:" + uploadContentType);
System.out.println("上傳檔案的名稱:" + uploadFileName);
// 完成檔案上傳.
FileUtils.copyFile(upload, new File("d:/upload", uploadFileName);
return null;
}
- struts.xml 中的配置
<action name="upload" class="cn.itcast.action.UploadAction">
</action>
關於struts2中檔案上傳細節
- 1.關於控制檔案上傳大小
- 在default.properties檔案中定義了檔案上傳大小
struts.multipart.maxSize=2097152
上傳檔案預設的總大小 2MB
在struts.xml`中通過修改如下引數,進行修改
<constant name="struts.multipart.maxSize" value="20971520"></constant>
2.在struts2中預設使用的是commons-fileupload進行檔案上傳。
- struts.multipart.parser=cos : 上傳元件(備用)
- struts.multipart.parser=pell : 上傳元件(備用)
- struts.multipart.parser=jakarta : 預設上傳方式
- 如果使用pell,cos進行檔案上傳,必須匯入其jar包.
3.如果出現問題,需要配置input檢視,在頁面上可以通過
<s:actionerror>
展示錯誤資訊.在頁面上展示的英文資訊的國際化操作
- struts-messages.properties 檔案裡預定義 上傳錯誤資訊,通過覆蓋對應key 顯示中文資訊
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
- 上述資訊在自己新建的properties檔案修改為
struts.messages.error.uploading=上傳錯誤: {0}
struts.messages.error.file.too.large=上傳檔案太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=上傳檔案的型別不允許: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=上傳檔案的字尾名不允許: {0} "{1}" "{2}" {3}
- {0}:
<input type=“file” name=“uploadImage”>
中name屬性的值 - {1}:上傳檔案的真實名稱
- {2}:上傳檔案儲存到臨時目錄的名稱
- {3}:上傳檔案的型別(對struts.messages.error.file.too.large是上傳檔案的大小)
- 4.關於多檔案上傳時的每個上傳檔案大小控制以及上傳檔案型別控制
- 1.多檔案上傳
- 伺服器端 : 只需要將action屬性宣告成List集合或陣列就可以。
- 1.多檔案上傳
public class UploadAction extends ActionSupport {
// 在action類中需要宣告三個屬性
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
@Override
public String execute() throws Exception {
for (int i = 0; i < upload.size(); i++) {
System.out.println("上傳檔案的型別:" + uploadContentType.get(i));
System.out.println("上傳檔案的名稱:" + uploadFileName.get(i));
// 完成檔案上傳.
FileUtils.copyFile(upload.get(i), new File("d:/upload", uploadFileName.get(i)));
}
return null;
}
}
- 2.怎樣控制每一個上傳檔案的大小以及上傳檔案的型別?
- 在fileupload攔截器中,通過其屬性進行控制.
- maximumSize—每一個上傳檔案大小
- allowedTypes–允許上傳檔案的mimeType型別.
- allowedExtensions–允許上傳檔案的字尾名.
- 在自定義攔截器配置中加入下程式碼:
- 在fileupload攔截器中,通過其屬性進行控制.
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>
- eg:
<action name="upload" class="cn.itcast.action.UploadAction">
<result name="input">/upload.jsp</result>
<interceptor-ref name="defaultStack">
<param name="maximumSize">2097152</param>
<param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>
</action>
檔案下載
- 檔案下載方式:
- 1.超連線
- 2.伺服器編碼,通過流向客戶端寫回。
過程:
- 1.通過response設定 response.setContentType(String mimetype);
- 2.通過response設定 response.setHeader(“Content-disposition;filename=xxx”);
- 3.通過response獲取流,將要下載的資訊寫出。
struts2中檔案下載
- 通過
<result type="stream">
完成。 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
在StreamResult類中有三個屬性:
protected String contentType = "text/plain";
: 用於設定下載檔案的mimeType型別
protected String contentDisposition = "inline";
: 用於設定進行下載操作以及下載檔案的名稱protected InputStream inputStream;
: 用於讀取要下載的檔案。
- 通過
eg:
<action name="download" class="cn.itcast.action.DownloadAction">
<result type="stream">
<param name="contentType">${contentType}</param> <!-- 呼叫當前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 呼叫當前action中的getInputStream()方法 -->
</result>
</action>
- 在action類中定義一個方法
public InputStream getInputStream() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}
- 問題1:
<a href="${pageContext.request.contextPath}/download?filename=捕獲.png">捕獲.png</a>
下載報錯
- 原因:超連線是get請求,並且下載的檔案是中文名稱,亂碼。
public InputStream getInputStream() throws FileNotFoundException,
UnsupportedEncodingException {
filename = new String(filename.getBytes("iso8859-1"), "utf-8"); // 解決中文名稱亂碼.
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}
問題2:下載捕獲檔案時,檔名稱是a.txt
- 下載檔案字尾名應該是png,而我們在配置檔案中規定卻是txt?
struts.xml 修訂:
<result type="stream">
<param name="contentType">${contentType}</param> <!-- 呼叫當前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 呼叫當前action中的getInputStream()方法 -->
</result>
// 設定下載檔案mimeType型別
public String getContentType() {
String mimeType = ServletActionContext.getServletContext().getMimeType(
filename);
return mimeType;
}
// 獲取下載檔名稱
public String getDownloadFileName() throws UnsupportedEncodingException {
return DownloadUtils.getDownloadFileName(ServletActionContext
.getRequest().getHeader("user-agent"), filename);
}
- 在struts2中進行下載時,如果使用
<result type="stream">
它有缺陷
- 例如:下載點選後,取消下載,伺服器端會產生異常。
- 在開發中,解決方案:可以下載一個struts2下載操作的外掛,它解決了stream問題。
OGNL與ValueStack
OGNL表示式使用
- OGNL是Object Graphic Navigation Language(物件圖導航語言)的縮寫,它是一個開源專案。 Struts2框架使用OGNL作為預設的表示式語言。
- xwork 提供 OGNL表示式
- ognl-3.0.5.jar
- OGNL 是一種比EL 強大很多倍的語言
OGNL 提供五大類功能
- 1、支援物件方法呼叫,如xxx.doSomeSpecial();
- 2、支援類靜態的方法呼叫和值訪問
- 3、訪問OGNL上下文(OGNL context)和ActionContext; (重點 操作ValueStack值棧 )
- 4、支援賦值操作和表示式串聯
5、操作集合物件。
使用OGNL訪問 物件方法 和 靜態方法
- OGNL 在jsp 結合 struts2 標籤庫 使用 ,
<s:property value="ognl表示式" />
執行 ognl表示式 - 呼叫 例項方法 :
物件.方法()
—-><s:property value="'hello,world'.length()"/>
- 呼叫 靜態方法 :
@[類全名(包括包路徑)]@[方法名]
—><s:property value="@[email protected]('您好,%s','小明')"/>
- 使用 靜態方法呼叫 必須 設定
struts.ognl.allowStaticMethodAccess=true
- OGNL 在jsp 結合 struts2 標籤庫 使用 ,
eg : 物件調方法
public static void main(String[] args) throws OgnlException {
// ognl可以通過物件呼叫方法.
// System.out.println("aaa".length());
// 使用ognl來完成上面操作.
// 1.建立一個ognl上下文。
OgnlContext context = new OgnlContext();
Object obj1 = Ognl.getValue("'aaa'.length()", context.getRoot());
System.out.println(obj1);
}
- 靜態方法
public static void main(String[] args) throws OgnlException {
// ognl可以通過物件呼叫方法.
System.out.println(Math.max(10, 20));
System.out.println(Math.PI);
// 使用ognl來完成上面操作.
// 1.建立一個ognl上下文。
OgnlContext context = new OgnlContext();
Object obj1 = Ognl.getValue("@[email protected](10,20)", context.getRoot());
System.out.println(obj1);
Object obj2= Ognl.getValue("@[email protected]", context.getRoot());
System.out.println(obj2);
}
- 訪問OGNL上下文(OGNL context)和ActionContext
public static void main(String[] args) throws OgnlException {
// 建立一個ognl上下文
OgnlContext context = new OgnlContext(); // 本質上就是一個Map集合.
Person p = new Person();
p.setName("張三");
Dog dog = new Dog();
dog.setName("lucy");
p.setDog(dog); //張三有條狗叫lucy
context.setRoot(p);
Dog dog1 = new Dog();
dog1.setName("豆豆");
Person pp=new Person();
pp.setName("james");
dog1.setP(pp);
context.put("dog", dog1);
context.put("name", "tom");
// 使用 ognl來獲取根中資料 獲取根中資料,不需要加#
Object name1 = Ognl.getValue("name", context, context.getRoot());
System.out.println(name1);
// 使用ognl來獲取非根中的資料 獲取非根中資料,需要使用#
Object name2 = Ognl.getValue("#name", context, context.getRoot());
System.out.println(name2);
//獲取出張三的的狗的名稱
//張三是root中故直接指向根張三
Object name3 = Ognl.getValue("dog.name", context, context.getRoot());
System.out.println(name3);
//豆豆的主人名稱
Object name4=Ognl.getValue("#dog.p.name", context, context.getRoot());
System.out.println(name4);
}
}
//執行結果:
//張三
//tom
//lucy
//james
* OGNL上下文(OGNL context)物件 值棧 ValueStack *
什麼是值棧 ValueStack ?
- ValueStack 是 struts2 提供一個介面,實現類 OgnlValueStack —- 值棧物件 (OGNL是從值棧中獲取資料的 )
- 每個Action例項都有一個ValueStack物件 (一個請求 對應 一個ValueStack物件 )
- 在其中儲存當前Action 物件和其他相關物件 (值棧中 是有Action 引用的 )
- Struts 框架把 ValueStack 物件儲存在名為 “struts.valueStack” 的請求屬性中,request中 (值棧物件 是 request一個屬性)
值棧的內部結構 ?
- 值棧由兩部分組成
- ObjectStack: Struts 把動作和相關物件壓入 ObjectStack 中–List
- ContextMap: Struts 把各種各樣的對映關係(一些 Map 型別的物件) 壓入 ContextMap 中,Struts 會把下面這些對映壓入 ContextMap 中
- parameters: 該 Map 中包含當前請求的請求引數
- request: 該 Map 中包含當前 request 物件中的所有屬性
- session: 該 Map 中包含當前 session 物件中的所有屬性
- application:該 Map 中包含當前 application 物件中的所有屬性
- attr: 該 Map 按如下順序來檢索某個屬性: request, session, application
- 值棧由兩部分組成
ValueStack中 存在
root屬性
(CompoundRoot) 、context 屬性
(OgnlContext )- CompoundRoot 就是ArrayList
- OgnlContext 就是 Map
context 對應Map 引入 root物件
- context中還存在 request、 session、application、 attr、 parameters 物件引用
- OGNL表示式,訪問root中資料時 不需要 #, 訪問 request、 session、application、 attr、 parameters 物件資料 必須寫 #
- 操作值棧 預設指 操作 root 元素
值棧物件的建立 ,ValueStack 和 ActionContext 是什麼關係 ?
- 值棧物件 是請求時 建立的
- doFilter中 prepare.createActionContext(request, response);
- 建立ActionContext 物件過程中,建立 值棧物件ValueStack
- ActionContext物件 對 ValueStack物件 有引用的 (在程式中 通過 ActionContext 獲得 值棧物件 )
- Dispatcher類 serviceAction 方法中 將值棧物件儲存到 request範圍
- request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
獲得值棧物件的兩種方法
- ValueStack valueStack = (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
- ValueStack valueStack2 = ActionContext.getContext().getValueStack();
向值棧儲存資料 (主要針對 root)的兩種方式
- 將資料儲存root的索引0位置,放置到第一個元素
- ArrayList add(0,element);
- valueStack.push(“itcast”);
- 在值棧建立引數map, 將資料儲存到map中
- valueStack.set(“company”, “傳智播客”);
- 在jsp中 通過
<s:debug />
檢視值棧的內容
- 將資料儲存root的索引0位置,放置到第一個元素
在JSP中獲取值棧的資料
- 訪問root中資料 不需要#
- 訪問 其它物件資料 加 #
通過下標獲取root中物件
<s:property value="[0].top"/>
//取值棧頂物件
直接在root中查詢物件屬性 (自上而下自動查詢)
valueStack:<s:property value="username"/>
在OgnlContext中獲取資料
request:<s:property value="#request.username"/>
session:<s:property value="#session.username"/>
application:<s:property value="#application.username"/>
attr:<s:property value="#attr.username"/>
parameters:<s:property value="#parameters.cid[0]"/>
相關推薦
Struts2學習第三天——攔截器與檔案傳輸
文件版本 開發工具 測試平臺 工程名字 日期 作者 備註 V1.0 2016.06.15 lutianfei none
Struts2學習第4天--攔截器
asi execute 部分 struts 代碼 inter nod efault alt 第1章 Struts2_day04筆記 1.1 上次課內容回顧 l OGNL表達式 n OGNL的概述 u OGNL:對象圖導航語言,是一門功能強大的
Jersey學習記錄(三)--過濾攔截器與異常處理
本文記錄全域性異常返回,過濾器,攔截器. 一.全域性異常處理器 webservice定義全域性返回錯誤碼是很重要的一個功能,jersey正好就能實現這一功能. 整個流程就是當jersey託管的類發現了異常,丟擲給jersey的異常處理器,該異常處理器直
Struts2學習第三課 Struts2詳解
request end apach -1 sso struts2 input div available 接著上次的課程 這次我們看struts.xml 修改如下:這裏是加上命名空間,默認的是不加,我們手動加上時就要在訪問時加上命名空間。 <?xml version
Struts2學習第三課 訪問Web資源
request logs w3c result ring servle ext 獲取request out 1.什麽是WEB資源? HttpServletRequest,HttpSession,ServletContext等原生的Servlet API。 2.為什麽訪問WE
Python學習第三天
pythonSet集合#是一個無序且不重復的元素集合,只需關註valueclass set(object): """ set() -> new empty set object set(iterable) -> new set object Build an uno
Android五天樂(第三天)ListFragment與ViewPager
viewgroup cat () wid group 得到 ica bottom csdn 1ListFragment 今天首先學習了一種很經常使用的展示場景:列表展示。 昨天學習了使用Fragmet來取代activity進行設計。今天在托管單
struts2學習(6)自定義攔截器-登錄驗證攔截器
back tps class res urn fff .com space war 需求:對登錄進行驗證,用戶名cy 密碼123456才能登錄進去; 登錄進去後,將用戶存在session中; 其他鏈接要來訪問(除了登錄鏈接),首先驗證
(三)Python 學習第三天--GUI桌面項目
gui spl 學習 ext pytho 文字 pen 內容 election (代碼參考了別人的代碼,只做學習用途!!!最近因為寫論文,好久沒有記錄,好內疚。。。今天學習了一個小案例,做一下) 主要使用模塊:tkinter 代碼如下: from tkinter impor
cisco VPN 學習第三天筆記
vpn 筆記IPSec VPN 網絡穿越和高可用性第一部分 IPSec VPN 網絡穿越問題1.1 IPSec 流量放行問題(中間設備)放行加密點之間的ISAKMP和ESP 流量site1:202.100.1.1site2:202.100.2.1access-list out extended permit
TP手冊學習第三天
進入 技術 cati 清除 app 控制 log true 學習 命令行先在cmd進入項目目錄,再執行命令 生成index模塊的Blog控制器類庫文件:php think make:controller index/Blog 如果僅僅生成空的控制器則可以使用:php th
Mysql學習第三天
sorting 數據類型 客戶 使用 update 控制 bsp insert 插入 1、對表中數據的增刪改(DML) create table t_user( id int primary key auto_increment, name v
python 學習第三天(簡單購物系統)2018.2.20
welcome class bic 繼續 please python else pre lease #_author_:"Bushii" #data:2018/2/20 print("*******************************") print("We
記錄學習第三天---系統基礎
學習第三天linux的基本原則:1.由目的單一的小程序組成:組合小程序完成復雜任務2.一切皆文件:3.盡量避免捕獲用戶接口:4.配置文件保存為純文本格式: CLI:命令提示符:prompt#:root$:普通用戶命令格式:命令本身+選項(短-長--)+參數 使用憑證:虛擬終端(terminal):若有圖形界面
c#學習第三天
想要 賦值 adk 數字 ati 字符串 get 整體 語法 1.常量:聲明常量的方法:const 常量(註意要大寫)//賦值號左邊必須是變量,而常量不能被重新賦值/*不想被改變得的量一般設為常量*/2.枚舉1).語法:[pubic] enum 枚舉名{ 值1, 值2,
python學習第三天:數據類型,交互,格式化輸出,基本運算符
描述 浮點型 針對 none 多個 其中 輸入 是我 基本 數據類型 1.什麽是數據類型? 變量值才是我們存的數據,所以數據類型指的是變量值的種類 2.為何數據要分類? 變量值是用來保存現實世界中的狀態的,那麽針對不同的狀態,就應該用不同
python學習--第三天 粗略介紹人臉識別
窗體 比對 tro all style 介紹 sca ase 機會 首先安裝opencv 在安裝opencv過程中遇到一些錯誤(百度解決) 直接貼代碼吧,講師略講了一下,體會不深,以後有機會深入學習,再詳細介紹解釋吧 人臉識別訓練集應該可以網上下載吧,都是開源的 impo
mysql學習第三天練習(日期和時間函數)
日期和時間 標準 學習第三天 入職 extract 時間 日期 如果 sys -- 日期和時間函數 -- 獲取當前日期 select curdate(),current_date() from dual -- 返回服務器當前的日期和時間 select NOW(),SYSD
mysql學習第三天練習(流程控制函數)
dep 個數 大於 div net sel word 127.0.0.1 流程 -- 流程控制函數 -- 1、查詢員工部門號,並賦予部門名 select empno,ename,deptno,case deptno
mysql學習第三天筆記
流程 ali align 笛卡爾積 日期時間 返回日期 至少 trac edi 連接連接是在多個表之間通過一定的連接條件,使表之間發生關聯,進而能從多個表之間獲取數據。在 WHERE子句中書寫連接條件。 如果在多個表中出現相同的列名,則需要使用表名作為來自該表的列名的前綴。