1
    My Controller:

How to get values from spring MVC to Angular js to display in jsp page. Below i posted my spring MVC and Angular js config code.Please some one help how to grt values from Spring controller.how to get url path of our controller.

    package com.dineshonjava.controller;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;






    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    import com.dineshonjava.bean.EmployeeBean;
    import com.dineshonjava.model.Employee;
    import com.dineshonjava.service.EmployeeService;


    @Controller
    public class EmployeeController {

        @Autowired
        private EmployeeService employeeService;







        @RequestMapping(value = "/search", method = RequestMethod.GET)
        public Map<String, Object> search(@ModelAttribute("command")  EmployeeBean employeeBean,
                BindingResult result) {

            Map<String, Object> model = new HashMap<String, Object>();
            model.put("employees",  prepareListofBean(employeeService.listEmployeess()));


            return (model);
        }




        private List<EmployeeBean> prepareListofBean(List<Employee> employees){
            List<EmployeeBean> beans = null;
            if(employees != null && !employees.isEmpty()){
                beans = new ArrayList<EmployeeBean>();
                EmployeeBean bean = null;
                for(Employee employee : employees){
                    bean = new EmployeeBean();
                    bean.setName(employee.getEmpName());
                    bean.setId(employee.getEmpId());
                    bean.setAddress(employee.getEmpAddress());
                    bean.setSalary(employee.getSalary());
                    bean.setAge(employee.getEmpAge());
                    bean.setBloodgrp(employee.getBloodgrp());
                    bean.setAids(employee.getAids());
                    bean.setWeight(employee.getWeight());
                    bean.setPass(employee.getPass());
                    beans.add(bean);
                }
            }
            return beans;
        }


    }



    Angular js page:

This is my angular js page here i try to get data's from Spring mvc controller but it fails . please explain how to get data from spring MVC .im new to angular js and spring mvc this is my task im fresher .

<html>
<head>
<script>
    var app = angular.module('angularjsTable', ['angularUtils.directives.dirPagination']);
     app.controller('listitemdata',function($scope, $http){
         $scope.employees = []; 
         $http.get("http://localhost:8080/sdnext/search").success(function(response){ 
             $scope.employees = response; 
});
});
</script>
</head>
<tbody>
<tr dir-paginate="employee in employees">

<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.age}}</td>
<td>{{employee.salary}}</td>
<td>{{employee.address}}</td>
<td>{{employee.bloodgrp}}</td>
<td>{{employee.aids}}</td>
<td>{{employee.weight}}</td>
</tr>
</tbody>

</html>
Guvaliour
  • 397
  • 1
  • 5
  • 17

1 Answers1

1

Dont use any model just send data In response and convert List Of DTO Object in json

 @RequestMapping(value = "/search", method = RequestMethod.GET)
      public void  search(HttpServletResponse res,HttpServletRequest req) {

      List<EmployeeBean> data =  employeeService.listEmployeess();
        JSONArray array = new JSONArray();
            for (EmployeeBean e : data) {
                JSONObject jsonObject = new JSONObject(e);
                array.put(jsonObject);
            }
         response.getWriter().append(array.toString());
        }

in pom.xml for jsonObject

 <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>
Raju
  • 459
  • 5
  • 23
  • 1
    How to get my data in Angular js page – Guvaliour Nov 20 '17 at 03:10
  • josn:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON – Raju Nov 20 '17 at 04:51
  • How to get this value in angular js page to display the details in JSP page.please some one help to get this value and view it in Angular JS using Ajax call. – Guvaliour Nov 22 '17 at 10:08
  • @RequestMapping(value = "/search", method = RequestMethod.GET) public void search(HttpServletResponse res,HttpServletRequest req) throws IOException { List data = employeeService.listEmployeess(); JSONArray array = new JSONArray(); for (Employee e : data) { JSONObject jsonObject = new JSONObject(e); array.put(jsonObject); } res.getWriter().append(array.toString()); } – Guvaliour Nov 22 '17 at 10:09
  • {{employee.id}} {{employee.name}} {{employee.age}} {{employee.salary}} {{employee.address}} {{employee.bloodgrp}} {{employee.aids}} {{employee.weight}} – Raju Nov 22 '17 at 10:27
  • Thanks bro how to call the url using ajax call in angular js thats the error showing. here in controller its sending response how to get that response in my ANgular js plz bro its very difficult for me to understand Angular js – Guvaliour Nov 22 '17 at 10:34
  • – Guvaliour Nov 22 '17 at 10:35
  • is this ri8 to call – Guvaliour Nov 22 '17 at 10:36
  • Please help me bro and also send link for Angular js U r very genious – Guvaliour Nov 22 '17 at 10:36
  • you get the Response from spring or not ? – Raju Nov 22 '17 at 10:42
  • ya i got the response if i post my url in chrome it display the details like this – Guvaliour Nov 22 '17 at 11:02
  • [{"empId":1,"bloodgrp":"0-ve","empName":"krishnaKumars","weight":78,"aids":"negative","empAge":23,"salary":15000,"empAddress":"madurai"},] – Guvaliour Nov 22 '17 at 11:02
  • https://stackoverflow.com/questions/34991498/how-to-send-multiple-files-along-with-the-form-data-in-angularjs/46708678#46708678-- check this url you will know how to call controller from angularjs – Raju Nov 22 '17 at 11:07
  • bro i dont get any idea please send Angular js code to get this response – Guvaliour Nov 23 '17 at 08:12
  • Bro how to get this value from controller plz send me the code this is my last phase of my project – Guvaliour Nov 27 '17 at 05:31
  • $http.get("./Your controllerName").success(function(response){ $scope.employees = response; – Raju Nov 27 '17 at 05:58