SSH框架系列之框架整合教程

举报
yd_273762914 发表于 2020/12/03 01:13:22 2020/12/03
【摘要】     我以我做的一个例子来说明框架的搭建过程 ^V^!   项目结构如图: action:存放Action类,也就是控制类 dao:DAO数据库操作 po:POJO类,也就是持久化类 service:存放Service类   dao类在Service类里调用,然后Service类再到action类里调用     搭建过程 我们先要准备jar价...

 

 

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

 

项目结构如图:

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

dao:DAO数据库操作

po:POJO类,也就是持久化类

service:存放Service类

 

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

 

 

搭建过程

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

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

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

 

然后配置SSH的配置文件

Spring的配置文件代码:


  
  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. <!-- 开启Spring框架的事务管理 ,开启之后@Transaction就可以用了 -->
  34. <tx:annotation-driven transaction-manager="txManager"/>
  35. <!-- 实现教师信息管理需要配置的Bean -->
  36. <bean id="teacherDao" class="com.sgdata.dao.impl.TeacherDaoImpl">
  37. <property name="sessionFactory" ref="sessionFactory"></property>
  38. </bean>
  39. <bean id="teacherService" class="com.sgdata.service.impl.TeacherServiceBean">
  40. </bean>
  41. <!--scope默认采用的是单例模式,scope="prototype" 可以保证 当有请求的时候都创建一个Action对象,保证Struts的Action线程安全 -->
  42. <bean id="teacherAction" class="com.sgdata.action.TeacherInfoManagerAction" scope="prototype"></bean>
  43. <bean id="loginCheckAction" class="com.sgdata.action.LoginCheckAction" scope="prototype"></bean>
  44. <!-- 实现学生信息管理需要配置的Bean -->
  45. <bean id="studentDao" class="com.sgdata.dao.impl.StudentDaoImpl">
  46. <property name="sessionFactory" ref="sessionFactory"></property>
  47. </bean>
  48. <bean id="studentService" class="com.sgdata.service.impl.StudentServiceBean"></bean>
  49. <bean id="studentAction" class="com.sgdata.action.StudentInfoManagerAction" scope="prototype"></bean>
  50. <!-- 实现课程信息管理需要配置的Bean -->
  51. <bean id="courseDao" class="com.sgdata.dao.impl.CourseDaoImpl">
  52. <property name="sessionFactory" ref="sessionFactory"></property>
  53. </bean>
  54. <bean id="courseService" class="com.sgdata.service.impl.CourseServiceBean"></bean>
  55. <bean id="courseAction" class="com.sgdata.action.CourseInfoManagerAction" scope="prototype"></bean>
  56. <!-- 实现比赛信息管理需要配置的Bean -->
  57. <bean id="matchDao" class="com.sgdata.dao.impl.MatchDaoImpl">
  58. <property name="sessionFactory" ref="sessionFactory"></property>
  59. </bean>
  60. <bean id="matchService" class="com.sgdata.service.impl.MatchServiceBean"></bean>
  61. <bean id="matchAction" class="com.sgdata.action.MatchInfoManagerAction" scope="prototype"></bean>
  62. </beans>

 

Struts2的配置文件代码:

 


  
  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. <!-- 登录验证的Action -->
  13. <action name="loginAction" class="loginCheckAction">
  14. <result name="success">/WEB-INF/page/admin/index.jsp</result>
  15. <result name="input">/WEB-INF/page/admin/login.jsp</result>
  16. </action>
  17. <!--
  18. SSH项目WEB-INF下面的页面跳转要通过Servlet来实现,这样确实是麻烦了点,
  19. 不过安全性就提高上去了,因为放在WEB-INF下面的JSP页面,是不可以直接访问的
  20. -->
  21. <action name="indexAction">
  22. <result>/WEB-INF/page/admin/index.jsp</result>
  23. </action>
  24. <action name="gotoLoginAction">
  25. <result>/WEB-INF/page/admin/login.jsp</result>
  26. </action>
  27. <!-- 学生信息管理的Action -->
  28. <action name="getAllStuInfoAction" class="studentAction" method="getAllInfo">
  29. <result name="success">/WEB-INF/page/admin/student/studentInfoManager.jsp</result>
  30. </action>
  31. <action name="getStuInfoByIdAction" class="studentAction" method="getInfoById">
  32. <result name="success">/WEB-INF/page/admin/student/studentInfoDetail.jsp</result>
  33. </action>
  34. <action name="getLearnScoresAction" class="studentAction" method="getLearnScoreById">
  35. <result name="success">/WEB-INF/page/admin/student/studentLearnScores.jsp</result>
  36. </action>
  37. <action name="getMatchScoresAction" class="studentAction" method="getMatchScoreById">
  38. <result name="success">/WEB-INF/page/admin/student/studentMatchScores.jsp</result>
  39. </action>
  40. <!-- 教师信息管理的Action -->
  41. <action name="getAllTeaInfoAction" class="teacherAction" method="getAllInfo">
  42. <result name="success">/WEB-INF/page/admin/teacher/teacherInfoManager.jsp</result>
  43. </action>
  44. <action name="getTeachingInfoAction" class="teacherAction" method="getTeachingInfoById">
  45. <result name="success">/WEB-INF/page/admin/teacher/teacherTeaching.jsp</result>
  46. </action>
  47. <action name="getMatchGuideInfoAction" class="teacherAction" method="getMatchGuideInfoById">
  48. <result name="success">/WEB-INF/page/admin/teacher/teacherMatchGuide.jsp</result>
  49. </action>
  50. <action name="getCourseStudentsInfoAction" class="teacherAction" method="getCourseStudentsInfoById">
  51. <result name="success">/WEB-INF/page/admin/teacher/teacherCourseStusInfo.jsp</result>
  52. </action>
  53. <action name="getMatchStudentsInfoAction" class="teacherAction" method="getMatchStudentsInfoById">
  54. <result name="success">/WEB-INF/page/admin/teacher/teacherMatchStusInfo.jsp</result>
  55. </action>
  56. <!-- 课程管理的Action -->
  57. <action name="getAllCourseInfoAction" class="courseAction" method="getAllInfo">
  58. <result name="success">/WEB-INF/page/admin/course/courseManager.jsp</result>
  59. </action>
  60. <action name="getTeachersInfoAction" class="courseAction" method="getTeachersInfoById">
  61. <result name="success">/WEB-INF/page/admin/course/courseTeachersInfo.jsp</result>
  62. </action>
  63. <!-- 比赛信息管理的Action -->
  64. <action name="getAllMatchInfoAction" class="matchAction" method="getAllInfo">
  65. <result name="success">/WEB-INF/page/admin/match/matchInfoManager.jsp</result>
  66. </action>
  67. <action name="getStudentsInfoAction" class="matchAction" method="getStudentsInfoById">
  68. <result name="success">/WEB-INF/page/admin/match/matchStudentsInfo.jsp</result>
  69. </action>
  70. </package>
  71. </struts>

 

 

 

 

