【问题标题】:struts2 s:form element trims the s:url parameter in the action attributestruts2 s:form 元素修剪 action 属性中的 s:url 参数
【发布时间】:2022-01-28 01:04:20
【问题描述】:

我正在使用<s:url> 创建一个类似removeAction.action?id=10 的URL,<s:form> 元素中的action 属性应该使用它。

这里的问题是当<s:form> 转换为<form> 元素时,我只能看到动作属性值为action="/project/removeAction.action"。 id 参数正在被修剪。我想要的结果是action="/project/removeAction.action?id=10"

<s:url var="actionUrl" action="removeAction" includeContext="false">
  <s:param name="id" value="%{id}" /> 
</s:url>

<s:form action="%{actionUrl}" method="post" enctype="multipart/form-data" >
 <div>
  <s:file name="imgUpload"/>
  <s:submit> upload </submit>
 </div>
</s:form>

最近我将 struts2 核心版本升级到 2.3.12,我遇到了这个问题。此问题在版本 2.3.4.1 之后开始

而且我不想使用hidden 属性来传递参数,因为当文件很大要上传时,此参数会丢失。

有什么解决办法吗?

【问题讨论】:

  • 也许这是你真正的问题:And I don't want to use a hidden attribute to pass the parameter as this parameter get lost when the file size is big to upload.?

标签: jsp parameters struts2


【解决方案1】:

如果文件太大,文件上传时隐藏参数会丢失是什么意思?它将被重新读取并自动填充...

  • 不要将 RemoveAction 称为实际上是上传文件的操作。 为了逻辑起见,称它为UploadAction:|

  • it is not a good idea to use Query Parameters in POST requests,它们只能用于 GET 请求,可能以 REST 方式...

  • 为防止出现max multipart size exceeded 错误,请将其放入Struts.xml

    <constant name="struts.multipart.maxSize" value="52428800" />
    
  • 要调整 fileUpload Interceptor 中单个文件的最大大小(默认为 2Mb),请将其放入 Struts.xmlStack definition

    <interceptor-ref name="fileUpload">
        <param name="maximumSize">10485760</param>
    </interceptor-ref>
    

    (在此示例中,您最多可以连续上传 5 个 10 MB 的文件)

  • 最后,对于所有符合 HTML5 的浏览器(几乎所有浏览器,除了旧版 IE 和某些移动设备),您可以在发送之前阻止上传,方法是在 onchange 事件中检查其大小,如下所示:

    <s:file name="imgUpload"/ 
            onchange="javascript:checkFileSize(this);" />
    
    <script>
        const maxFileSize = 10485760; // 10MB
    
        function checkFileSize(fileElement){
            if (fileElement.files[0].size > maxFileSize) {
                var mb = (((fileElement.files[0].size) / 1024)/1024).toFixed(2);
                alert("Max file size exceeded: " + mb + " MegaBytes");
                fileElement.value = '';
            }
        }
    </script>
    

【讨论】:

    【解决方案2】:

    也许你可以使用通配符映射

    <action name="removeAction\\*" class="..">
    
    </action>
    

    并将 id 作为它自己的 url 的一部分传递。例如:removeAction/101

    参考http://struts.apache.org/release/2.3.x/docs/wildcard-mappings.html

    【讨论】:

      【解决方案3】:

      出现该问题是因为 org.apache.struts2.components.ServletUrlRenderer.renderUrl() 方法找不到您的操作“removeAction”的操作配置,因为您的 URL (#actionUrl) 已包含“.action”后缀。

      来自struts2 s:form documentation的action参数:

      设置要提交的动作名称,不带 .action 后缀

      解决方法很简单:不要使用&lt;s:url&gt;,而是:

      <s:form action="removeAction?id=%{id}" method="post" enctype="multipart/form-data">
       <div>
        <s:file name="imgUpload"/>
        <s:submit> upload </s:submit>
       </div>
      </s:form>
      

      【讨论】:

      • 确实如此,但是查看 org.apache.struts2.components.Form 类使用的 ServletUrlRenderer 中的 renderFormUrl(),我可以看到它们处理带有参数的动作名称。我不明白为什么 %{actionUrl} 不会导致“未找到任何操作”错误,以及为什么我的代码适用于我而不适用于你。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      相关资源
      最近更新 更多