我想學習Java,Hibernate和MVC模式。 繼各種教程在線我設法映射我的數據庫,我創建了幾個主要的方法來測試它,它的工作原理。此外,我已經創建了幾個使用MVC模式的頁面,並且我能夠在視圖中顯示一些模擬數據。問題是我無法連接兩個。這是我所Java 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;
}
}
我的小服務程序定義
<?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天的夜晚在這個問題上沒有任何成功
謝謝
什麼不工作?你能向我們展示UserServiceImplementation的代碼嗎? – oedo 2010-05-28 23:00:52
抱歉,我忘了它,我想用數據庫中的數據替換servelet定義文件中定義的模擬數據 – GigaPr 2010-05-28 23:04:58
@GigaPr發佈正確的答案,以便別人可以解決他的問題 – ant 2010-05-29 10:41:39