Hibernate的配置文件代码:


  
  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. </hibernate-configuration>

 

 

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

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

 

创建POJO实体类:

 


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

 

 

 

 

配置Student.hbm.xml文件


  
  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. <hibernate-mapping package="com.sgdata.po">
  6. <class name="Student" table="tb_students">
  7. <id name="stuID" column="stuID" type="java.lang.String" length="11">
  8. <generator class="assigned"></generator>
  9. </id>
  10. <property name="stuName" type="java.lang.String" length="30" not-null="true"></property>
  11. <property name="stuSex" type="java.lang.String" length="2" not-null="true"></property>
  12. <property name="stuBirth" type="java.util.Date" not-null="true"></property>
  13. <property name="stuTel" type="java.lang.String" length="20" not-null="true"></property>
  14. <property name="stuEmail" type="java.lang.String" length="20" not-null="true"></property>
  15. <property name="dept" type="java.lang.String" length="10" not-null="true"></property>
  16. <property name="stuIDCard" type="java.lang.String" length="20" not-null="true"></property>
  17. <property name="className" type="java.lang.String" length="20" not-null="true"></property>
  18. <property name="password" type="java.lang.String" length="10" not-null="true"></property>
  19. <property name="isManager" type="java.lang.String" length="1" not-null="false"></property>
  20. </class>
  21. </hibernate-mapping>


DAO实现

 


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

 

 

 


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

 

 

 

 

 

 

Service实现:


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

 

 


  
  1. import java.util.List;
  2. import javax.annotation.Resource;
  3. import org.springframework.transaction.annotation.Transactional;
  4. import com.sgdata.dao.StudentDao;
  5. import com.sgdata.po.Student;
  6. import com.sgdata.service.StudentService;
  7. @Transactional(readOnly=false)
  8. public class StudentServiceBean implements StudentService {
  9. @Resource private StudentDao studentDao;
  10. public List<Student> getAllStudentInfo() {
  11. return studentDao.getAllStudentInfo();
  12. }
  13. }

 

 

 

 

Action实现:

 


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

 

 

 

 

然后就可以在JSP页面引入

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

然后获取数据了


  
  1. <table class="table table-hover">
  2. <tr>
  3. <th width="120">学号</th>
  4. <th width="120">姓名</th>
  5. <th width="120">性别</th>
  6. <th width="120">班级</th>
  7. <th width="120">系部</th>
  8. <th width="100">出生年月</th>
  9. <th width="100">操作</th>
  10. </tr>
  11. <s:iterator value="studentsInfo" id="ssif" >
  12. <tr>
  13. <td><s:property value="#ssif.stuID" /></td>
  14. <td><s:property value="#ssif.stuName" /></td>
  15. <td><s:property value="#ssif.stuSex" /></td>
  16. <td><s:property value="#ssif.className" /></td>
  17. <td><s:property value="#ssif.dept" /></td>
  18. <td><s:property value="#ssif.stuBirth" /></td>
  19. <td>
  20. <a class="button border-blue button-little" href="getStuInfoByIdAction?stuID=<s:property value='#ssif.stuID'/>">详情</a>
  21. <a class="button border-yellow button-little" href="getLearnScoresAction?stuID=<s:property value='#ssif.stuID' />" >学习</a>
  22. <a class="button border-green button-little" href="getMatchScoresAction?stuID=<s:property value='#ssif.stuID' />">比赛</a>
  23. </td>
  24. </tr>
  25. </s:iterator>
  26. </table>

 

实现数据获取


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



文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/50621111

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。