搭建SSH三大框架WEB项目过程(Struts2.3+Hibernate4.3+Spring4.1)

来源:互联网 发布:pywin32 知乎 编辑:程序博客网 时间:2024/05/16 05:06

搭建SSH三大框架WEB项目过程(Struts2.3+Hibernate4.3+Spring4.1)

我以我做的一个例子来说明框架的搭建过程 ^V^!


项目结构如图:

action:存放Action类,也就是控制类

daoDAO数据库操作

poPOJO类,也就是持久化类

service:存放Service


dao类在Service类里调用,然后Service类再到action类里调用



搭建过程

我们先要准备jar价包,这个可以去官网下载

下面是我准备的开发jar价包

然后我为了提高安全性,我将所有的JSP页面放在了WEB-INF下面


然后配置SSH的配置文件

Spring的配置文件代码:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:p="http://www.springframework.org/schema/p"  
    5.     xmlns:context="http://www.springframework.org/schema/context"  
    6.     xmlns:tx="http://www.springframework.org/schema/tx"  
    7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
    8.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
    9.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">  
    10. <!-- Spring框架配置文件 -->  
    11.     <!-- 属性注入配置 -->  
    12.     <context:annotation-config/>  
    13.     <!-- 实现数据库配置 -->  
    14.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
    15.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
    16.         <property name="url" value="jdbc:mysql://localhost:3306/db_sgdata?useUnicode=true&characterEncoding=UTF-8"></property>  
    17.         <property name="username" value="root"></property>  
    18.         <property name="password" value="111"></property>  
    19.         <property name="maxActive" value="100"></property>  
    20.         <property name="maxIdle" value="60"></property>  
    21.         <property name="maxWait" value="10000"></property>  
    22.     </bean>     
    23.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
    24.         <property name="dataSource" ref="dataSource"></property>  
    25.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
    26.     </bean>  
    27.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">  
    28.         <property name="sessionFactory" ref="sessionFactory"></property>  
    29.     </bean>  
    30.     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    31.         <property name="sessionFactory" ref="sessionFactory"></property>  
    32.     </bean>  
    33.       
    34.     <!-- 开启Spring框架的事务管理 ,开启之后@Transaction就可以用了 -->  
    35.     <tx:annotation-driven transaction-manager="txManager"/>  
    36.       
    37.     <!-- 实现教师信息管理需要配置的Bean -->  
    38.     <bean id="teacherDao" class="com.sgdata.dao.impl.TeacherDaoImpl">  
    39.         <property name="sessionFactory" ref="sessionFactory"></property>  
    40.     </bean>  
    41.     <bean id="teacherService" class="com.sgdata.service.impl.TeacherServiceBean">  
    42.     </bean>  
    43.         <!--scope默认采用的是单例模式,scope="prototype" 可以保证 当有请求的时候都创建一个Action对象,保证Struts的Action线程安全 -->     
    44.     <bean id="teacherAction" class="com.sgdata.action.TeacherInfoManagerAction" scope="prototype"></bean>  
    45.     <bean id="loginCheckAction" class="com.sgdata.action.LoginCheckAction" scope="prototype"></bean>  
    46.       
    47.     <!-- 实现学生信息管理需要配置的Bean -->  
    48.     <bean id="studentDao" class="com.sgdata.dao.impl.StudentDaoImpl">  
    49.         <property name="sessionFactory" ref="sessionFactory"></property>  
    50.     </bean>  
    51.     <bean id="studentService" class="com.sgdata.service.impl.StudentServiceBean"></bean>  
    52.     <bean id="studentAction" class="com.sgdata.action.StudentInfoManagerAction" scope="prototype"></bean>  
    53.       
    54.     <!-- 实现课程信息管理需要配置的Bean -->  
    55.     <bean id="courseDao" class="com.sgdata.dao.impl.CourseDaoImpl">  
    56.         <property name="sessionFactory" ref="sessionFactory"></property>  
    57.     </bean>  
    58.     <bean id="courseService" class="com.sgdata.service.impl.CourseServiceBean"></bean>  
    59.     <bean id="courseAction" class="com.sgdata.action.CourseInfoManagerAction" scope="prototype"></bean>  
    60.       
    61.     <!-- 实现比赛信息管理需要配置的Bean -->  
    62.     <bean id="matchDao" class="com.sgdata.dao.impl.MatchDaoImpl">  
    63.         <property name="sessionFactory" ref="sessionFactory"></property>  
    64.     </bean>  
    65.     <bean id="matchService" class="com.sgdata.service.impl.MatchServiceBean"></bean>  
    66.     <bean id="matchAction" class="com.sgdata.action.MatchInfoManagerAction" scope="prototype"></bean>  
    67.       
    68. </beans>
  1. <?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- Spring框架配置文件 --> <!-- 属性注入配置 --> <context:annotation-config/> <!-- 实现数据库配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/db_sgdata?useUnicode=true&characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="111"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="60"></property> <property name="maxWait" value="10000"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 开启Spring框架的事务管理 ,开启之后@Transaction就可以用了 --> <tx:annotation-driven transaction-manager="txManager"/> <!-- 实现教师信息管理需要配置的Bean --> <bean id="teacherDao" class="com.sgdata.dao.impl.TeacherDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="teacherService" class="com.sgdata.service.impl.TeacherServiceBean"> </bean> <!--scope默认采用的是单例模式,scope="prototype" 可以保证 当有请求的时候都创建一个Action对象,保证Struts的Action线程安全 --> <bean id="teacherAction" class="com.sgdata.action.TeacherInfoManagerAction" scope="prototype"></bean> <bean id="loginCheckAction" class="com.sgdata.action.LoginCheckAction" scope="prototype"></bean> <!-- 实现学生信息管理需要配置的Bean --> <bean id="studentDao" class="com.sgdata.dao.impl.StudentDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="studentService" class="com.sgdata.service.impl.StudentServiceBean"></bean> <bean id="studentAction" class="com.sgdata.action.StudentInfoManagerAction" scope="prototype"></bean> <!-- 实现课程信息管理需要配置的Bean --> <bean id="courseDao" class="com.sgdata.dao.impl.CourseDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="courseService" class="com.sgdata.service.impl.CourseServiceBean"></bean> <bean id="courseAction" class="com.sgdata.action.CourseInfoManagerAction" scope="prototype"></bean> <!-- 实现比赛信息管理需要配置的Bean --> <bean id="matchDao" class="com.sgdata.dao.impl.MatchDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="matchService" class="com.sgdata.service.impl.MatchServiceBean"></bean> <bean id="matchAction" class="com.sgdata.action.MatchInfoManagerAction" scope="prototype"></bean> </beans>


