0

I am trying to update database using angularjs and spring. Bellow code i was using but it is not working. app.js

$scope.updateUserInformation=function updateUserInformation(){

     alert("hai");

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

         alert("Update Successfull");
     });


 }

Controller

  @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="angular.js@*" 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>

I try to set the scope variable like $scope.student.Name=user.userame; even it is not working.how can i set the scope variable.

using the above program i try to update the database table but controller is not getting parameter can any one help me to fix this

2
  • what is the error on console? Commented Jul 17, 2017 at 6:26
  • You're sending JSON object as the body of the request on the angular side. So you need to get the JSON out of the body of the request on the spring side: @RequestMapping(value="/angular/edit/",method=RequestMethod.POST") public String updateUser(@RequestBody SomePojoMappedToYourJSONUsingJackson command) Commented Jul 17, 2017 at 6:27

1 Answer 1

1

Angular Controller

$scope.updateUserInformation=function updateUserInformation(){

     alert("hai");

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

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

Spring Controller

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

          String name=an.getUserame();
          ts.updateUser(an);
          return "AngularData";
}  
Sign up to request clarification or add additional context in comments.

8 Comments

Make sure in Angular class you have Name and City Attributes for mapping, because you have take ng-model="student.Name" so..
It's working fine.another thing i want to know how to set scope variable value like $scope.student.Name=data.userame;
If object have same name attributes it will automatically mapped other wise you have to do manually like $scope.student.Name=data.userame;..
objects are having same name while getting the values from db and display that into that text box how can i set the value to scope
just pass data to student object, $scope.student = data; so it will automatically set value of $scope.student.Name using ng-model.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.