國際化
1、 國際化原理 ? 什么是國際化 ?
同一款軟件 可以為不同用戶,提供不同語言界面 ---- 國際化軟件
需要一個語言資源包(很多properties文件,每個properties文件 針對一個國家或者語言 ,
通過java程序根據來訪者國家語言,自動讀取不同properties文件 )
2、 資源包編寫
properties文件命名 : 基本名稱_語言(小寫)_國家(大寫).properties
例如 :
messages_zh_CN.properties 中國中文
messages_en_US.properties 美國英文
3、 ResourceBundle 根據不同Locale(地域信息),讀取不同國家 properties文件
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);
struts2中國際化:
struts2中對國際化進行了封裝,我們只需要根據其提供的API進行訪問就可以。
問題1:在struts2中國際化時properties文件怎樣定義?(怎樣定義properties)
1.全局
需要通過一個常量來聲明.
struts.custom.i18n.resources=testmessages,testmessages2
對于properties配置文件可以放置在任意位置
<constant name="struts.custom.i18n.resources" value="message"> 代表message.properties在src下
<constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message"> 代表message.properties在cn.itcast.i18n.resource包下.
2.局部
1.針對于action類
位置:與action類在同一個包下.
名稱:ActionClassName.properties.
這個配置文件只對當前action有效。
2.針對于package下所有action
位置:在指定的包下
名稱:package.properties
3.jsp頁面臨時使用某一個properties文件.
<s:i18n name="cn.itcast.action.package"></s:i18n>
問題2:在struts2中國際化操作可以在哪些位置使用?(在哪此位置上使用)
1.action類中使用
2.配置文件中使用<validation.xml>
3.在jsp頁面上使用
問題3:怎樣在struts2中操作國際化?(怎樣使用)
1.在action類中使用
前提:action類要繼承ActionSupport類。
getText(String name)就可以獲取配置文件中對應名稱的值。
2.在validation.xml文件中使用
<message key="名稱"/>
3.在jsp頁面上使用
<s:text name="名稱"> 如果沒有使用<s:i18n name="">來指定,會從全局配置文件中獲取。
如果要從某一個配置文件中獲取,通過name屬性來指定, 包名.配置文件名稱 .
攔截器(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方法是真正攔截的方法。
在intercept方法中如果要向下繼續執行,通過其參數ActionInvocation調用它的invoke()方法就可以。
2.聲明一個Interceptor
在struts-default.xml文件中
<interceptors>
<interceptor name="" class=""/>
</interceptors>
注意:我們要自己聲明一個interceptor可以在struts.xml文件中聲明。
3.在action中指定使用哪些攔截器.
<interceptor-ref name="my"/>
注意:只要顯示聲明使用了一個攔截器。那么默認的攔截器就不在加載。
分析攔截器原理
源代碼執行流程:
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的生命周期中,攔截器可以多次調用,而過濾器只能在容器初始化時被調用一次。
案例及方法的選擇性攔截
權限控制:
1.login.jsp------>LoginAction------------->book.jsp
登錄成功,將用戶存儲到session。
2.在book.jsp中提供crud鏈接。
每一個連接訪問一個BookAction中一個方法。
要求:對于BookAction中的add,update,delete方法要求用戶必須登錄后才可以訪問。search無要求。
怎樣解決只控制action中某些方法的攔截?
1.創建類不在實現Interceptor接口,而是繼承其下的一個子類.MethodFilterInterceptor,
不用在重寫intercept方法,而是重寫 doIntercept方法。
2.在struts.xml文件中聲明
<interceptors>
<intercept name="" class="">
<param name="includeMethods">add,update,delete</param>
<param name="excludeMethods">search</param>
</intercept>
</interceptors>
文件上傳
瀏覽器端:
1.method=post
2.<input type="file" name="xx">
3.encType="multipart/form-data";
服務器端:
commons-fileupload組件
1.DiskFileItemFactory
2.ServletFileUpload
3.FileItem
struts2中文件上傳:
默認情況下struts2框架使用的就是commons-fileupload組件.
struts2它使用了一個interceptor幫助我們完成文件上傳操作。
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
在action中怎樣處理文件上傳?
頁面上組件:<input type="file" name="upload">
在action中要有三個屬性:
private File upload;
private String uploadContentType;
private String uploadFileName;
在execute方法中使用commons-io包下的FileUtils完成文件復制.
FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
關于struts2中文件上傳細節:
1.關于控制文件上傳大小
在default.properties文件中定義了文件上傳大小
struts.multipart.maxSize=2097152 上傳文件默認的總大小 2m
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}
修改為
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集合或數組就可以。
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
2.怎樣控制每一個上傳文件的大小以及上傳文件的類型?
在fileupload攔截器中,通過其屬性進行控制.
maximumSize---每一個上傳文件大小
allowedTypes--允許上傳文件的mimeType類型.
allowedExtensions--允許上傳文件的后綴名.
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>
文件下載
文件下載方式:
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; //用于讀取要下載的文件。
在action類中定義一個方法
public InputStream getInputStream() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}
<result type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;filename=a.txt</param>
<param name="inputStream">${inputStream}</param> 會調用當前action中的getInputStream方法。
</result>
問題1:<a href="${pageContext.request.contextPath}/download?filename=捕獲.png">捕獲.png</a>下載報錯
原因:超連接是get請求,并且下載的文件是中文名稱,亂碼。
問題2:下載捕獲文件時,文件名稱就是a.txt ,下載文件后綴名是png,而我們在配置文件中規定就是txt?
<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>
在struts2中進行下載時,如果使用<result type="stream">它有缺陷,例如:下載點擊后,取消下載,服務器端會產生異常。
在開發中,解決方案:可以下載一個struts2下載操作的插件,它解決了stream問題。
ognl
問題:ognl是什么,它有什么用?
OGNL是Object-Graph Navigation Language的縮寫,它是一種功能強大的表達式語言.
比el表達式功能強大。
struts2將ognl表達式語言,集成當sturts2框架中,做為它的默認表達式語言。
OGNL 提供五大類功能
1、支持對象方法調用,如xxx.doSomeSpecial();
2、支持類靜態的方法調用和值訪問
3、訪問OGNL上下文(OGNL context)和ActionContext; (重點 操作ValueStack值棧 )
4、支持賦值操作和表達式串聯
5、操作集合對象。