Struts2的配置文件代码:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
    5. <!-- Struts2框架配置文件 -->  
    6. <struts>  
    7.     <!-- 配置struts2可以受理的请求扩展名 -->  
    8.     <constant name="struts.action.extension" value="action,do,"></constant>  
    9.     <!-- struts2的package对应于项目的模块 -->  
    10.     <package name="action" extends="struts-default" namespace="/">  
    11.         <!-- 配置action -->  
    12.           
    13.         <!-- 登录验证的Action -->  
    14.         <action name="loginAction" class="loginCheckAction">  
    15.             <result name="success">/WEB-INF/page/admin/index.jsp</result>  
    16.             <result name="input">/WEB-INF/page/admin/login.jsp</result>  
    17.         </action>  
    18.           
    19.         <!--   
    20.             SSH项目WEB-INF下面的页面跳转要通过Servlet来实现,这样确实是麻烦了点,  
    21.             不过安全性就提高上去了,因为放在WEB-INF下面的JSP页面,是不可以直接访问的  
    22.         -->  
    23.          <action name="indexAction">    
    24.               <result>/WEB-INF/page/admin/index.jsp</result>    
    25.          </action>    
    26.          <action name="gotoLoginAction">  
    27.             <result>/WEB-INF/page/admin/login.jsp</result>  
    28.          </action>  
    29.           
    30.         <!-- 学生信息管理的Action -->  
    31.         <action name="getAllStuInfoAction" class="studentAction" method="getAllInfo">  
    32.                 <result name="success">/WEB-INF/page/admin/student/studentInfoManager.jsp</result>  
    33.         </action>  
    34.         <action name="getStuInfoByIdAction" class="studentAction" method="getInfoById">  
    35.             <result name="success">/WEB-INF/page/admin/student/studentInfoDetail.jsp</result>  
    36.         </action>  
    37.         <action name="getLearnScoresAction" class="studentAction" method="getLearnScoreById">  
    38.             <result name="success">/WEB-INF/page/admin/student/studentLearnScores.jsp</result>  
    39.         </action>  
    40.         <action name="getMatchScoresAction" class="studentAction" method="getMatchScoreById">  
    41.             <result name="success">/WEB-INF/page/admin/student/studentMatchScores.jsp</result>  
    42.         </action>  
    43.           
    44.         <!-- 教师信息管理的Action -->  
    45.         <action name="getAllTeaInfoAction" class="teacherAction" method="getAllInfo">  
    46.                 <result name="success">/WEB-INF/page/admin/teacher/teacherInfoManager.jsp</result>  
    47.         </action>  
    48.         <action name="getTeachingInfoAction" class="teacherAction" method="getTeachingInfoById">  
    49.                 <result name="success">/WEB-INF/page/admin/teacher/teacherTeaching.jsp</result>  
    50.         </action>  
    51.         <action name="getMatchGuideInfoAction" class="teacherAction" method="getMatchGuideInfoById">  
    52.                 <result name="success">/WEB-INF/page/admin/teacher/teacherMatchGuide.jsp</result>  
    53.         </action>  
    54.         <action name="getCourseStudentsInfoAction" class="teacherAction" method="getCourseStudentsInfoById">  
    55.                 <result name="success">/WEB-INF/page/admin/teacher/teacherCourseStusInfo.jsp</result>  
    56.         </action>  
    57.         <action name="getMatchStudentsInfoAction" class="teacherAction" method="getMatchStudentsInfoById">  
    58.                 <result name="success">/WEB-INF/page/admin/teacher/teacherMatchStusInfo.jsp</result>  
    59.         </action>  
    60.           
    61.         <!-- 课程管理的Action -->  
    62.         <action name="getAllCourseInfoAction" class="courseAction" method="getAllInfo">  
    63.             <result name="success">/WEB-INF/page/admin/course/courseManager.jsp</result>  
    64.         </action>  
    65.         <action name="getTeachersInfoAction" class="courseAction" method="getTeachersInfoById">  
    66.             <result name="success">/WEB-INF/page/admin/course/courseTeachersInfo.jsp</result>  
    67.         </action>  
    68.           
    69.         <!-- 比赛信息管理的Action -->  
    70.         <action name="getAllMatchInfoAction" class="matchAction" method="getAllInfo">  
    71.             <result name="success">/WEB-INF/page/admin/match/matchInfoManager.jsp</result>  
    72.         </action>  
    73.         <action name="getStudentsInfoAction" class="matchAction" method="getStudentsInfoById">  
    74.             <result name="success">/WEB-INF/page/admin/match/matchStudentsInfo.jsp</result>  
    75.         </action>  
    76.           
    77.     </package>  
    78.       
    79. </struts>
  1. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- Struts2框架配置文件 --> <struts> <!-- 配置struts2可以受理的请求扩展名 --> <constant name="struts.action.extension" value="action,do,"></constant>     <!-- struts2的package对应于项目的模块 -->     <package name="action" extends="struts-default" namespace="/">      <!-- 配置action -->           <!-- 登录验证的Action -->      <action name="loginAction">      <result name="success">/WEB-INF/page/admin/index.jsp</result>      <result name="input">/WEB-INF/page/admin/login.jsp</result>      </action>           <!--  SSH项目WEB-INF下面的页面跳转要通过Servlet来实现,这样确实是麻烦了点, 不过安全性就提高上去了,因为放在WEB-INF下面的JSP页面,是不可以直接访问的   -->       <action name="indexAction">              <result>/WEB-INF/page/admin/index.jsp</result>         </action>         <action name="gotoLoginAction">        <result>/WEB-INF/page/admin/login.jsp</result>       </action>           <!-- 学生信息管理的Action -->      <action name="getAllStuInfoAction" method="getAllInfo">      <result name="success">/WEB-INF/page/admin/student/studentInfoManager.jsp</result>      </action>      <action name="getStuInfoByIdAction" method="getInfoById">      <result name="success">/WEB-INF/page/admin/student/studentInfoDetail.jsp</result>      </action>      <action name="getLearnScoresAction" method="getLearnScoreById">      <result name="success">/WEB-INF/page/admin/student/studentLearnScores.jsp</result>      </action>      <action name="getMatchScoresAction" method="getMatchScoreById">      <result name="success">/WEB-INF/page/admin/student/studentMatchScores.jsp</result>      </action>           <!-- 教师信息管理的Action -->      <action name="getAllTeaInfoAction" method="getAllInfo">      <result name="success">/WEB-INF/page/admin/teacher/teacherInfoManager.jsp</result>      </action>      <action name="getTeachingInfoAction" method="getTeachingInfoById">      <result name="success">/WEB-INF/page/admin/teacher/teacherTeaching.jsp</result>      </action>      <action name="getMatchGuideInfoAction" method="getMatchGuideInfoById">      <result name="success">/WEB-INF/page/admin/teacher/teacherMatchGuide.jsp</result>      </action>      <action name="getCourseStudentsInfoAction" method="getCourseStudentsInfoById">      <result name="success">/WEB-INF/page/admin/teacher/teacherCourseStusInfo.jsp</result>      </action>      <action name="getMatchStudentsInfoAction" method="getMatchStudentsInfoById">      <result name="success">/WEB-INF/page/admin/teacher/teacherMatchStusInfo.jsp</result>      </action>           <!-- 课程管理的Action -->      <action name="getAllCourseInfoAction" method="getAllInfo">      <result name="success">/WEB-INF/page/admin/course/courseManager.jsp</result>      </action>      <action name="getTeachersInfoAction" method="getTeachersInfoById">      <result name="success">/WEB-INF/page/admin/course/courseTeachersInfo.jsp</result>      </action>           <!-- 比赛信息管理的Action -->      <action name="getAllMatchInfoAction" method="getAllInfo">      <result name="success">/WEB-INF/page/admin/match/matchInfoManager.jsp</result>      </action>      <action name="getStudentsInfoAction" method="getStudentsInfoById">      <result name="success">/WEB-INF/page/admin/match/matchStudentsInfo.jsp</result>      </action>          </package>      </struts>



 

 

 

