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.
angularjs spring
add a comment |
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.
angularjs spring
returnjson
instead ofstring
from the controller. i guess you are alertingfailure
.
– 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
add a comment |
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.
angularjs spring
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
angularjs spring
asked Nov 14 '14 at 9:29
Rameshbabu
25312
25312
returnjson
instead ofstring
from the controller. i guess you are alertingfailure
.
– 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
add a comment |
returnjson
instead ofstring
from the controller. i guess you are alertingfailure
.
– 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
add a comment |
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");
});
});
how it return hash as String
– Rameshbabu
Nov 14 '14 at 10:43
add a comment |
up vote
0
down vote
@Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you
add a comment |
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");
});
});
how it return hash as String
– Rameshbabu
Nov 14 '14 at 10:43
add a comment |
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");
});
});
how it return hash as String
– Rameshbabu
Nov 14 '14 at 10:43
add a comment |
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");
});
});
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");
});
});
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
add a comment |
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
add a comment |
up vote
0
down vote
@Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you
add a comment |
up vote
0
down vote
@Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you
add a comment |
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
@Rameshbabu have you looked into this URL http://www.javabeat.net/spring-mvc-angularjs-integration/ it might help you
answered Feb 7 '15 at 4:18
Arun
50851128
50851128
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
return
json
instead ofstring
from the controller. i guess you are alertingfailure
.– 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