Two buttons in to decide in the action class

2019-08-14 05:58发布

Is it possible to do something like this?

<s:form action="formAction1" namespace="/form1">
  <s:form action="formAction2" namespace="/form2">
    <s:textfield name="user"/>
    <s:password name="pwd" />
    <s:submit name="sub1" value="start1">
    <s:submit name="sub2" value="start2">
  </s:form>
</s:form>     

i.e. if I click start1 button, then formAction1 will be submitted. else when start2 is clicked, then formAction2 will be submitted. Actually I want to have 2 different actions on 2 different buttons in a single form.

I Don't want to use url <!-- s:url --> tag.

2条回答
不美不萌又怎样
2楼-- · 2019-08-14 06:15

No, it's not possible in HTML, nor in Struts2. You should not have nested forms, but you can use several forms on the page.

<s:form>
 <s:textfield name="user"/>
 <s:password name="pwd" />
 <s:submit name="sub1" value="start1" action="formAction1">
 <s:submit name="sub2" value="start2" action="formAction2">
</s:form>

Also see How to align two submit buttons?. There's also information how to configure Struts2 to use action prefix.

查看更多
Viruses.
3楼-- · 2019-08-14 06:28

It is not possible as you have shown in your code; but you can have a one form with multiple buttons & different action can be perform on each button.

you can achieve it by using following code

<s:form method="post" action="mySubmitAction">
    // your form fields
    <s:submit value="Submit"/>
    <s:submit value="Clear" action="myClearAction"/>
</form>

Here if you click on first button mySubmitAction will be called.

If you click on second button myClearAction will be called(as we have provided action attribute in second button, instead of mySubmitAction action myClearAction action will be called).

You can access all the form field in both actions.

Hope this will help you.

查看更多
登录 后发表回答