Hibernate的配置文件代码:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. <?xml version='1.0' encoding='UTF-8'?>  
    2. <!DOCTYPE hibernate-configuration PUBLIC  
    3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    4.           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
    5. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
    6. <hibernate-configuration>  
    7. <!-- Hibernate框架配置文件 -->  
    8. <session-factory>  
    9.     <!-- 配置sql语句可以打印在控制台 -->  
    10.     <property name="show_sql">true</property>  
    11.     <!--创建SessionFactory对象时自动创建数据表  -->  
    12.     <property name="hbm2ddl.auto">update</property>  
    13.     <!-- 配置映射文件 -->  
    14.     <mapping resource="com/sgdata/po/Course.hbm.xml"/>  
    15.     <mapping resource="com/sgdata/po/Deptment.hbm.xml"/>  
    16.     <mapping resource="com/sgdata/po/Match.hbm.xml"/>  
    17.     <mapping resource="com/sgdata/po/Student.hbm.xml"/>  
    18.     <mapping resource="com/sgdata/po/StudentCourse.hbm.xml"/>  
    19.     <mapping resource="com/sgdata/po/StudentMatch.hbm.xml"/>  
    20.     <mapping resource="com/sgdata/po/Teacher.hbm.xml"/>  
    21.     <mapping resource="com/sgdata/po/TeacherCourse.hbm.xml"/>  
    22.     <mapping resource="com/sgdata/po/TeacherMatch.hbm.xml"/>  
    23. </session-factory>  
    24.   
    25. </hibernate-configuration>
  1. <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools.                   --> <hibernate-configuration> <!-- Hibernate框架配置文件 --> <session-factory> <!-- 配置sql语句可以打印在控制台 --> <property name="show_sql">true</property> <!--创建SessionFactory对象时自动创建数据表  --> <property name="hbm2ddl.auto">update</property> <!-- 配置映射文件 --> <mapping resource="com/sgdata/po/Course.hbm.xml"/> <mapping resource="com/sgdata/po/Deptment.hbm.xml"/> <mapping resource="com/sgdata/po/Match.hbm.xml"/> <mapping resource="com/sgdata/po/Student.hbm.xml"/> <mapping resource="com/sgdata/po/StudentCourse.hbm.xml"/> <mapping resource="com/sgdata/po/StudentMatch.hbm.xml"/> <mapping resource="com/sgdata/po/Teacher.hbm.xml"/> <mapping resource="com/sgdata/po/TeacherCourse.hbm.xml"/> <mapping resource="com/sgdata/po/TeacherMatch.hbm.xml"/> </session-factory> </hibernate-configuration>



