如何将参数从JSP传递给Struts 2动作 [英] How to pass a parameter from JSP to Struts 2 action
问题描述
我有一个应用程序,每次按下该项目的按钮时,我都希望将项目 id
传递给操作。
I have an application and I want to pass item id
to the action every time the button for that item is pressed.
<s:submit value="addToCart" action="addToCart" type="submit">
<s:param name="id" value="%{#cpu.id}" />
</s:submit>
行动:
Action:
public class ProductsCPU extends BaseAction implements Preparable, SessionAware {
private static final long serialVersionUID = 2124421844550008773L;
private List colors = new ArrayList<>();
private List cpus;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
当我将id打印到控制台时,它有 null
value。有什么问题?
When I print id to console, it has the null
value. What is the problem?
推荐答案
问题是你不能参数化 s:submit
标记为 s:param
标记,如代码中使用 param
标记在<$的正文中c $ c>提交标签。
The problem is that you can't parametrize s:submit
tag with s:param
tag like in your code using param
tag in the body of submit
tag.
您也不想添加隐藏
field,因为您有多个值提交给操作。这是因为许多隐藏字段使用相同的名称呈现。您可以在提交表单之前使用仅一个字段并更新其值。
You also don't want to add hidden
field because you got multiple values submitted to the action. This is because many hidden fields are rendered with the same name. You could use only one field and update its value before submitting a form.
可能您使用了错误的标记将参数传递给操作。您可以使用锚标记并使用 param
标记对其进行参数化。
Probably you have used wrong tag to pass a parameter to the action. You can use anchor tag and parametrize it with param
tag.
第二种方法是使用javascript修改 action
属性。这样你也可以使用按钮
标签。
The second way is to use javascript to modify action
attribute. In this way you can also use a button
tag.
不推荐使用第三种方法,因为它需要为每个链接使用多个表单。通过这种方式,您可以直接向表单操作属性添加参数。
Third way is not recommended because it requires to use multiple forms one per each link. In this way you add a parameter to the form action attribute directly.
以下是上述选项的代码。
Below is the code for the options mentioned above.
<s:form name="myForm13" namespace="/" action="save?message=Hello param 3" theme="simple">
<br/><s:a cssClass="btn btn-primary" action="test"><s:param name="message">Hello param 1</s:param> Go </s:a>
<br/><s:a href="#" cssClass="btn btn-warning" onclick="myForm13.action='test?message=Hello param 2';myForm13.submit()">Submit</s:a>
<br/><s:submit cssClass="btn btn-danger" action="test"/>
</s:form>
这篇关于如何将参数从JSP传递给Struts 2动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!