如何使用 Struts2 从两个不同的按钮提交不同的操作
How to submit for different actions from two different button using Struts2
我有一个基本 JSP 页面,其中包含一个文本字段和两个按钮,如下所示。
<input id="tags" size="60" maxlength="60" autofocus name="config">
两个按钮,一个用于 Action1,另一个用于 Action2。
<form action="action1" method="get">
<div class="ui-widget" align="center">
<p>
<label for="tags"><strong><br>
<br>
DUT Name</strong>: </label>
<input id="tags1" size="60" maxlength="60" autofocus name="config1">
</p>
<p>
<input id="tags2" size="60" maxlength="60" autofocus name="config2">
</p>
</div>
</form>
每次提交时都会调用 action1,(如表单元素中所述)当用户按下 button2 时我如何调用 action2。意思是当用户按下按钮 1 和按钮 2 时如何调用不同的操作。
假设两个操作都可以处理相同的文本字段,并且我的操作 Class 和 Struts.xml 文件没有问题。
function submitForm(newActionValue){
alert($("#testForm").attr('action'));
$("#testForm").attr('action',newActionValue);
alert($("#testForm").attr('action'));
$("#testForm").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="testForm" action="XXX" method="post">
name:<input type="text" name="name"/>
<br/>
password:<input type="password" name="password"/>
</form>
<button id="button1" onclick="submitForm('action1')">button1</button>
<button id="button2" onclick="submitForm('action2')">button2</button>
我有一个基本 JSP 页面,其中包含一个文本字段和两个按钮,如下所示。
<input id="tags" size="60" maxlength="60" autofocus name="config">
两个按钮,一个用于 Action1,另一个用于 Action2。
<form action="action1" method="get">
<div class="ui-widget" align="center">
<p>
<label for="tags"><strong><br>
<br>
DUT Name</strong>: </label>
<input id="tags1" size="60" maxlength="60" autofocus name="config1">
</p>
<p>
<input id="tags2" size="60" maxlength="60" autofocus name="config2">
</p>
</div>
</form>
每次提交时都会调用 action1,(如表单元素中所述)当用户按下 button2 时我如何调用 action2。意思是当用户按下按钮 1 和按钮 2 时如何调用不同的操作。
假设两个操作都可以处理相同的文本字段,并且我的操作 Class 和 Struts.xml 文件没有问题。
function submitForm(newActionValue){
alert($("#testForm").attr('action'));
$("#testForm").attr('action',newActionValue);
alert($("#testForm").attr('action'));
$("#testForm").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="testForm" action="XXX" method="post">
name:<input type="text" name="name"/>
<br/>
password:<input type="password" name="password"/>
</form>
<button id="button1" onclick="submitForm('action1')">button1</button>
<button id="button2" onclick="submitForm('action2')">button2</button>