前面那些配置文件有包含其它的,这个要根据自己的项目需要去改的^V^

下面以学生信息管理的实现过程进行说明,只说明这个例子哈!


创建POJO实体类:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. import java.util.Date;  
    2. import java.util.HashSet;  
    3. import java.util.Set;  
    4.   
    5. /** 
    6.  *  
    7.  * 学生信息的实体类 
    8.  * @author Nicky 
    9.  * 
    10.  */  
    11. public class Student {  
    12.       
    13.     /* 
    14.      * 学号 
    15.      */  
    16.     private String stuID;  
    17.       
    18.     /* 
    19.      * 班级 
    20.      */  
    21.     private String stuName;  
    22.       
    23.     /* 
    24.      * 性别 
    25.      */  
    26.     private String stuSex;  
    27.       
    28.     /* 
    29.      * 出生年日 
    30.      */  
    31.     private Date stuBirth;  
    32.       
    33.     /* 
    34.      * 电话 
    35.      */  
    36.     private String stuTel;  
    37.       
    38.     /* 
    39.      * 邮箱 
    40.      */  
    41.     private String stuEmail;  
    42.       
    43.     /* 
    44.      * 专业 
    45.      */  
    46.     private String dept;  
    47.       
    48.     /* 
    49.      * 身份证 
    50.      */  
    51.     private String stuIDCard;  
    52.       
    53.     /* 
    54.      * 班级 
    55.      */  
    56.     private String className;  
    57.       
    58.     /* 
    59.      * 登录密码 
    60.      */  
    61.     private String password;  
    62.       
    63.     /* 
    64.      * 是否是管理员的标志  1表示是,0表示不是 
    65.      */  
    66.     private String isManager;  
    67.       
    68.     public String getStuID() {  
    69.         return stuID;  
    70.     }  
    71.   
    72.     public void setStuID(String stuID) {  
    73.         this.stuID = stuID;  
    74.     }  
    75.   
    76.     public String getStuName() {  
    77.         return stuName;  
    78.     }  
    79.   
    80.     public void setStuName(String stuName) {  
    81.         this.stuName = stuName;  
    82.     }  
    83.   
    84.     public String getStuSex() {  
    85.         return stuSex;  
    86.     }  
    87.   
    88.     public void setStuSex(String stuSex) {  
    89.         this.stuSex = stuSex;  
    90.     }  
    91.   
    92.     public Date getStuBirth() {  
    93.         return stuBirth;  
    94.     }  
    95.   
    96.     public void setStuBirth(Date stuBirth) {  
    97.         this.stuBirth = stuBirth;  
    98.     }  
    99.   
    100.     public String getStuTel() {  
    101.         return stuTel;  
    102.     }  
    103.   
    104.     public void setStuTel(String stuTel) {  
    105.         this.stuTel = stuTel;  
    106.     }  
    107.   
    108.     public String getStuEmail() {  
    109.         return stuEmail;  
    110.     }  
    111.   
    112.     public void setStuEmail(String stuEmail) {  
    113.         this.stuEmail = stuEmail;  
    114.     }  
    115.   
    116.     public String getDept() {  
    117.         return dept;  
    118.     }  
    119.   
    120.     public void setDept(String dept) {  
    121.         this.dept = dept;  
    122.     }  
    123.   
    124.     public String getStuIDCard() {  
    125.         return stuIDCard;  
    126.     }  
    127.   
    128.     public void setStuIDCard(String stuIDCard) {  
    129.         this.stuIDCard = stuIDCard;  
    130.     }  
    131.   
    132.     public String getClassName() {  
    133.         return className;  
    134.     }  
    135.   
    136.     public void setClassName(String className) {  
    137.         this.className = className;  
    138.     }  
    139.   
    140.     public String getPassword() {  
    141.         return password;  
    142.     }  
    143.   
    144.     public void setPassword(String password) {  
    145.         this.password = password;  
    146.     }  
    147.   
    148.     public String getIsManager() {  
    149.         return isManager;  
    150.     }  
    151.   
    152.     public void setIsManager(String isManager) {  
    153.         this.isManager = isManager;  
    154.     }  
    155.   
    156.           
    157. }
  1. import java.util.Date; import java.util.HashSet; import java.util.Set; /**  *   * 学生信息的实体类  * @author Nicky  *  */ public class Student { /*  * 学号  */ private String stuID; /*  * 班级  */ private String stuName; /*  * 性别  */ private String stuSex; /*  * 出生年日  */ private Date stuBirth; /*  * 电话  */ private String stuTel; /*  * 邮箱  */ private String stuEmail; /*  * 专业  */ private String dept; /*  * 身份证  */ private String stuIDCard; /*  * 班级  */ private String className; /*  * 登录密码  */ private String password; /*  * 是否是管理员的标志  1表示是,0表示不是  */ private String isManager; public String getStuID() { return stuID; } public void setStuID(String stuID) { this.stuID = stuID; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public Date getStuBirth() { return stuBirth; } public void setStuBirth(Date stuBirth) { this.stuBirth = stuBirth; } public String getStuTel() { return stuTel; } public void setStuTel(String stuTel) { this.stuTel = stuTel; } public String getStuEmail() { return stuEmail; } public void setStuEmail(String stuEmail) { this.stuEmail = stuEmail; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getStuIDCard() { return stuIDCard; } public void setStuIDCard(String stuIDCard) { this.stuIDCard = stuIDCard; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getIsManager() { return isManager; } public void setIsManager(String isManager) { this.isManager = isManager; } }



 

 

 

配置Student.hbm.xml文件

[html] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE hibernate-mapping PUBLIC   
    3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
    5.   
    6. <hibernate-mapping package="com.sgdata.po">  
    7.     <class name="Student" table="tb_students">  
    8.         <id name="stuID" column="stuID" type="java.lang.String" length="11">  
    9.             <generator class="assigned"></generator>  
    10.         </id>  
    11.         <property name="stuName" type="java.lang.String" length="30" not-null="true"></property>  
    12.         <property name="stuSex" type="java.lang.String" length="2" not-null="true"></property>  
    13.         <property name="stuBirth" type="java.util.Date" not-null="true"></property>  
    14.         <property name="stuTel" type="java.lang.String" length="20" not-null="true"></property>  
    15.         <property name="stuEmail" type="java.lang.String" length="20" not-null="true"></property>  
    16.         <property name="dept" type="java.lang.String" length="10" not-null="true"></property>  
    17.         <property name="stuIDCard" type="java.lang.String" length="20" not-null="true"></property>  
    18.         <property name="className" type="java.lang.String" length="20" not-null="true"></property>  
    19.         <property name="password" type="java.lang.String" length="10" not-null="true"></property>  
    20.         <property name="isManager" type="java.lang.String" length="1" not-null="false"></property>  
    21.     </class>  
    22. </hibernate-mapping>
  1. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.sgdata.po"> <class name="Student" table="tb_students"> <id name="stuID" column="stuID" type="java.lang.String" length="11"> <generator></generator> </id> <property name="stuName" type="java.lang.String" length="30" not-null="true"></property> <property name="stuSex" type="java.lang.String" length="2" not-null="true"></property> <property name="stuBirth" type="java.util.Date" not-null="true"></property> <property name="stuTel" type="java.lang.String" length="20" not-null="true"></property> <property name="stuEmail" type="java.lang.String" length="20" not-null="true"></property> <property name="dept" type="java.lang.String" length="10" not-null="true"></property> <property name="stuIDCard" type="java.lang.String" length="20" not-null="true"></property> <property name="className" type="java.lang.String" length="20" not-null="true"></property> <property name="password" type="java.lang.String" length="10" not-null="true"></property> <property name="isManager" type="java.lang.String" length="1" not-null="false"></property> </class> </hibernate-mapping>


DAO实现

[java] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. import java.util.List;  
    2.   
    3. import com.sgdata.po.Student;  
    4.   
    5. public interface StudentDao {  
    6.   
    7.     /** 
    8.      * 获取所有学生信息 
    9.      * @return 
    10.      */  
    11.     public List<Student> getAllStudentInfo();  
    12.       
    13. }
  1. import java.util.List; import com.sgdata.po.Student; public interface StudentDao { /**  * 获取所有学生信息  * @return  */ public List<Student> getAllStudentInfo(); }




[java] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {  
    2.       
    3.     @Resource HibernateTemplate ht;  
    4.       
    5.     /** 
    6.      * 获取所有信息 
    7.      */  
    8.     public List<Student> getAllStudentInfo() {  
    9.         String sql = "from Student";  
    10.         List<Student> students = (List<Student>) ht.find(sql);  
    11.         return students;  
    12.     }  
    13. }
  1. public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao { @Resource HibernateTemplate ht; /**  * 获取所有信息  */ public List<Student> getAllStudentInfo() { String sql = "from Student"; List<Student> students = (List<Student>) ht.find(sql); return students; } }



 

 

 

Service实现:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. import java.util.List;  
    2.   
    3. import com.sgdata.po.Student;  
    4.   
    5. public interface StudentService {  
    6.   
    7.     /** 
    8.      * 获取所有学生信息 
    9.      * @return 
    10.      */  
    11.     public List<Student> getAllStudentInfo();  
    12.     }  
    13. import java.util.List; import com.sgdata.po.Student; public interface StudentService { /**  * 获取所有学生信息  * @return  */ public List<Student> getAllStudentInfo(); }
    14. [java] view plain copy print?import java.util.List;    import javax.annotation.Resource;    import org.springframework.transaction.annotation.Transactional;    import com.sgdata.dao.StudentDao;  import com.sgdata.po.Student;  import com.sgdata.service.StudentService;    @Transactional(readOnly=false)  public class StudentServiceBean implements StudentService {        @Resource private StudentDao studentDao;      public List<Student> getAllStudentInfo() {          return studentDao.getAllStudentInfo();      }  }
  1. import java.util.List; import javax.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import com.sgdata.dao.StudentDao; import com.sgdata.po.Student; import com.sgdata.service.StudentService; @Transactional(readOnly=false) public class StudentServiceBean implements StudentService { @Resource private StudentDao studentDao; public List<Student> getAllStudentInfo() { return studentDao.getAllStudentInfo(); } }



 

 

 

Action实现:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片


    1. /** 
    2.  * 实现学生信息管理的Action类 
    3.  *   
    4.  */  
    5. public class StudentInfoManagerAction extends ActionSupport {  
    6.   
    7.     /** 
    8.      *  
    9.      */  
    10.     private static final long serialVersionUID = 1L;  
    11.       
    12.       
    13.     @Resource private StudentService studentService;  
    14.       
    15.     //页数  
    16.     int pagenum = 0;  
    17.       
    18.     //学号  
    19.     private String stuID;  
    20.       
    21.     //姓名  
    22.     private String stuName;  
    23.       
    24.     //性别  
    25.     private String stuSex;  
    26.       
    27.     //出生年月  
    28.     private String stuBirth;  
    29.       
    30.     //电话  
    31.     private String stuTel;  
    32.       
    33.     //邮箱  
    34.     private String stuEmial;  
    35.       
    36.     //系部  
    37.     private String dept;  
    38.       
    39.     //身份证  
    40.     private String stuIDCard;  
    41.       
    42.     //班级  
    43.     private String className;  
    44.       
    45.     //密码  
    46.     private String password;  
    47.       
    48.     /** 
    49.      * 学生对象来储存学生信息 
    50.      */  
    51.     private Student student;  
    52.       
    53.     /** 
    54.      * 学生信息的列表 
    55.      */  
    56.     private List<Student> studentsInfo;  
    57.       
    58.     /** 
    59.      * 学生学习成绩的信息列表 
    60.      */  
    61.     private List learnScores;  
    62.       
    63.     /** 
    64.      * 学生比赛成绩的信息列表 
    65.      */  
    66.     private List matchScores;  
    67.       
    68.     public StudentInfoManagerAction(){  
    69.         //student = new Student();  
    70.           
    71.     }  
    72.       
    73.     public Student getStudent() {  
    74.         return student;  
    75.     }  
    76.   
    77.     public void setStudent(Student student) {  
    78.         this.student = student;  
    79.     }  
    80.   
    81.     public void setStudentsInfo(List<Student> studentsInfo){  
    82.         this.studentsInfo = studentsInfo;  
    83.     }  
    84.   
    85.     public List<Student> getStudentsInfo() {  
    86.         return studentsInfo;  
    87.     }  
    88.   
    89.     public List getLearnScores() {  
    90.         return learnScores;  
    91.     }  
    92.   
    93.     public void setLearnScores(List learnScores) {  
    94.         this.learnScores = learnScores;  
    95.     }  
    96.   
    97.     public List getMatchScores() {  
    98.         return matchScores;  
    99.     }  
    100.   
    101.     public void setMatchScores(List matchScores) {  
    102.         this.matchScores = matchScores;  
    103.     }  
    104.   
    105.     public int getPagenum() {  
    106.         return pagenum;  
    107.     }  
    108.   
    109.     public void setPagenum(int pagenum) {  
    110.         this.pagenum = pagenum;  
    111.     }  
    112.   
    113.     public String getStuID() {  
    114.         return stuID;  
    115.     }  
    116.   
    117.     public void setStuID(String stuID) {  
    118.         this.stuID = stuID;  
    119.     }  
    120.   
    121.     public String getStuName() {  
    122.         return stuName;  
    123.     }  
    124.   
    125.     public void setStuName(String stuName) {  
    126.         this.stuName = stuName;  
    127.     }  
    128.   
    129.     public String getStuSex() {  
    130.         return stuSex;  
    131.     }  
    132.   
    133.     public void setStuSex(String stuSex) {  
    134.         this.stuSex = stuSex;  
    135.     }  
    136.   
    137.     public String getStuBirth() {  
    138.         return stuBirth;  
    139.     }  
    140.   
    141.     public void setStuBirth(String stuBirth) {  
    142.         this.stuBirth = stuBirth;  
    143.     }  
    144.   
    145.     public String getStuTel() {  
    146.         return stuTel;  
    147.     }  
    148.   
    149.     public void setStuTel(String stuTel) {  
    150.         this.stuTel = stuTel;  
    151.     }  
    152.   
    153.     public String getStuEmial() {  
    154.         return stuEmial;  
    155.     }  
    156.   
    157.     public void setStuEmial(String stuEmial) {  
    158.         this.stuEmial = stuEmial;  
    159.     }  
    160.   
    161.     public String getDept() {  
    162.         return dept;  
    163.     }  
    164.   
    165.     public void setDept(String dept) {  
    166.         this.dept = dept;  
    167.     }  
    168.   
    169.     public String getStuIDCard() {  
    170.         return stuIDCard;  
    171.     }  
    172.   
    173.     public void setStuIDCard(String stuIDCard) {  
    174.         this.stuIDCard = stuIDCard;  
    175.     }  
    176.   
    177.     public String getClassName() {  
    178.         return className;  
    179.     }  
    180.   
    181.     public void setClassName(String className) {  
    182.         this.className = className;  
    183.     }  
    184.   
    185.     public String getPassword() {  
    186.         return password;  
    187.     }  
    188.   
    189.     public void setPassword(String password) {  
    190.         this.password = password;  
    191.     }  
    192.   
    193.     /** 
    194.      * 获取学生的基本信息 
    195.      * @return 
    196.      * @throws Exception 
    197.      */  
    198.     //@Override  
    199.     public String getAllInfo() throws Exception {  
    200.         studentsInfo = studentService.getAllStudentInfo();  
    201.         return SUCCESS;  
    202.     }  
    203. }
  1. /**  * 实现学生信息管理的Action类  *  */ public class StudentInfoManagerAction extends ActionSupport { /**  *   */ private static final long serialVersionUID = 1L; @Resource private StudentService studentService; //页数 int pagenum = 0; //学号 private String stuID; //姓名 private String stuName; //性别 private String stuSex; //出生年月 private String stuBirth; //电话 private String stuTel; //邮箱 private String stuEmial; //系部 private String dept; //身份证 private String stuIDCard; //班级 private String className; //密码 private String password; /**  * 学生对象来储存学生信息  */ private Student student; /**  * 学生信息的列表  */ private List<Student> studentsInfo; /**  * 学生学习成绩的信息列表  */ private List learnScores; /**  * 学生比赛成绩的信息列表  */ private List matchScores; public StudentInfoManagerAction(){ //student = new Student(); } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public void setStudentsInfo(List<Student> studentsInfo){ this.studentsInfo = studentsInfo; } public List<Student> getStudentsInfo() { return studentsInfo; } public List getLearnScores() { return learnScores; } public void setLearnScores(List learnScores) { this.learnScores = learnScores; } public List getMatchScores() { return matchScores; } public void setMatchScores(List matchScores) { this.matchScores = matchScores; } public int getPagenum() { return pagenum; } public void setPagenum(int pagenum) { this.pagenum = pagenum; } public String getStuID() { return stuID; } public void setStuID(String stuID) { this.stuID = stuID; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public String getStuBirth() { return stuBirth; } public void setStuBirth(String stuBirth) { this.stuBirth = stuBirth; } public String getStuTel() { return stuTel; } public void setStuTel(String stuTel) { this.stuTel = stuTel; } public String getStuEmial() { return stuEmial; } public void setStuEmial(String stuEmial) { this.stuEmial = stuEmial; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getStuIDCard() { return stuIDCard; } public void setStuIDCard(String stuIDCard) { this.stuIDCard = stuIDCard; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /**  * 获取学生的基本信息  * @return  * @throws Exception  */ //@Override public String getAllInfo() throws Exception { studentsInfo = studentService.getAllStudentInfo(); return SUCCESS; } }



 

 

 

然后就可以在JSP页面引入

<%@ taglib uri="/struts-tags" prefix="s" %>

然后获取数据了

  1. [html]

view plain copy print?在CODE上查看代码片派生到我的代码片


  1. <table class="table table-hover">  
  2.  
  3.         <tr>  
  4.  
  5.             <th width="120">学号</th>  
  6.  
  7.             <th width="120">姓名</th>  
  8.  
  9.             <th width="120">性别</th>  
  10.  
  11.             <th width="120">班级</th>  
  12.  
  13.             <th width="120">系部</th>  
  14.  
  15.             <th width="100">出生年月</th>  
  16.  
  17.             <th width="100">操作</th>  
  18.  
  19.         </tr>  
  20.  
  21.            <s:iterator value="studentsInfo" id="ssif" >  
  22.  
  23.            <tr>  
  24.  
  25.             <td><s:property value="#ssif.stuID" /></td>  
  26.  
  27.             <td><s:property value="#ssif.stuName" /></td>  
  28.  
  29.             <td><s:property value="#ssif.stuSex" /></td>  
  30.  
  31.             <td><s:property value="#ssif.className" /></td>  
  32.  
  33.             <td><s:property value="#ssif.dept" /></td>  
  34.  
  35.             <td><s:property value="#ssif.stuBirth" /></td>  
  36.  
  37.             <td>  
  38.  
  39.                 <a class="button border-blue button-little" href="getStuInfoByIdAction?stuID=<s:property value='#ssif.stuID'/>">详情</a>   
  40.  
  41.                 <a class="button border-yellow button-little" href="getLearnScoresAction?stuID=<s:property value='#ssif.stuID' />" >学习</a>  
  42.  
  43.                 <a class="button border-green button-little" href="getMatchScoresAction?stuID=<s:property value='#ssif.stuID' />">比赛</a>   
  44.  
  45.             </td>  
  46.  
  47.            </tr>  
  48.  
  49.            </s:iterator>  
  50.  
  51.        </table>  
  52.  
  53.  <table class="table table-hover">          <tr>          <th width="120">学号</th>          <th width="120">姓名</th>          <th width="120">性别</th>          <th width="120">班级</th>          <th width="120">系部</th>          <th width="100">出生年月</th>          <th width="100">操作</th>          </tr>             <s:iterator value="studentsInfo" id="ssif" >             <tr>              <td><s:property value="#ssif.stuID" /></td>              <td><s:property value="#ssif.stuName" /></td>              <td><s:property value="#ssif.stuSex" /></td>              <td><s:property value="#ssif.className" /></td>              <td><s:property value="#ssif.dept" /></td>              <td><s:property value="#ssif.stuBirth" /></td>              <td>              <a class="button border-blue button-little" href="getStuInfoByIdAction?stuID=<s:property value='#ssif.stuID'/>">详情</a>               <a class="button border-yellow button-little" href="getLearnScoresAction?stuID=<s:property value='#ssif.stuID' />" >学习</a>              <a class="button border-green button-little" href="getMatchScoresAction?stuID=<s:property value='#ssif.stuID' />">比赛</a>               </td>             </tr>             </s:iterator>         </table>


实现数据获取


这是我结合Bootstrap和SSH做的,结合例子来说明实现过程,希望可以帮到学习的人,有疑惑请留言哈

0 0