Skip to content
Advertisement

Calling Struts 2 action class from AngularJS with HTTP GET request but not getting any response

I am creating a simple application, that make a simple call to action of Struts 2 via AngularJS. I just create a simple HTTP GET request from AngularJS to get JSON from the server. At server end I am using Struts2 action class named as AngularAction.java.

But, all the time I am unable to hit the class, even I had print some dummy message at the action class but could not find anything.

Code files are:

index.jsp (index page from where I am sending a HTTP GET request to Struts class via AngularJS)

JavaScript

AngularAction.java: (Struts action class)

JavaScript

struts.xml: (Struts configuration file)

JavaScript

Advertisement

Answer

You should initialize personData in either way, using parameter, container injection, manually because you will get NullPointerException in the execute() method.

Struts2 uses action mapping to the URL to execute the action method. Action mapping has properties such as namespace, action name, parameters. When the URL is parsed the action mapping object is created and then it’s used to determine the action config. If not the action config is found you should get 404 error There's no action mapped for the action name [] and namespace [] in the context [].

From the struts.xml you have defined two attributes: the action name and namespace. Remains to define the third attribute – context.

The name of the JSP is index.jsp if it’s not a welcome file then you can use struts tags inside it, such as s:url if this page is returned as a result of the index action. Here the example of such action from the Struts2 Hello World application

JavaScript

If you are using index.jsp as welcome file, which is used by default in most web servers, then you have to provide the context path to the action URL.

JavaScript

Make sure that struts.xml is found and parsed by Struts and Struts filter is mapped to handle all requests.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement