I am really new to Spring and tring to learn through some tutorials on the net. I created a simple Spring MVC Login App using AngularJS as the frontend and using JSON to post data to Spring Controller.
My simple login form:
<form id="loginForm" ng-submit="login()" novalidate ng-controller="loginController" align="center">
<fieldset>
<div class="form-group col-lg-10">
<label for="username" class="control-label">Username</label>
<input class="form-control" ng-model="username" placeholder="Username" type="text">
</div>
<div class="form-group col-lg-10">
<label for="password" class="control-label">Password</label>
<input class="form-control" ng-model="password" placeholder="Password" type="password">
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-25">
<button type="submit" class="btn btn-primary">Login</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</div>
</fieldset>
</form>
My angular controller:
function loginController($scope){
$scope.login=function(){
var data=$scope.fields;
var response = $http.post("rest/dologin",data);
alert(response);
};
};
My Web.xml:
<welcome-file-list>
<welcome-file>pages/login.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-core.xml
</param-value>
</context-param>
<!-- Spring MVC -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
My Spring Controller:
@Controller
public class LoginController {
@RequestMapping(value = "/dologin", method = RequestMethod.POST, headers = {"Content-type=application/json"})
protected @ResponseBody String validateUser(@RequestBody User user) {
System.out.println(user.getUsername() + " " + user.getPassword());
return "Sucess" + " " + user.getUsername();
}
}
My Dispatcher Servlet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.app.web.controllers" />
<mvc:annotation-driven />
</beans>
I tried putting an alert box in the Angularjs controller and it displays but the print statement in my Spring Controller doesn't print. Also, there are no errors displayed.
Update:
I tried posting data to my Spring controller and it gives correct response. So, problem is with the AngularJS Controller. I tried using firebug and see that the login method in angular controller is not called but the script itself is loaded.
Update2:
I changed my AngularJS controller to this and now the controller is getting detected:
var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController', ['$scope','$http',function($scope,$http) {
$scope.login = function() {
var data=$scope.fields;
var response = $http.post('/test/rest/dologin',data).success(function(data){
alert(response);
});
}
This time I get a "Error 400 Bad request" error code in Firebug:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 400 BAD_REQUEST</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /test/rest/dologin. Reason:
<pre> BAD_REQUEST</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</body>
</html>
};
}]);
The server is giving following exception:
NotReadableException: Required request body is missing
Please suggest what I am doing wrong here.
Aucun commentaire:
Enregistrer un commentaire