SSH框架搭建与项目实践步骤
2015-06-03 23:50
274 查看
一、工具和环境
MyEclipse10、Tomcat7.0、MySql5.6、JDK1.7、Struts2.0-Hibernate3.2-Spring2.0。
二、操作步骤
建立数据库:编写sql语句,建立数据库;新建工程:打开MyEclipse,File—>New—>WebProject;
导包: 下载jar包,将所有jar包粘贴进“WebRoot/WEB-INF/lib”文件夹里;
向工程中添加Spring框架,删除重复或有冲突的jar包;
编写WebRoot/WEB-INF目录下的“web.xml”;
编写“applicationContext.xml”,以后也要边编写类边补充;
编写简化后的“hibernate.cfg.xml”文件;
编写PO类和相应的“po类名.hbm.xml”映射文件;
编写DAO接口和DAO实现类;
编写业务接口和业务类;
编写Action类和VO类;
编写“struts.xml”文件;
编写jsp页面。
注:此操作步骤遵循ssh框架的分层结构,从底层逐层往上进行建立。有时候也可以根据需要改变建立步骤,如jsp页面就可以需要的时候编写即可。
三、详细实例
此处我们以“查看所有学生信息”为例,具体步骤如下:1、 建立数据库:编写sql语句,建立数据库studentdb,创建students表:
CREATE DATABASE studentdb; USE studentdb; DROP TABLE IF EXISTS students; CREATE TABLE students( id bigint NOT NULL AUTO_INCREMENT, sno varchar(12) NOT NULL UNIQUE, sname varchar(10) NOT NULL, ssex varchar(2) NOT NULL, sage int NOT NULL, sdept varchar(20) NOT NULL, PRIMARY KEY(id) ); insert into students(sno,sname,ssex,sage,sdept) values ('201506031234','张三','男',22,'信息学院'); insert into students(sno,sname,ssex,sage,sdept) values ('201506031235','李四','女',20,'艺术学院');
2、 新建工程:打开MyEclipse,File—>New—>Web Project,Project Name命名为StudentExample—>Finish:
3、 导包:将所需的jar包粘贴进“WebRoot/WEB-INF/lib”文件夹里:(点击下载jar包)
4、 向工程中添加Spring框架,删除重复或有冲突的jar包:
5、 打开WebRoot/WEB-INF目录下的“web.xml”,编写如下代码:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 定义WEB应用的名字 --> <display-name>ssh</display-name> <!-- 配置struts2 filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 指定WEB应用的默认首页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置spring监听器和上下文参数 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> </web-app>
至此,SSH框架的搭建已经基本完成,后面主要是项目的具体实现和编码,代码仅供参考,小弟也是新手一枚,望多多指教。
6、 编写“applicationContext.xml”,以后也要边编写类边补充:(这里给出已写好后的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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 定义数据源: --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/STUDENTDB"/> <!--连接数据库STUDENTDB--> <property name="user" value="root"/> <property name="password" value="root"/> <!-- mysql的用户名和密码 --> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="1"/> <property name="initialPoolSize" value="1"/> <property name="maxIdleTime" value="30"/> </bean> <!-- 定义Hibernate的SessionFactory: --> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update </value> </property> <property name="mappingResources"> <list> <value>com/po/StudentPO.hbm.xml</value> <!-- PO类的Hibernate映射文件路径 --> </list> </property> </bean> <!-- 定义DAO: --> <bean id="studentDao" class="com.dao.StudentDaoImpl" lazy-init="true"> <property name="sessionFactory" ref="sessionfactory"></property> </bean> <!-- 定义业务类: --> <bean id="studentService" class="com.service.StudentServiceImpl"> <property name="studentDao" ref="studentDao"></property> </bean> <!-- 以下定义各个Action类: --> <bean id="findStudents" class="com.action.FindStudentsAction" scope="prototype"> <property name="studentService" ref="studentService"></property> </bean> </beans>
7、 在src目录新建xml文件,编写简化后的“hibernate.cfg.xml”文件:(须保留,其头文件仍然有用)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> </session-factory> </hibernate-configuration>
8、 编写“StudentPO”类和相应的“StudentPO.hbm.xml”映射文件:
StudentPO.java:
package com.po; public class StudentPO { private long oid; private String sno; private String sname; private String ssex; private int sage; private String sdept; //setter和getter方法 public long getOid() { return oid; } public void setOid(long oid) { this.oid = oid; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public String getSdept() { return sdept; } public void setSdept(String sdept) { this.sdept = sdept; } }
StudentPO.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.po.StudentPO" table="STUDENTS"> <id name="oid" column="ID" type="long"> <generator class="native" /> </id> <property name="sno" column="SNO" type="string" not-null="true" unique="true" /> <property name="sname" column="SNAME" type="string" not-null="true" /> <property name="ssex" column="SSEX" type="string" not-null="true" /> <property name="sage" column="SAGE" type="integer" not-null="true" /> <property name="sdept" column="SDEPT" type="string" not-null="true" /> </class> </hibernate-mapping>
9、 编写DAO接口和DAO实现类:
StudentDaoInt.java:
package com.dao; import java.util.List; import com.po.StudentPO; public interface StudentDaoInt { //查询所有学生信息 public List<StudentPO> findAllStudent() throws Exception; }
StudentDaoImpl.java:
package com.dao; import java.util.ArrayLis d148 t; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.po.StudentPO; public class StudentDaoImpl extends HibernateDaoSupport implements StudentDaoInt{ //查询所有学生信息 public List<StudentPO> findAllStudent() throws Exception{ List<StudentPO> students = new ArrayList<StudentPO>(); students = this.getHibernateTemplate().find("from StudentPO"); return students; } }
10、 编写业务接口和业务类:
StudentServiceInt.java:
package com.service; import java.util.List; import com.po.StudentPO; public interface StudentServiceInt { //查看所有学生信息 public List<StudentPO> findAllStudents(); }
StudentServiceImpl.java:
package com.service; import java.util.ArrayList; import java.util.List; import com.dao.StudentDaoInt; import com.po.StudentPO; public class StudentServiceImpl implements StudentServiceInt{ private StudentDaoInt studentDao; //采用set注入 public StudentDaoInt getStudentDao() { return studentDao; } public void setStudentDao(StudentDaoInt studentDao) { this.studentDao = studentDao; } //查看所有学生信息 public List<StudentPO> findAllStudents(){ List<StudentPO> students = new ArrayList<StudentPO>(); try { students = studentDao.findAllStudent(); } catch (Exception e) { e.printStackTrace(); } return students; } }
11、编写Action类和VO类:
FindStudentsAction.java:
package com.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.po.StudentPO; import com.service.StudentServiceInt; public class FindStudentsAction extends ActionSupport{ private StudentServiceInt studentService; List<StudentPO> students = new ArrayList<StudentPO>(); public StudentServiceInt getStudentService() { return studentService; } public void setStudentService(StudentServiceInt studentService) { this.studentService = studentService; } public List<StudentPO> getStudents() { return students; } public void setStudents(List<StudentPO> students) { this.students = students; } public String execute() throws Exception{ students = studentService.findAllStudents(); return "success"; } }
StudentVO.java:
package com.vo; public class StudentVO { private String sno; private String sname; private String ssex; private int sage; private String sdept; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public String getSdept() { return sdept; } public void setSdept(String sdept) { this.sdept = sdept; } }
12、在src目录新建xml文件,编写“struts.xml”文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="false" /> <include file="struts-default.xml" /> <package name="students" extends="struts-default"> <action name="findStudents" class="findStudents"> <result name="success">index.jsp</result> <result name="input">index.jsp</result> </action> </package> </struts>
13、编写jsp页面:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>查看学生信息页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <a href="findStudents.action">点击查看学生信息</a> <br><br> <table border="1"> <tr> <th>序号</th> <th>学号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>院系</th> </tr> <s:iterator value="students" status="st"> <tr> <td><s:property value="#st.count"/></td> <td><s:property value="sno"/></td> <td><s:property value="sname"/></td> <td><s:property value="ssex"/></td> <td><s:property value="sage"/></td> <td><s:property value="sdept"/></td> </tr> </s:iterator> </table> </center> </body> </html>
工程结构图如下:
最后的运行结果如下:
附:实例下载链接http://download.csdn.net/download/azrael05020/8773031
相关文章推荐
- Linux Generating SSH Keys
- Scrapy的架构介绍
- 发布一个自己做的jsp博客系统
- 提供个 全免杀海洋2006asp木马 下载
- 高手写的Tracer-Flash代码调试类代码下载
- Flex字符串比较 还有Flex字符串操作
- Vista 防火墙 Vista Firewall Control v1.0.11 下载
- 国外Lightbox v2.03.3 最新版 下载
- 火影漫画下载助手 下载
- USBkill U盘防火墙 v3.0 下载
- 腾讯 Tencent Traveler v3.4 下载
- 查杀软件 360安全卫士 v3.2.1.1001 下载
- Kaspersky 6.0.2.666 MP2 nct Release+汉化补丁 下载
- Symantec Norton Ghost v12.0 Retail ISO 多国语言版 下载
- 电脑硬件分析Ultimate Boot CD v4.1.1 下载
- 花香盈路7.6商业美化版 下载
- Orca (Msi文件编辑器) 3.1.4000.1830 修正 汉化版 下载
- 办公室文档模板集合 下载
- Adobe Shockwave Player v10.2.0.022 下载
- 太平洋软件下载站SQL全功能新云2.1商业版代码 下载