How to call spring controller via Angularjs Controller











up vote
0
down vote

favorite












Hi i am new to spring restful service using angular.js, i need to display data returning from spring controller via angular js. Here i hava attached the code



Controller file:-



        @Controller
public class Control {
@RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
public @ResponseBody String show(){
String msg = "welcome to spring angular js";
return msg;

}}


servlet file:



          <context:component-scan base-package="spring" />

<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="useJaf" value="false"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>


index.jsp file:



         <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index file</h1>

<a href="#/nextfile">Click</a>

<div ng-view></div>

</body>
</html>


index1.jsp file:



            <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index1 file</h1>

<div ng-controller="routeController">

<p>Message is:{{$scope.Data}} </p>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>
</div>
</body>
</html>


Script.js file:



             var app = angular.module("sample",['ngRoute']);
app.config(['$routeProvider',function($routeProvider){
$routeProvider.when("/nextfile",{
templateUrl: 'index1.jsp',
controller: 'routeController'
});
}]);


Controller.js file:



            app.controller('routeController',function ($scope,$http){
$http.get('http://domainname:8080/sample/getContent.json').success(function(data) {alert("success");$scope.Data =data;}).error(function(data){alert("failure");});


});



Please specify a suggestion, Thanks in advance.










share|improve this question






















  • return json instead of string from the controller. i guess you are alerting failure.
    – Jai
    Nov 14 '14 at 9:35












  • s to whether it's returning data or not Mr.jai.
    – Rameshbabu
    Nov 14 '14 at 9:38










  • how to return it as json????...
    – Rameshbabu
    Nov 14 '14 at 9:39










  • sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
    – Jai
    Nov 14 '14 at 9:42










  • changed to spring but still it doesn't call the spring controller
    – Rameshbabu
    Nov 14 '14 at 10:11















up vote
0
down vote

favorite












Hi i am new to spring restful service using angular.js, i need to display data returning from spring controller via angular js. Here i hava attached the code



Controller file:-



        @Controller
public class Control {
@RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
public @ResponseBody String show(){
String msg = "welcome to spring angular js";
return msg;

}}


servlet file:



          <context:component-scan base-package="spring" />

<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="useJaf" value="false"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>


index.jsp file:



         <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index file</h1>

<a href="#/nextfile">Click</a>

<div ng-view></div>

</body>
</html>


index1.jsp file:



            <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index1 file</h1>

<div ng-controller="routeController">

<p>Message is:{{$scope.Data}} </p>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>
</div>
</body>
</html>


Script.js file:



             var app = angular.module("sample",['ngRoute']);
app.config(['$routeProvider',function($routeProvider){
$routeProvider.when("/nextfile",{
templateUrl: 'index1.jsp',
controller: 'routeController'
});
}]);


Controller.js file:



            app.controller('routeController',function ($scope,$http){
$http.get('http://domainname:8080/sample/getContent.json').success(function(data) {alert("success");$scope.Data =data;}).error(function(data){alert("failure");});


});



Please specify a suggestion, Thanks in advance.










share|improve this question






















  • return json instead of string from the controller. i guess you are alerting failure.
    – Jai
    Nov 14 '14 at 9:35












  • s to whether it's returning data or not Mr.jai.
    – Rameshbabu
    Nov 14 '14 at 9:38










  • how to return it as json????...
    – Rameshbabu
    Nov 14 '14 at 9:39










  • sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
    – Jai
    Nov 14 '14 at 9:42










  • changed to spring but still it doesn't call the spring controller
    – Rameshbabu
    Nov 14 '14 at 10:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hi i am new to spring restful service using angular.js, i need to display data returning from spring controller via angular js. Here i hava attached the code



Controller file:-



        @Controller
public class Control {
@RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
public @ResponseBody String show(){
String msg = "welcome to spring angular js";
return msg;

}}


servlet file:



          <context:component-scan base-package="spring" />

<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="useJaf" value="false"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>


index.jsp file:



         <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index file</h1>

<a href="#/nextfile">Click</a>

<div ng-view></div>

</body>
</html>


