0

I am migrating from struts 1 to struts 2 framework. Based on struts 1 framework parameter=method attribute, I am able to execute different methods using the same jsp page by adding a hidden field "method".

How do I achieve the same in struts 2?

My Action class:

public class MyAction extends ActionSupport {
    public String methodA() {
        return "a";
    }

    public String methodB() {
        return "b";
    }
}

My JSP page

<s:form action="MyAction">
    <s:select label="Method Name"
       name="method"
       headerKey="-1" headerValue="Select Method"
       list="#{'01':'A', '02':'B', [...]}"
       value="selectedMethod"
       required="true"
    />
    <s:submit type="button" name="submit" />
</s:form>
ilovetolearn
  • 2,006
  • 5
  • 33
  • 64

1 Answers1

1

You could achieve that by changing the "action" url before submitting.

Check out Wildcard Method and Dynamic Method Invocation here

Though, the dynamic Method Invocation can be considered a Security Vulnerability

PKey
  • 3,715
  • 1
  • 14
  • 39
  • changing the form.action using JavaScript? – ilovetolearn Aug 12 '16 at 15:13
  • @youcanlearnanything yes, something like " document.myform.action ='actionName_method.action' ", you can do that, say, within select's onChange method. (sorry, if you are expecting the exact code for your case - cannot provide you with that - due to lack of time and resources) – PKey Aug 12 '16 at 15:33
  • I got it. I couldn't think of any other way too! I just wanted to know if there is any alternative. – ilovetolearn Aug 12 '16 at 15:58
  • @youcanlearnanything you can also use hidden field as you have before if you use dynamic method invocation -> (it is described in the 1st link I have provided in the answer) – PKey Aug 12 '16 at 16:19
  • that's a security risk which I will avoid. doing so – ilovetolearn Aug 13 '16 at 03:49
  • @youcanlearnanything if you are using struts 2.5, then take a look at [this](http://stackoverflow.com/questions/37218790/wildcard-action-mapping-no-longer-working-after-updating-to-struts-2-5) also, to avoid any confusion. There are now more strict rules regarding usage of wildcard method. – PKey Aug 13 '16 at 05:26