struts2 文件的上传下载 表单的重复提交 自定义拦截器

文件上传中表单的准备

要想使用 HTML 表单上传一个或多个文件

  须把 HTML 表单的 enctype 属性设置为 multipart/form-data

  须把 HTML 表单的method 属性设置为 post

  需添加 <input type=“file”> 字段.

Struts 对文件上传的支持

  在 Struts 应用程序里, FileUpload 拦截器和 Jakarta Commons FileUpload 组件可以完成文件的上传.

  步骤:1. 在 Jsp 页面的文件上传表单里使用 file 标签. 如果需要一次上传多个文件, 就必须使用多个 file 标签, 但它们的名字必须是相同的

     2. 在 Action 中新添加 3 个和文件上传相关的属性. 这 3 个属性的名字必须是以下格式
      [File Name] : File -被上传的文件。例如:data
      [File Name]ContentType : String -上传文件的文件类型。例如:dataContentType
      [File Name]FileName : String -上传文件的文件名。例如:dataFileName

    如果上上传多个文件, 可以使用 List

配置 FileUpload 拦截器

FileUpload 拦截器有 3 个属性可以设置.

  maximumSize: 上传单个文件的最大长度(以字节为单位), 默认值为 2 MB

  allowedTypes: 允许上传文件的类型, 各类型之间以逗号分隔

  allowedExtensions: 允许上传文件扩展名, 各扩展名之间以逗号分隔

可以在 struts.xml 文件中覆盖这 3 个属性

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8gAAABaCAIAAADrduiAAAAgAElEQVR4nO2dW7mrMBCFYyEWsBALtYAFLNQCFmoBC1jAQiwgITkP0z1Nc5mES/dpd9f/9YECSYbcWJ0OQXkAAAAAAADAYdT/NgAAAAAAAIC/AIQ1AAAAAAAAJwBhDQAAAAAAwAlAWAMAAAAAAHACO4W1Ul5tT2qMM8btK/F/8Yk2E8PgtHbUUkr5y+Ujr2IT6oetCYdh0Fpz8svlcqIxWXuMMcaYfTkfSVtid9UBAAAAgKncR611l0tGWe4T1vtS/V8+0Wbv/eXilPLL8kfEdNd1Sqmu66LtlB3q8HK5KKWWZTnB0GZ7ZDuHYaDL1FobY7TW7WlPN5UptYIx7nJx1v6RzgYAAADsRrqPDoMzxp0ozj5RpH6izf5jzS7R971Squ/7aDtl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bUsAAAAAADQQkX/WusEnzS99Sp9f9U+Yf2J6zF9os3+54UO0UuJaTFaftUwAAAAAADYhKQKh8EZ4yL5xZC7ehhO02GfKFI/0WZfNntZnDHuxDYFAAAAAPge8qpwHJ0xbpokgUWBBJG7uvQuAN55u90jEPr+kZA83+Gn656yXVfHoQtau2F4OFbDVKT16Rw23lrX944DHubZae025czZjuMhm7muqjYLVNMKV+ST9zVkFfY0OWNceLEAAAAAAKBKrKoaRdU4ukhoPmWak2uhNrU21talVHSy1q7r7r7zZXEkakNXeqg1yTySucvilPLGPEStMY5LqeYcZksq1piMTi3ZrPUjZ/qa1biRzS2U0rbUlWB2SMuPKwAAAAAAwDzJK5LLpdiPEJKJxUzLwlo4p6T2UmlIcjmUoaHKDCEZHaYll3ljzlG27CGu2pwGMVPO4TOCJZt9g1+5lLalrgSzIygtXNcAAAAAAC3s8VhfrxW9da6wzqrMRoErK8hqzlUjN5XbmLaFI3XVWDQ81gAAAAAAmyjGWHddXlSta8YDGmf6Yo91Y4m+sG4JL3JSzXm3x7rdy76PI3VVLXqaXNchxhoAAAAAYBuVVUE4WjfcWQpgeGS6S1jT83aUcxiwwXHSbAm51cNYlJJS5MBo1ta01EljzuzuLcVYCzZr/VhThVbb4JBr2eYWSmlb6kpOjlVBAAAAAAD2UVF26/q0jjU9dFhyVwtBCPJXLotUu9au7908P0qhlT34hScT4hgAAABxSURBVCbGPE6ohj3Ms6NIa7I8Uo1Czj7wWKvgycuofko2N64KkrVZoJq25YqyyWkd63RVcgAAAAAA0MI2lyk9kxfKx7/NEacyAAAAAAD4KrbJRnJqvsiUNwTCGgAAAAAANALZWGRfqAYAAAAAAPhO/gHdfnWfLBmdZgAAAABJRU5ErkJggg==" alt="" />

