-1

I am using the following code to have different submit buttons for my form. The problem is that if I change the value of the Value attribute of each of the submit buttons I have to change the Java code too as if conditions are based on these values.

I am looking for an alternative solution to avoid this problem, so back-end and front-end would be independent.

JSP

      <s:form name="myform" method="POST" action="myformActions">
            .....
            <input id="sub1Btn" type="submit" name="action" value="mysubmit1"/>
            <input id="sub2Btn" type="submit" name="action" value="mysubmit2"/>
      </s:form>

Java

{    

 ....

 private String action;
 ....
 public void myformActions(){

    if(action.equalsIgnoreCase("mysubmit1") //if I change the value need to change this as well
    { 
       do whatever is required to fulfill the request of mysubmit1 ...
    }

    if(action.equalsIgnoreCase("mysubmit2") //if I change the value need to change this as well
    {  

       do whatever is required to fulfill the request of mysubmit2 ...
    }

  }
}
J888
  • 1,944
  • 8
  • 42
  • 76
  • without some sort of stable attribute between the form on the front end and the handler on the backend, what is the purpose of having multiple submit buttons? Maybe if you gave some context to your project I could understand better. – chiliNUT Nov 22 '13 at 03:06
  • 1
    @chiliNUT Imagine you have access to a list of messages and you have options to reply, move or delete them . Once you choose one of the messages, the form is used to pass the id of selected message to back-end and java code is supposed to return the associated page to the selected action. Then you can see the result of deletion, a reply form or a move form. Although there are many other situations that you may need different submit btns for a single form to avoid having different separate forms. – J888 Nov 22 '13 at 03:16
  • Right, so then wouldn't you need to somehow communicate that action back to your servlet? It seems like the value attribute is doing that for you. There are other ways to do it but they still involve a common value that the client sends and the server sees. How will the server know the desired action is to "reply" if the client does not tell the server it wishes to complete a reply request? – chiliNUT Nov 22 '13 at 03:25
  • when it is delete a "success message" will be shown and the page will be re-generated to show the required message is deleted, if it is move or reply associated form will be shown. Actually I do not get your question. – J888 Nov 22 '13 at 03:27
  • Right, how does the server know what situation to present? – chiliNUT Nov 22 '13 at 04:50
  • 1
    it is quite easy you can figure out by having a look at the code, when you click on one of the btns its value will be sent to server, let say you press btn1 then "mysubmit1" will be passed .... – J888 Nov 22 '13 at 04:56
  • 2
    Yeah exactly, that's what I'm saying! The server knows what to do based on that value. So that's why I don't understand why you are looking for another way of doing this. – chiliNUT Nov 22 '13 at 05:07
  • because if I change the value lets say Delete to remove I have to change it in the JavaCode as well. I am looking for a method to minimize the dependency of back-end to front-end. A method to figure out the presses btn without using its value. Take localization as an example – J888 Nov 22 '13 at 05:14
  • 1
    lets say one time "move" btn is "move" the other time it needs to be in another language let say it should be "XXX" then in this situation or any other similar one I have to consider all those values as well. – J888 Nov 22 '13 at 05:18
  • I don't think you have any option other than checking for all possible values server side. Other SOers feel free to provide another answer – chiliNUT Nov 22 '13 at 05:23
  • 1
    @chiliNUT please vote it up to see if someone else has a better idea, thanks – J888 Nov 22 '13 at 05:25
  • 1
    @J888: Take a look at that question: http://stackoverflow.com/q/13343954/1700321. – Aleksandr M Nov 22 '13 at 08:34
  • 1
    http://stackoverflow.com/a/14467399/573032 – Roman C Nov 22 '13 at 12:47
  • Using the same form for many functions is sloppy coding. You would be better off making several forms, each with their own proper servlet to respond, rather than one form with a slew of if-statements in the servlet. – developerwjk Nov 22 '13 at 20:09
  • @RomanC great it works, please write the code in answer section, – J888 Nov 24 '13 at 22:32

3 Answers3

1

I didn't go through all the comments, but from your question , I have an idea like you can use some javascript functions to set the action for the form depending on the criteria you want.So when the form is submitted , corresponding action will be invoked. You can even use a single action class and different methods like edit , add etc , which you can map in the struts.xml file. In this case mapping can be done like action="*user" method="{1}" so the method edit() in the action class will be invoked for action editUser and delete() for deleteUser ...

dileepVikram
  • 890
  • 4
  • 14
  • 30
1

This solution may not be good in all cases and will not directly answer the question. My solution depends on the fundamental of your actions. If your button defines different actions on a object, you can do below.

This will not move buttons out of your action, but put them in the place they should be.

Consider a page with a form, the user can complete the form and submit it to result page. The result page will have export buttons to let user save the form in html,pdf,excel formats.

Your jsp

    <s:submit button="true" key="form.btn.export.pdf" name="export" />
    <s:submit button="true" key="form.btn.export.excel" name="export"/>
    <s:submit button="true" key="form.btn.export.html" name="export"  />

Then in your action the export setter will be if/else or switch statements (not a simple setter):

public void setExport(String exportBtn) {
    if (exportBtn.toUpperCase().contains("PDF")) {
        this.export = "PDF";
    } else if (exportBtn.toUpperCase().contains("EXCEL")) {
        this.export = "XLSX";
    } else if (exportBtn.toUpperCase().contains("CVS")) {
        this.export = "CVS";
    } else if (exportBtn.toUpperCase().contains("HTML")) {
        this.export = "HTML";
    }

    LOG.debug("Exporting to is " + this.export);
}

Then in your action doesn't need any if/else.

@Action(value = "export-action")
public String exportMethod(){
ExportHelper ex= new ExportHelper("report",export);//No if is required
inputStream = new ByteArrayInputStream( ExportHelper);
}

This easy solution make your actions more easy to maintenance.

Hint. In this solution the backend and front end are not coupled! We are coupling the front end(jsp) to Controller(Struts Action). Which is not a bad approach.

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
0

use method or action attributes of the submit tag to redirect each to different method / action.

AlexCartio1
  • 238
  • 3
  • 9
  • 29