我创建了一个使用Hibernate的Spring MVC应用程序
我的表Person
即将创建,但我从视图中提供的数据未插入。我浏览了很多网站,但我无法指出我的错误。
这是我指定的HomeController 两个请求映射,一个用于根,另一个用于保存方法**
package com.controller;
@Controller
public class MainController {
@Autowired
private PersonService ps;
@RequestMapping("/")
public String home() {
return "index"; }
@RequestMapping(value="/save",method=RequestMethod.POST)
public String save(@ModelAttribute Person p) {
ps.save(p);
return "index";
}
}
这是PersonDAOClass,它包含保存方法
package com.dao;
import com.model.Person;
public interface PersonDAO {
public void save(Person p);
}
以下是DAO的实现:
@Transactional
@Repository
public class PersonDAOImpl implements PersonDAO{
@Autowired
private SessionFactory sessionFactory;
public void save(Person p) {
// TODO Auto-generated method stub
Session s = sessionFactory.openSession();
sessionFactory.getCurrentSession();
// Transactional tx = (Transactional) s.beginTransaction();
s.persist(p);
s.flush();
s.close();
}
}
这是我的服务类
package com.service;
import com.model.Person;
public interface PersonService {
public void save(Person p);
}
这是服务的serviceImpl
package com.service;
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
private PersonDAO pdao;
public void setPersonDAO(PersonDAO personDAO) {
this.pdao = personDAO; }
@Transactional
public void save(Person p) {
// TODO Auto-generated method stub
this.pdao.save(p);
}
}
最后,这是我的index.jsp
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>Hello World!</h2>
<form action="save" >
<!-- id : <input type="text" name="id"> -->
name : <input type="text" name="name"> <input type="submit"
value="submit">
</form>
</body>
</html>