第四讲 struts中的国际化与文件上传下载
国际化:
传统的jsp版:
1. 在src下面,创建资源文件:资源文件名_语言_区域.properties
2. 在jsp页面使用国际化标签:format;
<s:setBundle baseName=” 资源文件名” />
<s:message key=”资源文件中的key” />
3. 可以根据浏览器中设置的语言,自动切换语言。
Struts版本的国际化:
动态语言切换功能:
1. 在src下面,创建资源文件:资源文件名_语言_区域.properties
2. 在struts.xml中配置资源文件。
<constant name=”struts.custom.i18n.resources” value=”资源文件名” />
3. 定义一个Action,用于完成语言切换。
3.1 在Action中定义两个属性:(语言、区域)
3.2 创建一个对象,表示本地语言环境:
Local local = new Local(语言,区域);
3.3 将local对象,存放在session中。
3.4 立即启动当前的语言环境。
//用来完成语言切换 public String execute(){ //1. 转化成一个Local的对象 Locale local = new Locale(language, country); //2. 需要将对象存放在session中 ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", local); //3. 立即启动当前的语言环境 ActionContext.getContext().setLocale(local); //返回到国际化的页面 return "show"; }
文件的上传、下载
传统jsp的上传、下载:使用smartUpload组件。
Struts中的上传、下载:
上传步骤:
1. 修改form表单的数据类型。(复合数据类型:enctype=“multipart/form-data”)
2. 在Action中定义三个属性:文件内容(File)、文件名、文件的类型(String)。
注意:如果一次需要上传多个,将三个属性定义成数组即可。
3. 需要手动完成文件流的读写操作。
public class UploadAction extends ActionSupport{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //注意,必须定义3个属性 private File upload;//内容 private String uploadFileName;//文件名 private String uploadContentType;//文件类型 public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } //定义方法用于文件的上传 public String execute(){ System.out.println(uploadContentType); System.out.println(getName()); //2种:文本文件读写、二进制文件的读写。 //文件文件:FileReader\BufferedReader FileWriter\BufferedWriter //二进制文件: FileInputStream \ FileOutputStream //需要手动完成文件的读写操作。 FileInputStream fin = null; FileOutputStream fout = null; try { //读 fin = new FileInputStream(getUpload()); //写 fout = new FileOutputStream(new File(ServletActionContext.getServletContext().getRealPath("/")+"upload/"+uploadFileName)); byte[] data = new byte[1024]; int count = 0; while((count=fin.read(data))!=-1){ //一边写 fout.write(data, 0, count); } System.out.println("文件上传成功!"); } catch (Exception e) { e.printStackTrace(); }finally{ try { fin.close(); fout.close(); } catch (Exception e2) { e2.printStackTrace(); } } return "success"; } }
下载步骤:
1. 定义一个Action,定义下载方法,返回值必须是文件流。(inputStream)
2. 定义的方法,必须以get开头。
必须定义一个空方法,为action默认执行的方法。
/** * * 下载,方法必须返回文件流 * * @return */ public InputStream getDownFile(){ try { //设置报头 ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName,"UTF-8")); return new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/")+"upload/"+fileName)); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * 必须提供execute方法 * */ public String execute(){ return "ok"; }
接着在struts.xml中对下载进行配置。
<!-- 默认调用execute --> <action name="downloadAction" class="com.zuxia.action.DownloadAction"> <result name="ok" type="stream"> <param name="inputName">downFile</param> </result> </action>
取消时,报异常处理方法。
导入struts2-sunspoter-stream-1.0.jar包。 在package中配置: <result-types> <result-type name="xw" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX" /> </result-types> 在Action中使用刚才配置的类型: <action name="downloadAction" class="com.zuxia.action.DownloadAction"> <result name="ok" type="xw"> <param name="inputName">downFile</param> </result> </action>
posted on 2012-11-04 17:40 somewhere! 阅读(245) 评论(0) 编辑 收藏 举报