Cadeq Cadeq - 4 months ago 26
Java Question

AngularJs send value to Struts2

i am having some problems to receive a value from angularJS in struts2 action.

The objective is to receive the index of the current image in a carousel.

selectArm.jsp

var app = angular.module('menuAPP', ['ui.bootstrap']);
app.controller('mainController', function($scope, $http, $window, $location) {
$scope.hideError=true;
$scope.show = function() {


$scope.currentIndex = $('div.active').index() + 1;//Carousel index
var params = "NSlider :"+ $scope.currentIndex;
var data = angular.toJson(params)
$http({
method: 'post',
url: 'showArm',
data: 'NSlider =' + data
}).success(function(response)
{
post("formShowBrazo");
});
};


ArmAction.java

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class BrazoAction implements Action
{
private static final Logger log = LogManager.getLogger(BrazoAction.class);
private String NSlider;

public String execute() throws Exception {

HttpSession session = ServletActionContext.getRequest().getSession();

session.setAttribute("forward","sBrazo");
String aux = NSlider.split(":")[1]; //Receive "NSlider : {int}"
session.setAttribute("id_brazo", aux);
return SUCCESS;
}

public String getNSlider() {
return NSlider;
}

public void setNSlider(String NSlider) {
this.nSlider = NSlider;
}


}

This jsp must show the index of selected picture in the carousel.

showArm.jsp

<body>
<script src="lib/threejs/three.min.js"></script>
<script src="lib/threejs/OrbitControls.js"></script>
<script src="lib/threejs/dat.gui.min.js"></script>
<script src="lib/threejs/stats.min.js"></script>
<script src="js/webGL.js"></script>
<%
out.println(session.getAttribute("id_brazo"));
%>
<div id='container' width=800, height=600>
</body>


If I debug the action, I can see that i don't receive any value from angular.

UPDATE
Changed the json petition, changed the name of get and set and also changed to string the parameter.

Now i receive null value. Still not working well.

UPDATE 2
Now show the final result. Now works fine.

Answer

This is how i pass data to Struts action please take reference from this.

$scope.submitForm = function() {

        var params = {
            "name" : $scope.name,
            "username" : $scope.username,
            "email" : $scope.email
        };

        var data = angular.toJson(params);
        $http( {
            method : 'POST',
            url : '/angular/studentForm.action',
            data : 'value=' + data,
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded'
            }
        }).success(function(data) {
            if (data.errors) {
                $scope.errorname = data.errors.name;
                $scope.errorUsername = data.errors.username;
                $scope.erroremail = data.errors.email;
            } else {
                console.log(data);
                $scope.message = data;

            }
        }).error(function(data, status) {
            return false;
        });
    };
});

You have to change the parameter to string because angular js only works with json data.