1. web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2. struts-config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans /> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/student" type="org.springframework.web.struts.DelegatingActionProxy" parameter="method" > <forward name="studentList" path="/StudentList.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> <!-- 利用plug-in将Action的实例化工作转移到spring去实现 --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> </plug-in> </struts-config>
3. applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/ps.properties"/> </bean> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="datasource" /> </property> <property name="mappingResources"> <list> <value>com/beans/HibernateStudent.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean> <bean id="studentDao" class="com.dao.StudentDaoImpl"> <property name="sessionFactory"> <ref bean="sessionfactory"/> </property> </bean> <bean id="studentService" class="com.bo.StudentServiceImpl"> <property name="studentDao"> <ref bean="studentDao"/> </property> </bean> <bean name="/student" class="com.action.StudentAction"> <property name="studentService"> <ref bean="studentService"/> </property> </bean> </beans>
4. ps.properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/tarena jdbc.username=root jdbc.password= #Hibernate Setting hibernate.show_sql=true hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.jdbc.fetch_size=50 hibernate.jdbc.batch_size=30 hibernate.use_sql_comments=false hibernate.cache.use_second_level_cache=false hibernate.cache.use_query_cache=false
5.StudentAction.java /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.beans.Student; import com.bo.IStudentService; public class StudentAction extends DispatchAction{ private IStudentService studentService; public IStudentService getStudentService() { return studentService; } public void setStudentService(IStudentService studentService) { this.studentService = studentService; } public ActionForward findStudents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub List<Student> students = studentService.findAllStudents(); request.setAttribute("students",students); return mapping.findForward("studentList"); } }
6. StudentDaoImpl.java package com.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.beans.Student; public class StudentDaoImpl extends HibernateDaoSupport implements IStudentDao { public List<Student> findAllStudents() { return this.getHibernateTemplate().find("from student"); } }
7. index.jsp <jsp:forward page="student.do?method=findStudents"></jsp:forward>
8.HibernateStudent.hbm.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.beans.Student" table="student"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="name"> <column name="name"/> </property> <property name="password"> <column name="password"/> </property> </class> </hibernate-mapping>
9. Student.java package com.beans; public class Student { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }