[关闭]
@MrXiao 2017-12-22T06:30:02.000000Z 字数 2626 阅读 789

Struts2(6)——Struts标签、国际化、文件上传下载

Struts2


1、struts标签

Struts2标签库常用标签

2、国际化

Struts2在ActionSupport中为我们提供了getText()来获取国际化value.

2.1 定义资源包

  1. 全局资源

    创建资源文件

    格式:基本名称_语言_国家.properties 
    例如:resources_zh_CN.properties
          resources_en_US.properties
    

    指定文在所在位置及文件名

    1. <!-- 国际化资源文件路径及名称 -->
    2. <constant name="struts.custom.i18n.resources"
    3. value="com/topvision/s2sm/resources" />
  2. 包范围

    在包下创建资源文件,不用在xml中指定文件路径

    文件名:package_zh_CN.properties
            package_zh_CN.properties
    
    1. Action范围

    在Action同级目录创建资源文件,不用在xml中指定文件路径。

    文件名:ActionName_zh_CN.properties
    ActionName是对应action的名字
    

2.2 获取资源包内容

获取国际化

自由选择消息资源包

  1. <!--自己随意指定消息资源包-->
  2. <s:i18n name="com/topvision/package">
  3. <s:text name="key"></s:text>
  4. </s:i18n>

3、文件上传下载

3.1 上传

Struts2上传对表单有要求:

  1. post提交
  2. 表单的enctype必须为multipart/form-data
  3. 表单提供如下类型的input

    1. <input type="file" name="name"/>

Struts2中如何实现文件上传的:

  1. 借助一个fileUpload拦截器完成的;
  2. 最底层的还是借助的commons-fileupload这个组件;

单文件上传

  1. 准备表单

    1. <form action="aaa/upload.tv" method="post" enctype="multipart/form-data">
    2. <input type="file" name="upload">
    3. <button type="submit">上传</button>
    4. </form>
  2. action

    1. private File upload;//input标签的name属性
    2. private String uploadFileName; //name属性+FileName
    3. private String uploadContentType;//name属性+ContentType
    4. public String upload() throws IOException {
    5. ServletContext servletContext = ServletActionContext.getServletContext();
    6. String realPath = servletContext.getRealPath("/WEB-INF/file");
    7. File file = new File(realPath);
    8. if (!file.exists()) {
    9. file.mkdirs();
    10. }
    11. /*InputStream is = new FileInputStream(upload);
    12. OutputStream os = new FileOutputStream(new File(file, uploadFileName));
    13. int len = -1;
    14. byte b[] = new byte[1024];
    15. while ((len = is.read(b)) != -1) {
    16. os.write(b, 0, len);
    17. }*/
    18. FileUtils.moveFile(upload, new File(file, uploadFileName));
    19. is.close();
    20. os.close();
    21. return SUCCESS;
    22. }
    1. 配置xml
    1. <action name="upload" class="com.topvision.s2sm.login.action.LoginAction" method="upload2">
    2. <result>/WEB-INF/jsp/login/login.jsp</result>
    3. </action>

多文件上传

第一种,比较煞笔的,多写几个上传框
第二种:使用 uploadify 插件

3.2 下载

action配置

  1. private InputStream inputStream;
  2. public String download() throws FileNotFoundException {
  3. ServletContext servletContext = ServletActionContext.getServletContext();
  4. String realPath = servletContext.getRealPath("/WEB-INF/web.xml");
  5. inputStream = new FileInputStream(realPath);
  6. return SUCCESS;
  7. }
  8. public InputStream getInputStream() {
  9. return inputStream;
  10. }
  11. public void setInputStream(InputStream inputStream) {
  12. this.inputStream = inputStream;
  13. }

xml配置

  1. <result name="success" type="stream">
  2. <!-- 设置输入流来源 -->
  3. <param name="inputstream">inputstream</param>
  4. <!-- 响应头 -->
  5. <param name="contentDisposition">attachment;filename=web.xml</param>
  6. <!-- 传的什么类型数据,在tomcat的web.xml中有定义 -->
  7. <param name="contentType">application/xml</param>
  8. <!-- 下载缓存 ,默认1024-->
  9. <param name="bufferSize">10240</param>
  10. <!-- 下载文件大小 -->
  11. <param name="contentLength">10240</param>
  12. </result>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注