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)
<script> var app = angular.module("myApp", []); app.controller("controller", function($scope,$http){ $scope.getDataFromServer = function() { $http.get("angularAction").success( function(data, status, headers, config) { $scope.person = data; }).error(function(data, statusenter code here, headers, config) { console.log("error message " + data); }); }; }); </script> </head> <body> <div ng-app="myApp" > <div ng-controller="controller" > <button ng-click="getDataFromServer()"> Fetch data from server </button> <p> First Name : {{person.firstName}} </p> <p> Last Name : {{person.lastName}} </p> </div> </div> </body> </html>
AngularAction.java
:
(Struts action class)
public class AngularAction extends ActionSupport { PersonData personData ; @Override public String execute() throws Exception { personData.setFirstName("test"); personData.setLastName("base"); return SUCCESS; } public PersonData getPersonData() { return personData; } public void setPersonData(PersonData personData) { this.personData = personData; } }
struts.xml
:
(Struts configuration file)
<?xml version="1.0" encoding="UTF-8"?> <struts> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="json-default"> <action name="angularAction" class="com.action.AngularAction" > <result type="json" > <param name="root">personData</param> <param name="excludeNullProperties">true</param> <param name="noCache">true</param> </result> </action> </package> </struts>
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
<action name="index"> <result>/index.jsp</result> </action>
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.
app.controller("controller", ['$scope', '$http', function ($scope, $http) { $scope.getDataFromServer = function() { $http.get("${pageContext.request.contextPath}/angularAction") .then(function(response) { $scope.person = response.data; }, function(response) { console.log("error message: " + response.statusText); }); }; }]);
Make sure that struts.xml
is found and parsed by Struts and Struts filter is mapped to handle all requests.