Java Mvc 和 Hibernate
我正在尝试学习 Java、Hibernate 和 MVC 模式。 按照各种在线教程,我设法映射我的数据库,我创建了一些主要方法来测试它并且它有效。此外,我使用 MVC 模式创建了几个页面,并且我还能够在视图中显示一些模拟数据。问题是我无法连接两者。这就是我的
视图 我的视图看起来像这样
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head>
<title>Users</title>
<%@ include file="/WEB-INF/jsp/head.jsp" %>
</head>
<body>
<%@ include file="/WEB-INF/jsp/header.jsp" %>
<img src="images/rss.png" alt="Rss Feed"/>
<%@ include file="/WEB-INF/jsp/menu.jsp" %>
<div class="ContainerIntroText">
<img src="images/usersList.png" class="marginL150px" alt="Add New User"/>
<br/>
<br/>
<div class="usersList">
<div class="listHeaders">
<div class="headerBox">
<strong>FirstName</strong>
</div>
<div class="headerBox">
<strong>LastName</strong>
</div>
<div class="headerBox">
<strong>Username</strong>
</div>
<div class="headerAction">
<strong>Edit</strong>
</div>
<div class="headerAction">
<strong>Delete</strong>
</div>
</div>
<br><br>
<c:forEach items="${users}" var="user">
<div class="listElement">
<c:out value="${user.firstName}"/>
</div>
<div class="listElement">
<c:out value="${user.lastName}"/>
</div>
<div class="listElement">
<c:out value="${user.username}"/>
</div>
<div class="listElementAction">
<input type="button" name="Edit" title="Edit" value="Edit"/>
</div>
<div class="listElementAction">
<input type="image" src="images/delete.png" name="image" alt="Delete" >
</div>
<br />
</c:forEach>
</div>
</div>
<a id="addUser" href="addUser.htm" title="Click to add a new user"> </a>
</body>
</html>
我的控制器
public class UsersController implements Controller {
private UserServiceImplementation userServiceImplementation;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelAndView modelAndView = new ModelAndView("users");
List<User> users = this.userServiceImplementation.get();
modelAndView.addObject("users", users);
return modelAndView;
}
public UserServiceImplementation getUserServiceImplementation() {
return userServiceImplementation;
}
public void setUserServiceImplementation(UserServiceImplementation userServiceImplementation) {
this.userServiceImplementation = userServiceImplementation;
}
}
我的servlet定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean name="/home.htm" class="com.rssFeed.mvc.HomeController"/>
<bean name="/rssFeeds.htm" class="com.rssFeed.mvc.RssFeedsController"/>
<bean name="/addUser.htm" class="com.rssFeed.mvc.AddUserController"/>
<bean name="/users.htm" class="com.rssFeed.mvc.UsersController">
<property name="userServiceImplementation" ref="userServiceImplementation"/>
</bean>
<bean id="userServiceImplementation" class="com.rssFeed.ServiceImplementation.UserServiceImplementation">
<property name="users">
<list>
<ref bean="user1"/>
<ref bean="user2"/>
</list>
</property>
</bean>
<bean id="user1" class="com.rssFeed.domain.User">
<property name="firstName" value="firstName1"/>
<property name="lastName" value="lastName1"/>
<property name="username" value="username1"/>
<property name="password" value="password1"/>
</bean>
<bean id="user2" class="com.rssFeed.domain.User">
<property name="firstName" value="firstName2"/>
<property name="lastName" value="lastName2"/>
<property name="username" value="username2"/>
<property name="password" value="password2"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
最后是这个类来访问数据库 数据库
public class HibernateUserDao extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
getHibernateTemplate().saveOrUpdate(user);
}
public List<User> get() {
User user1 = new User();
user1.setFirstName("FirstName");
user1.setLastName("LastName");
user1.setUsername("Username");
user1.setPassword("Password");
List<User> users = new LinkedList<User>();
users.add(user1);
return users;
}
public User get(int id) {
throw new UnsupportedOperationException("Not supported yet.");
}
public User get(String username) {
return null;
}
}
连接发生在这个文件中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost/rss"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/rssFeed/domain/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="com.rssFeed.dao.hibernate.HibernateUserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
并且 UserServiceImplementation 类看起来像这样
public class UserServiceImplementation implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User user) throws UserAlreadyExistsException {
if (userDao.get(user.getUsername()) != null) { //user already exists
throw new UserAlreadyExistsException();
}
else{
userDao.addUser(user);
}
}
private static List<User> users;
static {
User user1 = new User();
user1.setFirstName("FirstName");
user1.setLastName("LastName");
user1.setUsername("my username");
user1.setPassword("my Password");
users = new LinkedList<User>();
users.add(user1);
}
public User createUser(User u) {
User user = new User();
user.setId(users.size() + 1);
user.setFirstName(u.getFirstName());
user.setLastName(u.getLastName());
user.setUsername(u.getUsername());
user.setPassword(u.getPassword());
users.add(user);
return user;
}
public List<User> get() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public User get(int id) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
你能帮我解决这个问题吗 我花了过去4天和这个问题上的夜晚没有任何成功
谢谢
i am trying to learn Java, Hibernate and the MVC pattern.
Following various tutorial online i managed to map my database, i have created few Main methods to test it and it works. Furthermore i have created few pages using the MVC patter and i am able to display some mock data as well in a view. the problem is i can not connect the two. this is what i have
My view Looks like this
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head>
<title>Users</title>
<%@ include file="/WEB-INF/jsp/head.jsp" %>
</head>
<body>
<%@ include file="/WEB-INF/jsp/header.jsp" %>
<img src="images/rss.png" alt="Rss Feed"/>
<%@ include file="/WEB-INF/jsp/menu.jsp" %>
<div class="ContainerIntroText">
<img src="images/usersList.png" class="marginL150px" alt="Add New User"/>
<br/>
<br/>
<div class="usersList">
<div class="listHeaders">
<div class="headerBox">
<strong>FirstName</strong>
</div>
<div class="headerBox">
<strong>LastName</strong>
</div>
<div class="headerBox">
<strong>Username</strong>
</div>
<div class="headerAction">
<strong>Edit</strong>
</div>
<div class="headerAction">
<strong>Delete</strong>
</div>
</div>
<br><br>
<c:forEach items="${users}" var="user">
<div class="listElement">
<c:out value="${user.firstName}"/>
</div>
<div class="listElement">
<c:out value="${user.lastName}"/>
</div>
<div class="listElement">
<c:out value="${user.username}"/>
</div>
<div class="listElementAction">
<input type="button" name="Edit" title="Edit" value="Edit"/>
</div>
<div class="listElementAction">
<input type="image" src="images/delete.png" name="image" alt="Delete" >
</div>
<br />
</c:forEach>
</div>
</div>
<a id="addUser" href="addUser.htm" title="Click to add a new user"> </a>
</body>
</html>
My controller
public class UsersController implements Controller {
private UserServiceImplementation userServiceImplementation;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelAndView modelAndView = new ModelAndView("users");
List<User> users = this.userServiceImplementation.get();
modelAndView.addObject("users", users);
return modelAndView;
}
public UserServiceImplementation getUserServiceImplementation() {
return userServiceImplementation;
}
public void setUserServiceImplementation(UserServiceImplementation userServiceImplementation) {
this.userServiceImplementation = userServiceImplementation;
}
}
My servelet definitions
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean name="/home.htm" class="com.rssFeed.mvc.HomeController"/>
<bean name="/rssFeeds.htm" class="com.rssFeed.mvc.RssFeedsController"/>
<bean name="/addUser.htm" class="com.rssFeed.mvc.AddUserController"/>
<bean name="/users.htm" class="com.rssFeed.mvc.UsersController">
<property name="userServiceImplementation" ref="userServiceImplementation"/>
</bean>
<bean id="userServiceImplementation" class="com.rssFeed.ServiceImplementation.UserServiceImplementation">
<property name="users">
<list>
<ref bean="user1"/>
<ref bean="user2"/>
</list>
</property>
</bean>
<bean id="user1" class="com.rssFeed.domain.User">
<property name="firstName" value="firstName1"/>
<property name="lastName" value="lastName1"/>
<property name="username" value="username1"/>
<property name="password" value="password1"/>
</bean>
<bean id="user2" class="com.rssFeed.domain.User">
<property name="firstName" value="firstName2"/>
<property name="lastName" value="lastName2"/>
<property name="username" value="username2"/>
<property name="password" value="password2"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
and finally this class to access the database
public class HibernateUserDao extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
getHibernateTemplate().saveOrUpdate(user);
}
public List<User> get() {
User user1 = new User();
user1.setFirstName("FirstName");
user1.setLastName("LastName");
user1.setUsername("Username");
user1.setPassword("Password");
List<User> users = new LinkedList<User>();
users.add(user1);
return users;
}
public User get(int id) {
throw new UnsupportedOperationException("Not supported yet.");
}
public User get(String username) {
return null;
}
}
the database connection occurs in this file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost/rss"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/rssFeed/domain/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="com.rssFeed.dao.hibernate.HibernateUserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
and UserServiceImplementation Class looks like this
public class UserServiceImplementation implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User user) throws UserAlreadyExistsException {
if (userDao.get(user.getUsername()) != null) { //user already exists
throw new UserAlreadyExistsException();
}
else{
userDao.addUser(user);
}
}
private static List<User> users;
static {
User user1 = new User();
user1.setFirstName("FirstName");
user1.setLastName("LastName");
user1.setUsername("my username");
user1.setPassword("my Password");
users = new LinkedList<User>();
users.add(user1);
}
public User createUser(User u) {
User user = new User();
user.setId(users.size() + 1);
user.setFirstName(u.getFirstName());
user.setLastName(u.getLastName());
user.setUsername(u.getUsername());
user.setPassword(u.getPassword());
users.add(user);
return user;
}
public List<User> get() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public User get(int id) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Could you help me to solve this problem i spent the last 4 days and nights on this issue without any success
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一周夜以继日地研究这个问题,我终于解决了这个问题。我不确定我提出的解决方案是否正确,但它有效,并且在这个学习阶段对我来说足够好。
这就是我所做的:
我修改了 bean userServiceImplementation,如下
所示,现在引用 userDao bean,
控制器保持不变,而 HibernateUserDao 更改如下,
我希望这会对你们有所帮助!
After a week working on this problem day and night i managed to solve the issue. I am not sure if the solution i am proposing is the right one but it works and is good enough for me at this learning stage.
this is what i have done:
i modified the bean userServiceImplementation as follow
it now reference the userDao bean
the controller remain the same while the HibernateUserDao is changed as follow
I hope this will help you guys!!
如果您收到任何有关错误的信息,将会有所帮助。
令我震惊的是你的 html - 你没有表单也没有输入字段?也许你可以从那里开始?
Would've help some information about the error if you get any.
Something strikes me right away is your html - you have no form and no input fields? Perhaps you can start from there?