나는 봄에 새로운 것이므로 그물에 대한 튜토리얼을 배워야한다. 프론트 엔드로 AngularJS를 사용하고 JSON을 사용하여 스프링 컨트롤러에 데이터를 게시하는 간단한 스프링 MVC 로그인 애플리케이션을 만들었습니다.스프링 컨트롤러에 데이터를 게시하지 않는 폼
내 간단한 로그인 폼 :
<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>
내 각 컨트롤러 :
function loginController($scope){
$scope.login=function(){
var data=$scope.fields;
var response = $http.post("rest/dologin",data);
alert(response);
};
};
내의 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>
내 봄 컨트롤러 :
@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();
}
}
내 디스패처 서블릿 :
<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>
나는 AngularJS와 컨트롤러에 경고 상자를 넣어 시도하고 표시하지만 내 봄 컨트롤러에서 인쇄 문이 인쇄되지 않습니다. 또한 오류가 표시되지 않습니다.
업데이트 :
내가 나의 봄 컨트롤러에 게시 데이터를 시도하고 정확한 응답을 제공합니다. 문제는 AngularJS 컨트롤러 때문입니다. 방화 광을 사용하여 각도 컨트롤러의 로그인 방법이 호출되지 않았지만 스크립트 자체가로드되는지 확인했습니다.
갱신 2는 :
나는이 내 AngularJS와 컨트롤러를 변경하고 이제 컨트롤러가 감지지고 :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);
});
}
내가 방화범에 "오류 400 잘못된 요청"오류 코드를 얻을이 시간 :
<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>
};
}]);
서버가주고 다음과 같은 예외 :
NotReadableException: Required request body is missing
내가 여기서 잘못하고있는 것을 제안하십시오.
의견을 보내 주셔서 감사합니다. 당신이 말하는 것은 제게 의미가 있습니다. 이러한 변경 사항을 코드에 포함 시켰습니다. 그래도 여전히 같은 오류 : ( – k19
링크를 많이 주셔서 감사합니다. 좋은, 그것을 똑같은 문제를 복제하려고 ... – k19