1

Can I able to access method as action in Struts 2 with Codebehind plugin? Since there is no struts.xml file and its works as action!method in the URL.

Just I'm wondering to access a method

How to use Struts tag on class?

"@Action(name="action"), <s:url action="action" method="method" />

Class :

@Action(name = "JSONexample")
@Result(name="success",type=JSONResult.class,value="",params={"root","List"})
public class PartAction extends ActionSupport {

public String JSONexample{
-----
return SUCCESS;
}

public String readxml { }
}

if I access below method on submit button click , will I get the JSON data?

 $.getJSON('ajax/sayHi.action', formInput,function(data) 

So i can access action in URL as

localhost:7001/Sample/JSONmethod.action

to get the JSON data ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2444474
  • 623
  • 8
  • 21
  • 40

1 Answers1

1

As far as Codebehind Plugin doesn't support actions on methods you need to use Convention Plugin then use convention annotation @Action on method you want to be your action then you don't need ! explicit mapping.

If you continue to use the Codebehind Plugin, then you have to use Dynamic Action Invocation (DMI) to call the method other than mapped to your action.

Another time had to reread you question, it seems you want to access the action that has not mapping to the method other that execute:

  1. It isn't supported by the codebehind plugin;
  2. You have not mapped it via struts.xml. In this case the answer would be - no, you can't. But you can always use execute method!

If you continue with using of DMI better use s:url tag to construct the URL and use the method attribute.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for reply.What is name attribute for @Action? like @Action(name = "") ? – user2444474 Jun 29 '13 at 16:22
  • Like `@Action(name="action"), ` – Roman C Jun 29 '13 at 16:27
  • Okay , Will try conversion. Its very hard working in codebehind.One more last question, So no way to access action " $.getJSON('ajax/sayHi.action', formInput,function(data) " like this ? Method updated in question section. – user2444474 Jun 29 '13 at 17:28
  • No, not like that, I reviewed the code of codebehind plugin at first I thought It should, because you can do this with xml, but unfortunately it's looking only for annotation on class, not on method. – Roman C Jun 29 '13 at 18:04
  • I mean you can use $.getJSON but you should put annotation on class and name it name="sayHi", the action name should be the same in url and annotation attribute. – Roman C Jun 29 '13 at 18:17
  • How to use struts tag url in action class ? – user2444474 Jun 29 '13 at 21:36
  • Just place it above the class definition. – Roman C Jun 30 '13 at 13:25
  • @Action(name="action"), @Result(name="success",type=JSONResult.class,value="",params={"root","List"}) public class PartAction extends ActionSupport { like this ? How it will accept tag syntax in class ? – user2444474 Jun 30 '13 at 13:45
  • No, that syntax is incorrect, s:url tag is used in jsp not in java – Roman C Jun 30 '13 at 13:49