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

最后更新于:2022-04-01 19:53:06

我以我做的一个例子来说明框架的搭建过程 ^V^! 项目结构如图: action:存放Action类,也就是控制类 dao:DAO数据库操作 po:POJO类,也就是持久化类 service:存放Service类 dao类在Service类里调用,然后Service类再到action类里调用 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caa834c0784.jpg) 搭建过程 我们先要准备jar价包,这个可以去官网下载 下面是我准备的开发jar价包 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caa834d51cb.jpg) 然后我为了提高安全性,我将所有的JSP页面放在了WEB-INF下面 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caa8350ece9.jpg) 然后配置SSH的配置文件 Spring的配置文件代码: ~~~ ~~~ Hibernate的配置文件代码: ~~~ /WEB-INF/page/admin/index.jsp /WEB-INF/page/admin/login.jsp /WEB-INF/page/admin/index.jsp /WEB-INF/page/admin/login.jsp /WEB-INF/page/admin/student/studentInfoManager.jsp /WEB-INF/page/admin/student/studentInfoDetail.jsp /WEB-INF/page/admin/student/studentLearnScores.jsp /WEB-INF/page/admin/student/studentMatchScores.jsp /WEB-INF/page/admin/teacher/teacherInfoManager.jsp /WEB-INF/page/admin/teacher/teacherTeaching.jsp /WEB-INF/page/admin/teacher/teacherMatchGuide.jsp /WEB-INF/page/admin/teacher/teacherCourseStusInfo.jsp /WEB-INF/page/admin/teacher/teacherMatchStusInfo.jsp /WEB-INF/page/admin/course/courseManager.jsp /WEB-INF/page/admin/course/courseTeachersInfo.jsp /WEB-INF/page/admin/match/matchInfoManager.jsp /WEB-INF/page/admin/match/matchStudentsInfo.jsp ~~~ 前面那些配置文件有包含其它的,这个要根据自己的项目需要去改的^V^ 下面以学生信息管理的实现过程进行说明,只说明这个例子哈! 创建POJO实体类: ~~~ 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文件 ~~~ ~~~ DAO实现 ~~~ import java.util.List; import com.sgdata.po.Student; public interface StudentDao { /** * 获取所有学生信息 * @return */ public List getAllStudentInfo(); } ~~~ ~~~ public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao { @Resource HibernateTemplate ht; /** * 获取所有信息 */ public List getAllStudentInfo() { String sql = "from Student"; List students = (List) ht.find(sql); return students; } } ~~~ Service实现: ~~~ import java.util.List; import com.sgdata.po.Student; public interface StudentService { /** * 获取所有学生信息 * @return */ public List getAllStudentInfo(); } ~~~ ~~~ 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 getAllStudentInfo() { return studentDao.getAllStudentInfo(); } } ~~~ Action实现: ~~~ /** * 实现学生信息管理的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 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 studentsInfo){ this.studentsInfo = studentsInfo; } public List 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页面引入 然后获取数据了 ~~~
学号 姓名 性别 班级 系部 出生年月 操作
详情 学习 比赛
~~~ 实现数据获取 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caa8352379b.jpg) 这是我结合Bootstrap和SSH做的,结合例子来说明实现过程,希望可以帮到学习的人,有疑惑请留言哈!^V^
';