【发布时间】:2014-07-30 02:20:14
【问题描述】:
我是 Angularjs 的新手,我一直在寻找一些示例,我看到了一些与我相关的问题,但给出的解决方案对我不起作用,我真的需要帮助,所以我希望有人给我一个请手。
我有一个 Java RESTApi、一个 bean、一个 DAO 和一个服务类,我正在使用 Hibernate 与 MySQL 中的数据库进行通信。
这是我的豆子:
@Entity
@Table(name = "T_PERSON")
@XmlRootElement(name = "Person")
//@XmlType(propOrder = {"id", "fullName", "age"})
public class Person {
private static Logger LOGGER = LoggerFactory
.getLogger(Person.class);
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int id;
@Column(name = "FULL_NAME")
private String fullName;
@Column(name = "AGE")
private int age;
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(" Id: " + this.id + " Name: " + this.fullName + " Age: " + this.age + " ");
return sb.toString();
}
}
此方法是从数据库中获取所有记录,它在我的 DAO 中:
public List<Person> getAllPersons() {
List<Person> persons = null;
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
persons = session.createQuery("FROM Person").list();
session.getTransaction().commit();
LOGGER.info("Query Ok");
} catch (HibernateException e) {
if (session != null) {
session.getTransaction().rollback();
}
} finally {
if (session != null) {
session.close();
}
}
return persons;
}
这是我的服务获取 JSON 格式列表的方法。
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getAllPersonsInJSON")
public List<Person> getAllPersonsInJSON() {
return personDao.getAllPersons();
}
所有这些都是我的后端。
现在我在 YouTube 上寻找一个教程,您必须在其中创建一个 Web 项目并为 angularjs 使用自定义模板,因此在本教程中您必须修改三个文件,Services.js、Controller.js 和 partial1 .html,这是每个文件的代码。
服务:
var personService = angular.module('myApp.services', [ 'ngResource' ]);
personService.factory('Person', function($resource) {
return $resource('http://localhost:8080/rest/person/getAllPersonsInJSON',
{}, {
findAll : {
method : 'GET',
isArray : true
}
})
});
控制器:
angular.module('myApp.controllers', []).controller('MyCtrl1',
function($scope, Person) {
$scope.allPersons = Person.findAll();
// console.log("Data_: " + JSON.stringify(Person.findAll()));
}).controller('MyCtrl2', [ '$scope', function($scope) {
} ]);
部分1:
<h3>View 1</h3>
<ul>
<li ng-repeat="persons in allPersons">Person</li>
</ul>
所以在这个 html 文件中,它应该显示一个包含数据库上所有记录的列表,但不是,我正在使用 Chrome 网络工具,我得到 200 状态,它没问题,但在控制台我得到此消息:
错误:[$resource:badcfg] 资源配置错误。包含数组但得到对象的预期响应
所以我被困住了,我真的需要帮助,请有人告诉我我做错了什么,因为我已经处理了两天了!
感谢您的帮助、时间和关注。
问候!
【问题讨论】:
标签: java javascript angularjs hibernate