我有一个应用程序,我想在每次按下该项目的按钮时将项目 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>
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
值.有什么问题?
When I print id to console, it has the null
value. What is the problem?
问题是你不能用 s:param
标签参数化 s:submit
标签在您的代码中使用 submit
标签正文中的 param
标签.
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.
您也不想添加 hidden
字段,因为您有多个值提交给操作.这是因为许多隐藏字段以相同的名称呈现.您可以只使用一个字段并在提交表单之前更新其值.
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
属性.通过这种方式,您还可以使用 button
标记.
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 动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!