2017-07-17 43 views
0

我想用angularjs和spring更新數據庫。 我正在使用的貝婁代碼,但它不工作。 app.js如何使用spring更新angularjs的數據庫?

$scope.updateUserInformation=function updateUserInformation(){ 

    alert("hai"); 

    $http.post(urlBase+'/angular/edit/',{student:$scope.student}).success(function (data){ 

     alert("Update Successfull"); 
    }); 


} 

控制器

@RequestMapping(value="/angular/edit/",method=RequestMethod.POST,params="{student}") 
     public String updateUser(@RequestParam("student") Angular an) throws ParseException{ 

      String name=an.getUserame(); 
      ts.updateUser(an); 
      return "AngularData"; 




     } 

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 

    pageEncoding="ISO-8859-1"%> 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

<html ng-app="taskManagerApp"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 

<title>AngularJS Task Manager</title> 

<script data-require="[email protected]*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js"></script> 

<script src="<c:url value="/resources/js/app.js"/>"></script> 
</head> 

<body> 

<div ng-controller="taskManagerController"> 



    <span>Add Task</span> 


    <div> 

    <div> 

    <table> 

    <tr> 

     <td> Name:</td> 

     <td><input type="text" ng-model="student.Name"/></td> 

    </tr> 

    <tr> 

     <td>City:</td> 

     <td><input type="text" ng-model="student.City"/></td> 

    </tr> 


    <tr> 

     <td> 

<button ng-click="addTask()" class="btn-panel-big">Add New Task</button></td> 

<td><button ng-click="updateUserInformation()" class="btn-panel-big">Update User</button></td> 

    </tr> 

    </table>   

    </div> 

    </div> 


    <div> 
    <table> 
    <div> 

    <tr ng-repeat="user in users"> 
    <td> {{user.id}}</td> 
    <td> {{user.userame}}</td> 
    <td> {{user.city}}</td> 
    <td><button ng-click="updateUser(user)" class="btn-panel-big">Edit</button></td></td> 
    </tr> 

    </div> 
    </table> 

    </div> 


    </div> 


</body> 

</html> 

我嘗試設置像$scope.student.Name=user.userame;範圍變量,甚至是不working.how可以我設置範圍可變即使用

上面的程序我嘗試更新數據庫表,但控制器沒有得到參數任何一個可以幫助我解決這個

+0

控制檯上的錯誤是什麼? –

+0

您正在發送JSON對象作爲請求的主體。所以你需要從彈出端的請求體獲取JSON:'@RequestMapping(value =「/ angular/edit /」,method = RequestMethod.POST「)public String updateUser(@RequestBody SomePojoMappedToYourJSONUsingJackson command) –

回答

1

角控制器

$scope.updateUserInformation=function updateUserInformation(){ 

    alert("hai"); 

    $http.post(urlBase+'/angular/edit/',$scope.student).success(function (data){ 

     alert("Update Successfull"); 
    }); 
} 

春季控制器

@RequestMapping(value="/angular/edit/",method=RequestMethod.POST) 
     public String updateUser(@RequestBody Angular an) throws ParseException{ 

      String name=an.getUserame(); 
      ts.updateUser(an); 
      return "AngularData"; 
} 
+0

我試過了,但對象'an'中沒有數據 – KVK

+1

請確保在Angular類中有'Name'和'City'屬性進行映射,因爲你已經把'ng-model =「student.Name」' .. – hrdkisback

+0

它的工作很好。另一件事,我想知道如何設置範圍變量值,如$ scope.student.Name = data.userame; – KVK

相關問題