@MrXiao
2017-12-22T06:30:02.000000Z
字数 2626
阅读 1122
Struts2
Struts2在ActionSupport中为我们提供了getText()来获取国际化value.
全局资源
创建资源文件
格式:基本名称_语言_国家.properties
例如:resources_zh_CN.properties
resources_en_US.properties
指定文在所在位置及文件名
<!-- 国际化资源文件路径及名称 --><constant name="struts.custom.i18n.resources"value="com/topvision/s2sm/resources" />
包范围
在包下创建资源文件,不用在xml中指定文件路径
文件名:package_zh_CN.properties
package_zh_CN.properties
在Action同级目录创建资源文件,不用在xml中指定文件路径。
文件名:ActionName_zh_CN.properties
ActionName是对应action的名字
获取国际化
自由选择消息资源包
<!--自己随意指定消息资源包--><s:i18n name="com/topvision/package"><s:text name="key"></s:text></s:i18n>
Struts2上传对表单有要求:
表单提供如下类型的input
<input type="file" name="name"/>
Struts2中如何实现文件上传的:
单文件上传
准备表单
<form action="aaa/upload.tv" method="post" enctype="multipart/form-data"><input type="file" name="upload"><button type="submit">上传</button></form>
action
private File upload;//input标签的name属性private String uploadFileName; //name属性+FileNameprivate String uploadContentType;//name属性+ContentTypepublic String upload() throws IOException {ServletContext servletContext = ServletActionContext.getServletContext();String realPath = servletContext.getRealPath("/WEB-INF/file");File file = new File(realPath);if (!file.exists()) {file.mkdirs();}/*InputStream is = new FileInputStream(upload);OutputStream os = new FileOutputStream(new File(file, uploadFileName));int len = -1;byte b[] = new byte[1024];while ((len = is.read(b)) != -1) {os.write(b, 0, len);}*/FileUtils.moveFile(upload, new File(file, uploadFileName));is.close();os.close();return SUCCESS;}
<action name="upload" class="com.topvision.s2sm.login.action.LoginAction" method="upload2"><result>/WEB-INF/jsp/login/login.jsp</result></action>
多文件上传
第一种,比较煞笔的,多写几个上传框
第二种:使用 uploadify 插件
action配置
private InputStream inputStream;public String download() throws FileNotFoundException {ServletContext servletContext = ServletActionContext.getServletContext();String realPath = servletContext.getRealPath("/WEB-INF/web.xml");inputStream = new FileInputStream(realPath);return SUCCESS;}public InputStream getInputStream() {return inputStream;}public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}
xml配置
<result name="success" type="stream"><!-- 设置输入流来源 --><param name="inputstream">inputstream</param><!-- 响应头 --><param name="contentDisposition">attachment;filename=web.xml</param><!-- 传的什么类型数据,在tomcat的web.xml中有定义 --><param name="contentType">application/xml</param><!-- 下载缓存 ,默认1024--><param name="bufferSize">10240</param><!-- 下载文件大小 --><param name="contentLength">10240</param></result>