index1.jsp file:



            <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index1 file</h1>

<div ng-controller="routeController">

<p>Message is:{{$scope.Data}} </p>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>
</div>
</body>
</html>


Script.js file:



             var app = angular.module("sample",['ngRoute']);
app.config(['$routeProvider',function($routeProvider){
$routeProvider.when("/nextfile",{
templateUrl: 'index1.jsp',
controller: 'routeController'
});
}]);


Controller.js file:



            app.controller('routeController',function ($scope,$http){
$http.get('http://domainname:8080/sample/getContent.json').success(function(data) {alert("success");$scope.Data =data;}).error(function(data){alert("failure");});


});



Please specify a suggestion, Thanks in advance.










share|improve this question













Hi i am new to spring restful service using angular.js, i need to display data returning from spring controller via angular js. Here i hava attached the code



Controller file:-



        @Controller
public class Control {
@RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
public @ResponseBody String show(){
String msg = "welcome to spring angular js";
return msg;

}}


servlet file:



          <context:component-scan base-package="spring" />

<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="useJaf" value="false"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>


index.jsp file:



         <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index file</h1>

<a href="#/nextfile">Click</a>

<div ng-view></div>

</body>
</html>


index1.jsp file:



            <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>

</head>
<body>
<h1>Index1 file</h1>

<div ng-controller="routeController">

<p>Message is:{{$scope.Data}} </p>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="controller.js"></script>
</div>
</body>
</html>


Script.js file:



             var app = angular.module("sample",['ngRoute']);
app.config(['$routeProvider',function($routeProvider){
$routeProvider.when("/nextfile",{
templateUrl: 'index1.jsp',
controller: 'routeController'
});
}]);


Controller.js file:



            app.controller('routeController',function ($scope,$http){
$http.get('http://domainname:8080/sample/getContent.json').success(function(data) {alert("success");$scope.Data =data;}).error(function(data){alert("failure");});


});



Please specify a suggestion, Thanks in advance.







angularjs spring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '14 at 9:29









Rameshbabu

25312




25312












  • return json instead of string from the controller. i guess you are alerting failure.
    – Jai
    Nov 14 '14 at 9:35












  • s to whether it's returning data or not Mr.jai.
    – Rameshbabu
    Nov 14 '14 at 9:38










  • how to return it as json????...
    – Rameshbabu
    Nov 14 '14 at 9:39










  • sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
    – Jai
    Nov 14 '14 at 9:42










  • changed to spring but still it doesn't call the spring controller
    – Rameshbabu
    Nov 14 '14 at 10:11


















  • return json instead of string from the controller. i guess you are alerting failure.
    – Jai
    Nov 14 '14 at 9:35












  • s to whether it's returning data or not Mr.jai.
    – Rameshbabu
    Nov 14 '14 at 9:38










  • how to return it as json????...
    – Rameshbabu
    Nov 14 '14 at 9:39










  • sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
    – Jai
    Nov 14 '14 at 9:42










  • changed to spring but still it doesn't call the spring controller
    – Rameshbabu
    Nov 14 '14 at 10:11
















return json instead of string from the controller. i guess you are alerting failure.
– Jai
Nov 14 '14 at 9:35






return json instead of string from the controller. i guess you are alerting failure.
– Jai
Nov 14 '14 at 9:35














s to whether it's returning data or not Mr.jai.
– Rameshbabu
Nov 14 '14 at 9:38




s to whether it's returning data or not Mr.jai.
– Rameshbabu
Nov 14 '14 at 9:38












how to return it as json????...
– Rameshbabu
Nov 14 '14 at 9:39




how to return it as json????...
– Rameshbabu
Nov 14 '14 at 9:39












sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
– Jai
Nov 14 '14 at 9:42




sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples will help you.
– Jai
Nov 14 '14 at 9:42












changed to spring but still it doesn't call the spring controller
– Rameshbabu
Nov 14 '14 at 10:11




changed to spring but still it doesn't call the spring controller
– Rameshbabu
Nov 14 '14 at 10:11












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Simply you can return this way:



        @Controller