Commons FileUpload 组件默认接受上传文件总的最大值为 2M, 可以通过在 struts 配置文件中配置常量的方式修改

与文件上传有关的出错消息在org.apache.struts2 下的 struts-messages.properties 文件里预定义.  可以在文件上传 Action 相对应的资源文件中重新定义错误消息

文件的下载

1). Struts2 中使用 type="stream" 的 result 进行下载即可

2). 具体使用细节参看 struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/stream-result.html

3). 可以为 stream 的 result 设定如下参数

  contentType:被下载的文件的 MIME 类型。默认值为 text/plain
  contentLength:被下载的文件的大小,以字节为单位
  contentDisposition: 可以设置下载文件名的ContentDispositon 响应头,默认值为 inline,通常设置为如下格式: attachment;filename="document.pdf".
  inputName:Action 中提供的文件的输入流。默认值为 inputStream
  bufferSize:文件下载时缓冲区的大小。默认值为 1024
  allowCaching :文件下载时是否允许使用缓存。默认值为 true
  contentCharSet:文件下载时的字符编码。

Stream 结果类型的参数可以在 Action 以属性的方式覆盖

表单的重复提交问题

1). 什么是表单的重复提交

  > 在不刷新表单页面的前提下:
          >> 多次点击提交按钮
          >> 已经提交成功, 按 "回退" 之后, 再点击 "提交按钮".
          >> 在控制器响应页面的形式为转发情况下,若已经提交成功, 然后点击 "刷新(F5)"

  > 注意:
          >> 若刷新表单页面, 再提交表单不算重复提交
          >> 若使用的是 redirect 的响应类型, 已经提交成功后, 再点击 "刷新", 不是表单的重复提交

2). 重复提交的缺点: 加重了服务器的负担;  可能导致错误操作.

3). Struts2 解决表单的重复提交问题:

I. 在 s:form 中添加 s:token 子标签. 它将在表单里插入一个隐藏字段并把标记值(隐藏域的字段的值)保存在HttpSession 对象里.

II. 使用 Token 或 TokenSession 拦截器.

  > 这两个拦截器均不在默认的拦截器栈中, 所以需要手工配置一下
     > 若使用 Token 拦截器, 则需要配置一个 token.valid 的 result
     > 若使用 TokenSession 拦截器, 则不需要配置任何其它的 result

III. Token VS TokenSession

  > 都是解决表单重复提交问题的
     > 使用 token 拦截器会转到 token.valid 这个 result
     > 使用 tokenSession 拦截器则还会响应那个目标页面, 但不会执行 tokenSession 的后续拦截器. 就像什么都没发生过一样!

IV. 可以使用 s:actionerror 标签来显示重复提交的错误消息.

该错误消息可以在国际化资源文件中覆盖. 该消息可以在 struts-messages.properties 文件中找到

自定义拦截器

1). 具体步骤

I. 定义一个拦截器的类

  > 可以实现 Interceptor 接口
     > 继承 AbstractInterceptor 抽象类

II. 在 struts.xml 文件配置.

    <interceptors>

        <interceptor name="hello" class="com.atguigu.struts2.interceptors.MyInterceptor"></interceptor>

    </interceptors>

    <action name="testToken" class="com.atguigu.struts2.token.app.TokenAction">
<interceptor-ref name="hello"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/success.jsp</result>
<result name="invalid.token">/token-error.jsp</result>
</action>

III. 注意: 在自定义的拦截器中可以选择不调用 ActionInvocation 的 invoke() 方法. 那么后续的拦截器和 Action 方法将不会被调用.Struts 会渲染自定义拦截器 intercept 方法返回值对应的 result

上一篇:项目回顾1-图片上传-form表单还是base64-前端图片压缩


下一篇:HTML5 History 模式