java 如何在 Struts2 中提交表单的 URL 中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13373760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:28:52  来源:igfitidea点击:

How to pass a parameter in URL on a form submit in Struts2

javaurlstruts2strutsparameter-passing

提问by user1819548

I am doing a project in Struts2 where I need of setting a parameter in URL like user parameter in below link.

我正在 Struts2 中做一个项目,我需要在 URL 中设置一个参数,如以下链接中的用户参数。

I want this parameter to be passed when I click a form submit button and not any links separately. I know how to do this with <s:url>but that way I need to create a link instead of form submit.

我希望在单击表单提交按钮而不是单独的任何链接时传递此参数。我知道如何做到这一点,<s:url>但那样我需要创建一个链接而不是表单提交。

Can someone please help me with a code sample how to do this? I know there's a way to do it with HTML or Struts1 but how to do it with Struts2? If there is a way to do this in struts.xml, please explain with an example.

有人可以帮助我提供代码示例如何执行此操作吗?我知道有一种方法可以用 HTML 或 Struts1 来做到这一点,但如何用 Struts2 做到这一点?如果有办法做到这一点struts.xml,请举例说明。

<form action="/example/xyz.action?user=george" method="POST">

回答by Jaiwo99

try this:

试试这个:

<s:form action="xyz.action" method="GET">
    <s:hidden name="user" value="george"/>
    // other fields
</s:form>

method="GET"will show your parameter on the url, <s:hidden name="user" value="george"/>will take your parameter.

method="GET"将在网址上显示您的参数,<s:hidden name="user" value="george"/>将采用您的参数。

here is also an example for you Example

这里也是你的一个例子Example

回答by Roman C

It's possible if you use <s:urland <s:formtags.

如果您使用<s:url<s:form标记,这是可能的。

<%@ taglib prefix="s" uri="/struts-tags" %>

<s:url id="xyzUrl" action="xyz" includeContext="false"><s:param name="user" value="george"/></s:url>
<s:form name="xyzForm" action="%{xyzUrl}" method="POST">