public class Control {
@RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
public @ResponseBody String show(){
HashMap hash = new HashMap();
hash.put("msg", "welcome to spring angular js");
return hash; // should print {msg=welcome to spring angular js}
} // in eclipse console
}


and in your Controller.js:



     app.controller('routeController',function ($scope, $http){
$http.get('http://domainname:8080/sample/getContent').
success(function(data) {
alert("success");
$scope.Data = data;
}).
error(function(data){
alert("failure");
});
});





share|improve this answer





















  • how it return hash as String
    – Rameshbabu
    Nov 14 '14 at 10:43


















up vote
0
down vote













@Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26926753%2fhow-to-call-spring-controller-via-angularjs-controller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Simply you can return this way:



            @Controller
    public class Control {
    @RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
    public @ResponseBody String show(){
    HashMap hash = new HashMap();
    hash.put("msg", "welcome to spring angular js");
    return hash; // should print {msg=welcome to spring angular js}
    } // in eclipse console
    }


    and in your Controller.js:



         app.controller('routeController',function ($scope, $http){
    $http.get('http://domainname:8080/sample/getContent').
    success(function(data) {
    alert("success");
    $scope.Data = data;
    }).
    error(function(data){
    alert("failure");
    });
    });





    share|improve this answer





















    • how it return hash as String
      – Rameshbabu
      Nov 14 '14 at 10:43















    up vote
    0
    down vote













    Simply you can return this way:



            @Controller
    public class Control {
    @RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
    public @ResponseBody String show(){
    HashMap hash = new HashMap();
    hash.put("msg", "welcome to spring angular js");
    return hash; // should print {msg=welcome to spring angular js}
    } // in eclipse console
    }


    and in your Controller.js:



         app.controller('routeController',function ($scope, $http){
    $http.get('http://domainname:8080/sample/getContent').
    success(function(data) {
    alert("success");
    $scope.Data = data;
    }).
    error(function(data){
    alert("failure");
    });
    });





    share|improve this answer





















    • how it return hash as String
      – Rameshbabu
      Nov 14 '14 at 10:43













    up vote
    0
    down vote










    up vote
    0
    down vote









    Simply you can return this way:



            @Controller
    public class Control {
    @RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
    public @ResponseBody String show(){
    HashMap hash = new HashMap();
    hash.put("msg", "welcome to spring angular js");
    return hash; // should print {msg=welcome to spring angular js}
    } // in eclipse console
    }


    and in your Controller.js:



         app.controller('routeController',function ($scope, $http){
    $http.get('http://domainname:8080/sample/getContent').
    success(function(data) {
    alert("success");
    $scope.Data = data;
    }).
    error(function(data){
    alert("failure");
    });
    });





    share|improve this answer












    Simply you can return this way:



            @Controller
    public class Control {
    @RequestMapping(value="/getContent",method = RequestMethod.GET,produces = {"application/json"})
    public @ResponseBody String show(){
    HashMap hash = new HashMap();
    hash.put("msg", "welcome to spring angular js");
    return hash; // should print {msg=welcome to spring angular js}
    } // in eclipse console
    }


    and in your Controller.js:



         app.controller('routeController',function ($scope, $http){
    $http.get('http://domainname:8080/sample/getContent').
    success(function(data) {
    alert("success");
    $scope.Data = data;
    }).
    error(function(data){
    alert("failure");
    });
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '14 at 10:30









    Jai

    63.4k95479




    63.4k95479












    • how it return hash as String
      – Rameshbabu
      Nov 14 '14 at 10:43


















    • how it return hash as String
      – Rameshbabu
      Nov 14 '14 at 10:43
















    how it return hash as String
    – Rameshbabu
    Nov 14 '14 at 10:43




    how it return hash as String
    – Rameshbabu
    Nov 14 '14 at 10:43












    up vote
    0
    down vote













    @Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you






    share|improve this answer

























      up vote
      0
      down vote













      @Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        @Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you






        share|improve this answer












        @Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 7 '15 at 4:18









        Arun

        50851128




        50851128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26926753%2fhow-to-call-spring-controller-via-angularjs-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff