我是春天的新手,正在通过春天的示例程序....我的程序如下
用户.java
package project2;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User {
private Long id;
private String name;
private String password;
private String gender;
private String country;
@Id
@GeneratedValue
@Column(name="USER_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="USER_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="USER_PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="USER_GENDER")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Column(name="USER_COUNTRY")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
用户DAO.java
package project2;
import java.util.List;
public interface UserDAO {
public void saveUser(User user) ;
public List<User> listUser() ;
public void deleteUser(User user) ;
}
用户控制器2.java
package project2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
public class UserController2 extends MultiActionController {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@RequestMapping(params = "add", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.saveUser(user);
return new ModelAndView("redirect:list.htm");
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
@Transactional
public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.deleteUser(user);
return new ModelAndView("redirect:list.htm");
}
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("userList", userDAO.listUser());
modelMap.addAttribute("user", new User());
return new ModelAndView("userForm", modelMap);
}
}
UserDAOImpl.java
package project2;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class UserDAOImpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public void saveUser(User user) {
hibernateTemplate.saveOrUpdate(user);
}
@Override
@SuppressWarnings("unchecked")
public List<User> listUser() {
return hibernateTemplate.find("from User");
}
@Override
public void deleteUser(User user) {
hibernateTemplate.delete(user);
}
}
UserController2-servlet.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/indi"/>
<property name="username" value="admin"/>
<property name="password" value=""/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>Spring.project2.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="project2.UserDAOImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean name="/user/*.htm" class="project2.UserController2" >
<property name="userDAO" ref="myUserDAO" />
</bean>
<bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="userForm" />
</beans>
而userForm.jsp文件如下
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center> WELCOME TO CUSTOMER ACCESS SITE.PLEASE ENTER THE FOLLOWING
INFORMATION</center>
<form:form method="POST" action="add.htm" commandName="user">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="password">Name</form:label></td>
<td><form:input path="password" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="country">Name</form:label></td>
<td><form:input path="country" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
而web.xml文件如下...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>UserController2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UserController2</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
现在,当我运行此程序时,我的 UserController2-servlet.xml 文件中出现嵌套异常,如下所示
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/UserController2-servlet.xml]:
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'/user/*.htm' defined in ServletContext resource [/WEB-INF/UserController2-
servlet.xml]: Cannot resolve reference to bean 'myUserDAO' while setting bean property
'userDAO'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'myUserDAO' defined in ServletContext resource [/WEB-INF/UserController2-
servlet.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean
property 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'mySessionFactory' defined in ServletContext resource [/WEB-INF/UserController2-
servlet.xml]: Initialization of bean failed; nested exception is
org.springframework.beans.TypeMismatchException: Failed to convert property value of
type 'java.util.ArrayList' to required type 'java.lang.Class[]' for property
'annotatedClasses'; nested exception is java.lang.IllegalArgumentException: Cannot
find class [Spring.project2.User]
Caused by: java.lang.ClassNotFoundException: Spring.project2.User
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:290)
我已将这些文件保存在 src 中的文件夹 project2 下,项目名称为 Spring。我已将所有 jar 文件包含在 lib 中,并且所有 jar 都具有相同的版本。我不知道我哪里错了。