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

ben52th 9年前發布 | 168K 次閱讀 Web框架

來自: http://blog.csdn.net/u014427391/article/details/50621111




我以我做的一個例子來說明框架的搭建過程 ^V^!


項目結構如圖:

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

daoDAO數據庫操作

poPOJO類,也就是持久化類

service:存放Service


dao類在Service類里調用,然后Service類再到action類里調用



搭建過程

我們先要準備jar價包,這個可以去官網下載

下面是我準備的開發jar價包

然后我為了提高安全性,我將所有的JSP頁面放在了WEB-INF下面


然后配置SSH的配置文件

Spring的配置文件代碼:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
    <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></pre>


Struts2的配置文件代碼:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    ";
<!-- 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" class="loginCheckAction">
        <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" class="studentAction" method="getAllInfo">
            <result name="success">/WEB-INF/page/admin/student/studentInfoManager.jsp</result>
    </action>
    <action name="getStuInfoByIdAction" class="studentAction" method="getInfoById">
        <result name="success">/WEB-INF/page/admin/student/studentInfoDetail.jsp</result>
    </action>
    <action name="getLearnScoresAction" class="studentAction" method="getLearnScoreById">
        <result name="success">/WEB-INF/page/admin/student/studentLearnScores.jsp</result>
    </action>
    <action name="getMatchScoresAction" class="studentAction" method="getMatchScoreById">
        <result name="success">/WEB-INF/page/admin/student/studentMatchScores.jsp</result>
    </action>

    <!-- 教師信息管理的Action -->
    <action name="getAllTeaInfoAction" class="teacherAction" method="getAllInfo">
            <result name="success">/WEB-INF/page/admin/teacher/teacherInfoManager.jsp</result>
    </action>
    <action name="getTeachingInfoAction" class="teacherAction" method="getTeachingInfoById">
            <result name="success">/WEB-INF/page/admin/teacher/teacherTeaching.jsp</result>
    </action>
    <action name="getMatchGuideInfoAction" class="teacherAction" method="getMatchGuideInfoById">
            <result name="success">/WEB-INF/page/admin/teacher/teacherMatchGuide.jsp</result>
    </action>
    <action name="getCourseStudentsInfoAction" class="teacherAction" method="getCourseStudentsInfoById">
            <result name="success">/WEB-INF/page/admin/teacher/teacherCourseStusInfo.jsp</result>
    </action>
    <action name="getMatchStudentsInfoAction" class="teacherAction" method="getMatchStudentsInfoById">
            <result name="success">/WEB-INF/page/admin/teacher/teacherMatchStusInfo.jsp</result>
    </action>

    <!-- 課程管理的Action -->
    <action name="getAllCourseInfoAction" class="courseAction" method="getAllInfo">
        <result name="success">/WEB-INF/page/admin/course/courseManager.jsp</result>
    </action>
    <action name="getTeachersInfoAction" class="courseAction" method="getTeachersInfoById">
        <result name="success">/WEB-INF/page/admin/course/courseTeachersInfo.jsp</result>
    </action>

    <!-- 比賽信息管理的Action -->
    <action name="getAllMatchInfoAction" class="matchAction" method="getAllInfo">
        <result name="success">/WEB-INF/page/admin/match/matchInfoManager.jsp</result>
    </action>
    <action name="getStudentsInfoAction" class="matchAction" method="getStudentsInfoById">
        <result name="success">/WEB-INF/page/admin/match/matchStudentsInfo.jsp</result>
    </action>

</package>

</struts></pre>

Hibernate的配置文件代碼:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          ";
<!-- 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></pre>



前面那些配置文件有包含其它的,這個要根據自己的項目需要去改的^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; }

}</pre>

配置Student.hbm.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    ";

<hibernate-mapping package="com.sgdata.po"> <class name="Student" table="tb_students"> <id name="stuID" column="stuID" type="java.lang.String" length="11"> <generator class="assigned"></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></pre>


DAO實現

import java.util.List;

import com.sgdata.po.Student;

public interface StudentDao {

/**
 * 獲取所有學生信息
 * @return
 */
public List<Student> getAllStudentInfo();

}</pre>

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;
}

}</pre>

Service實現:

import java.util.List;

import com.sgdata.po.Student;

public interface StudentService {

/**
 * 獲取所有學生信息
 * @return
 */
public List<Student> getAllStudentInfo();
}</pre> <p><br />

</p>

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();
}

}</pre>

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<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;
}

}</pre>

然后就可以在JSP頁面引入

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

然后獲取數據了

<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做的,結合例子來說明實現過程,希望可以幫到學習的人,有疑惑請留言哈!^V^

??

 本文由用戶 ben52th 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!