國際化
1、 國際化原理 固灵? 什么是國際化 ?
同一款軟件 可以為不同用戶,提供不同語言界面 ---- 國際化軟件
需要一個(gè)語言資源包(很多properties文件,每個(gè)properties文件 針對一個(gè)國家或者語言 戴甩,
通過java程序根據(jù)來訪者國家語言尊沸,自動(dòng)讀取不同properties文件 )
2、 資源包編寫
properties文件命名 : 基本名稱_語言(小寫)_國家(大寫).properties
例如 :
messages_zh_CN.properties 中國中文
messages_en_US.properties 美國英文
3帽衙、 ResourceBundle 根據(jù)不同Locale(地域信息),讀取不同國家 properties文件
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);
struts2中國際化:
struts2中對國際化進(jìn)行了封裝贞绵,我們只需要根據(jù)其提供的API進(jìn)行訪問就可以佛寿。
問題1:在struts2中國際化時(shí)properties文件怎樣定義?(怎樣定義properties)
1.全局
需要通過一個(gè)常量來聲明.
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類在同一個(gè)包下.
名稱:ActionClassName.properties.
這個(gè)配置文件只對當(dāng)前action有效但壮。
2.針對于package下所有action
位置:在指定的包下
名稱:package.properties
3.jsp頁面臨時(shí)使用某一個(gè)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)就可以獲取配置文件中對應(yīng)名稱的值。
2.在validation.xml文件中使用
<message key="名稱"/>
3.在jsp頁面上使用
<s:text name="名稱"> 如果沒有使用<s:i18n name="">來指定蜡饵,會從全局配置文件中獲取弹渔。
如果要從某一個(gè)配置文件中獲取,通過name屬性來指定溯祸, 包名.配置文件名稱 .
攔截器(interceptor)
介紹攔截器:
struts2攔截器使用的是AOP思想肢专。
AOP的底層實(shí)現(xiàn)就是動(dòng)態(tài)代理舞肆。
攔截器 采用 責(zé)任鏈 模式
* 在責(zé)任鏈模式里,很多對象由每一個(gè)對象對其下家的引用而連接起來形成一條鏈。
* 責(zé)任鏈每一個(gè)節(jié)點(diǎn)博杖,都可以繼續(xù)調(diào)用下一個(gè)節(jié)點(diǎn)椿胯,也可以阻止流程繼續(xù)執(zhí)行
struts2中在struts-default.xml文件中聲明了所有的攔截器。
而struts2框架默認(rèn)使用的是defaultStack這個(gè)攔截器棧剃根。
在這個(gè)攔截器棧中使用了18個(gè)攔截器哩盲。簡單說,struts2框架
在默認(rèn)情況下狈醉,加載了18個(gè)攔截器廉油。
struts2中怎樣使用攔截器
問題:使用攔截器可以做什么?
可以通過使用攔截器進(jìn)行控制action的訪問苗傅。例如抒线,權(quán)限操作。
怎樣使用攔截器?
1.創(chuàng)建一個(gè)Interceptor 可以自定義一個(gè)類實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor
在這個(gè)接口中有三個(gè)方法 init destory intercept渣慕, intercept方法是真正攔截的方法嘶炭。
在intercept方法中如果要向下繼續(xù)執(zhí)行,通過其參數(shù)ActionInvocation調(diào)用它的invoke()方法就可以逊桦。
2.聲明一個(gè)Interceptor
在struts-default.xml文件中
<interceptors>
<interceptor name="" class=""/>
</interceptors>
注意:我們要自己聲明一個(gè)interceptor可以在struts.xml文件中聲明眨猎。
3.在action中指定使用哪些攔截器.
<interceptor-ref name="my"/>
注意:只要顯示聲明使用了一個(gè)攔截器。那么默認(rèn)的攔截器就不在加載卫袒。
分析攔截器原理
源代碼執(zhí)行流程:
1.在StrutsPrepareAndExecuteFilter中查找
在doFilter方法內(nèi)有一句話 execute.executeAction (request, response, mapping) 執(zhí)行Action操作.
2.在executeAction執(zhí)行過程中會訪問Dispatcher類中的serviceAction,在這個(gè)方法中會創(chuàng)建一個(gè)
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false);
這就是我們的Action的代理對象
3.查看ActionInvocation单匣,查看其實(shí)現(xiàn)類 DefaultActionInvocation.
在其invoke方法中
if (interceptors.hasNext()) {//判斷是否有下一個(gè)攔截器.
final InterceptorMapping interceptor = interceptors.next(); //得到一個(gè)攔截器
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
//調(diào)用得到的攔截器的攔截方法.將本類對象傳遞到了攔截器中夕凝。
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
}
通過源代碼分析,發(fā)現(xiàn)在DefaultActionInvocation中就是通過遞歸完成所有的攔截調(diào)用操作.
關(guān)于interceptor與Filter區(qū)別:
1户秤、攔截器是基于java反射機(jī)制的码秉,而過濾器是基于函數(shù)回調(diào)的。
2鸡号、過濾器依賴于servlet容器转砖,而攔截器不依賴于servlet容器。
3鲸伴、攔截器只能對Action請求起作用府蔗,而過濾器則可以對幾乎所有請求起作用。
4汞窗、攔截器可以訪問Action上下文姓赤、值棧里的對象,而過濾器不能仲吏。
5不铆、在Action的生命周期中蝌焚,攔截器可以多次調(diào)用,而過濾器只能在容器初始化時(shí)被調(diào)用一次誓斥。
案例及方法的選擇性攔截
權(quán)限控制:
1.login.jsp------>LoginAction------------->book.jsp
登錄成功只洒,將用戶存儲到session。
2.在book.jsp中提供crud鏈接劳坑。
每一個(gè)連接訪問一個(gè)BookAction中一個(gè)方法毕谴。
要求:對于BookAction中的add,update,delete方法要求用戶必須登錄后才可以訪問。search無要求泡垃。
怎樣解決只控制action中某些方法的攔截析珊?
1.創(chuàng)建類不在實(shí)現(xiàn)Interceptor接口,而是繼承其下的一個(gè)子類.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";
服務(wù)器端:
commons-fileupload組件
1.DiskFileItemFactory
2.ServletFileUpload
3.FileItem
struts2中文件上傳:
默認(rèn)情況下struts2框架使用的就是commons-fileupload組件.
struts2它使用了一個(gè)interceptor幫助我們完成文件上傳操作存和。
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
在action中怎樣處理文件上傳?
頁面上組件:<input type="file" name="upload">
在action中要有三個(gè)屬性:
private File upload;
private String uploadContentType;
private String uploadFileName;
在execute方法中使用commons-io包下的FileUtils完成文件復(fù)制.
FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
關(guān)于struts2中文件上傳細(xì)節(jié):
1.關(guān)于控制文件上傳大小
在default.properties文件中定義了文件上傳大小
struts.multipart.maxSize=2097152 上傳文件默認(rèn)的總大小 2m
2.在struts2中默認(rèn)使用的是commons-fileupload進(jìn)行文件上傳奕剃。
# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta
如果使用pell,cos進(jìn)行文件上傳,必須導(dǎo)入其jar包.
3.如果出現(xiàn)問題捐腿,需要配置input視圖纵朋,在頁面上可以通過<s:actionerror>展示錯(cuò)誤信息.
問題:在頁面上展示的信息,全是英文茄袖,要想展示中文操软,國際化
struts-messages.properties 文件里預(yù)定義 上傳錯(cuò)誤信息,通過覆蓋對應(yīng)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=上傳錯(cuò)誤: {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}:上傳文件的真實(shí)名稱
{2}:上傳文件保存到臨時(shí)目錄的名稱
{3}:上傳文件的類型(對struts.messages.error.file.too.large是上傳文件的大小)
4.關(guān)于多文件上傳時(shí)的每個(gè)上傳文件大小控制以及上傳文件類型控制.
1.多文件上傳
服務(wù)器端:
只需要將action屬性聲明成List集合或數(shù)組就可以宪祥。
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
2.怎樣控制每一個(gè)上傳文件的大小以及上傳文件的類型?
在fileupload攔截器中聂薪,通過其屬性進(jìn)行控制.
maximumSize---每一個(gè)上傳文件大小
allowedTypes--允許上傳文件的mimeType類型.
allowedExtensions--允許上傳文件的后綴名.
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>
文件下載
文件下載方式:
1.超連接
2.服務(wù)器編碼,通過流向客戶端寫回蝗羊。
1.通過response設(shè)置 response.setContentType(String mimetype);
2.通過response設(shè)置 response.setHeader("Content-disposition;filename=xxx");
3.通過response獲取流藏澳,將要下載的信息寫出。
struts2中文件下載:
通過<result type="stream">完成耀找。
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
在StreamResult類中有三個(gè)屬性:
protected String contentType = "text/plain"; //用于設(shè)置下載文件的mimeType類型
protected String contentDisposition = "inline";//用于設(shè)置進(jìn)行下載操作以及下載文件的名稱
protected InputStream inputStream; //用于讀取要下載的文件翔悠。
在action類中定義一個(gè)方法
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> 會調(diào)用當(dāng)前action中的getInputStream方法。
</result>
問題1:<a href="${pageContext.request.contextPath}/download?filename=捕獲.png">捕獲.png</a>下載報(bào)錯(cuò)
原因:超連接是get請求野芒,并且下載的文件是中文名稱蓄愁,亂碼。
問題2:下載捕獲文件時(shí)狞悲,文件名稱就是a.txt ,下載文件后綴名是png,而我們在配置文件中規(guī)定就是txt?
<result type="stream">
<param name="contentType">${contentType}</param> <!-- 調(diào)用當(dāng)前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 調(diào)用當(dāng)前action中的getInputStream()方法 -->
</result>
在struts2中進(jìn)行下載時(shí)涝登,如果使用<result type="stream">它有缺陷,例如:下載點(diǎn)擊后效诅,取消下載胀滚,服務(wù)器端會產(chǎn)生異常趟济。
在開發(fā)中,解決方案:可以下載一個(gè)struts2下載操作的插件咽笼,它解決了stream問題顷编。
ognl
問題:ognl是什么,它有什么用?
OGNL是Object-Graph Navigation Language的縮寫剑刑,它是一種功能強(qiáng)大的表達(dá)式語言.
比el表達(dá)式功能強(qiáng)大媳纬。
struts2將ognl表達(dá)式語言,集成當(dāng)sturts2框架中施掏,做為它的默認(rèn)表達(dá)式語言钮惠。
OGNL 提供五大類功能
1、支持對象方法調(diào)用七芭,如xxx.doSomeSpecial()素挽;
2、支持類靜態(tài)的方法調(diào)用和值訪問
3狸驳、訪問OGNL上下文(OGNL context)和ActionContext预明; (重點(diǎn) 操作ValueStack值棧 )
4、支持賦值操作和表達(dá)式串聯(lián)
5耙箍、操作集合對象撰糠。