如何使用 $http 服务将 Angular JS 与 spring mvc 连接起来以从控制器获取数据到 jsp
How to connect Angular JS with spring mvc using $http service to get data from controller to jsp
My Controller:
如何从 spring MVC 获取值到 Angular js 以显示在 jsp 页面中。
下面我发布了我的 spring MVC 和 Angular js 配置 code.Please 一些帮助
如何从 Spring controller.how 中获取值以获取我们的 url 路径
控制器。
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:
这是我的 angular js 页面,我尝试从 Spring mvc 控制器获取数据,但失败了。请解释如何从 spring MVC .im 获取数据到 angular js 和 spring mvc 这是我的任务,我更新鲜。
<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>
不要使用任何模型只是在响应中发送数据并转换 json
中的 DTO 对象列表
@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());
}
在 pom.xml 中用于 jsonObject
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
My Controller:
如何从 spring MVC 获取值到 Angular js 以显示在 jsp 页面中。 下面我发布了我的 spring MVC 和 Angular js 配置 code.Please 一些帮助 如何从 Spring controller.how 中获取值以获取我们的 url 路径 控制器。
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:
这是我的 angular js 页面,我尝试从 Spring mvc 控制器获取数据,但失败了。请解释如何从 spring MVC .im 获取数据到 angular js 和 spring mvc 这是我的任务,我更新鲜。
<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>
不要使用任何模型只是在响应中发送数据并转换 json
中的 DTO 对象列表 @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());
}
在 pom.xml 中用于 jsonObject
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>