SSH三大框架開(kāi)發(fā)案例_第1頁(yè)
SSH三大框架開(kāi)發(fā)案例_第2頁(yè)
SSH三大框架開(kāi)發(fā)案例_第3頁(yè)
SSH三大框架開(kāi)發(fā)案例_第4頁(yè)
SSH三大框架開(kāi)發(fā)案例_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、jdk6+myeclipse8.5+tomcat6+oracle+spring2.0+hibernate3+struts2 好了,開(kāi)始我們的第一個(gè)ssh2之旅吧。 首先先分析一下我們的第一個(gè)ssh2項(xiàng)目的需求,簡(jiǎn)單的說(shuō)就是有一張表單,讓你填寫(xiě)用戶(hù)名和密碼,提交后存入數(shù)據(jù)庫(kù)。就這么簡(jiǎn)單,呵呵。 第一步:。我們首先新建一張mysql數(shù)據(jù)表。Sql如下: Create table t_employee(Id integer not null primary key,Name varchar(20),Age varchar(20),Sex varchar(20),Tel varchar(20),Add

2、ress varchar(20))數(shù)據(jù)創(chuàng)建好后結(jié)構(gòu)如下:第二步 打開(kāi)myeclipse,新建一個(gè)web項(xiàng)目,命名為ssh2-2, java ee規(guī)范我們選擇5,如圖 第三步 務(wù)必小心的一步 導(dǎo)入ssh2的各個(gè)jar,步驟如下: 選中當(dāng)前的項(xiàng)目后,點(diǎn)擊菜單爛的myeclipse-project capablities-add hibernate項(xiàng),跳出如圖 務(wù)必按照?qǐng)D示選擇,尤其是copy checkde一項(xiàng),然后點(diǎn)擊next,默認(rèn)next,去掉specify database復(fù)選框,next,去掉create session復(fù)選框,finish。 再次選中選中當(dāng)前的項(xiàng)目后,點(diǎn)擊菜單爛的myec

3、lipse-project capablities-add spring項(xiàng),跳出如圖 依然務(wù)必按照如是選擇,jar文件選擇如下5個(gè): 點(diǎn)擊next, 之后按下選擇,務(wù)必, next后finsh即可。 然后導(dǎo)入struts2的jar 如下5個(gè)放到lib下 然后放入我們的數(shù)據(jù)庫(kù)驅(qū)動(dòng)jar mysql-connector-java-5.0.8-bin.jar 沒(méi)得話(huà)附件中有 至此,包都導(dǎo)入完畢 第四步 配置web.xml 代碼如下 注意注釋?zhuān)?Xml代碼 1. <?xml version="1.0" encoding="UTF-8"?

4、>   2. <web-app version="2.5"     3.     xmlns="    4.     xmlns:xsi="/2001/XMLSchema-instance"     5.     xsi:schemaLocation="  

5、;  6.      7.         8.             <!- struts2的常規(guī)配置 ->   9.     <filter>   10.        

6、 <filter-name>struts2</filter-name>   11.         <filter-class>   12.             org.apache.struts2.dispatcher.FilterDispatcher    13.  

7、0;      </filter-class>   14.     </filter>   15.     <filter-mapping>   16.         <filter-name>struts2</filter-name>  

8、; 17.         <url-pattern>/*</url-pattern>   18.     </filter-mapping>   19.         20.         <!-  在web應(yīng)用啟動(dòng)是自動(dòng)形成一個(gè)s

9、pring容器 ->   21.     <listener>     22.         <listener-class>   23.             org.springframework.web.context.ContextLoa

10、derListener    24.         </listener-class>   25.     </listener>   26.   <welcome-file-list>   27.     <welcome-file>index.jsp</welcome-file&

11、gt;   28.   </welcome-file-list>   29. </web-app>   <?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation=&quo

12、t;<!- struts2的常規(guī)配置 -><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>&l

13、t;/filter-mapping><!- 在web應(yīng)用啟動(dòng)是自動(dòng)形成一個(gè)spring容器 -><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-ap

14、p>第五步: 從視圖層入手;在webroot目錄下新建一個(gè)add.jsp文件 如下代碼; Jsp代碼 <% page language="java" import="java.util.*" pageEncoding="UTF-8"%><% taglib uri=" prefix="c"%><% taglib prefix="s" uri="/struts-tags" %> <%String path = reques

15、t.getContextPath();String basePath = request.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html> <head> <base href="<%=basePath%>&q

16、uot;> <title>My JSP 'start.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta

17、 http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!-<link rel="stylesheet" type="text/css" href="styles.css">-> </head> <body> &l

18、t;h1 align="center">員工信息</h1> <form action="$pageContext.request.contextPath/emp/addSaveemployee.action" method="post"> <table align="center" border="2" bgcolor=""> <tr><td align="center" colspan=&quo

19、t;2">員工信息錄入</td></tr> <tr><td>姓名</td><td><input type="text" name=""></td></tr> <tr><td>年齡</td><td><input type="text" name="employee.age"></td></tr

20、> <tr><td>性別</td><td> 男<input type="radio" name="employee.sex" value="0" checked> 女<input type="radio" name="employee.sex" value="1"> </td></tr> <tr><td>電話(huà)</td><td>&

21、lt;input type="text" name="employee.tel"></td></tr> <tr><td>地址</td><td><input type="text" name="employee.address"></td></tr> <tr><td align="center" colspan="2"><input t

22、ype="submit" value="提交"></td> </tr> </table> </form> </body></html>接下去順?biāo)浦郏匀婚_(kāi)發(fā)action,及控制層 新建包c(diǎn)om.ucap.emp.action新建一個(gè)EmployeeAction.java 代碼初步如下: Java代碼 package com.ucap.emp.action;import java.util.List;import org.apache.struts2.ServletAction

23、Context;import com.opensymphony.xwork2.ActionSupport;import com.ucap.emp.pojo.Employee;import com.ucap.emp.service.EmployeeService;public class EmployeeAction extends ActionSupport private Employee employee; private List<Employee> list; private EmployeeService employeeService; public List<E

24、mployee> getList() return list;public void setList(List<Employee> list) this.list = list;public EmployeeService getEmployeeService() return employeeService;public void setEmployeeService(EmployeeService employeeService) this.employeeService = employeeService; public Employee getEmployee() r

25、eturn employee;public void setEmployee(Employee employee) this.employee = employee; /查詢(xún) public String list() list = this.employeeService.findAll(employee); / ServletActionContext.getRequest().setAttribute("list", list);return "list" /增加 public String add()return "add" /

26、增加保存 public String addSave() this.employeeService.save(this.employee); employee=new Employee();return list(); /更改之前 public String updateBefore() employee=this.employeeService.findById(employee.getId();return "update" /更改 public String update() this.employeeService.update(employee); employe

27、e=new Employee();return list(); /刪除 public String delete() this.employeeService.delete(employee);return list();于是,自然我們要新建一個(gè)pojo 新建包c(diǎn)om.ucap.emp.pojo 新建Employee.java Java代碼 package com.ucap.emp.pojo;public class Employee private Integer id; private String name; private String age ; private String sex

28、 ; private String tel; private String address; public Employee() public Employee(Integer id, String name, String age, String sex, String tel,String address) super();this.id = id; = name;this.age = age;this.sex = sex;this.tel = tel;this.address = address;public Integer getId() return id;publ

29、ic void setId(Integer id) this.id = id;public String getName() return name;public void setName(String name) = name;public String getAge() return age;public void setAge(String age) this.age = age;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public String g

30、etTel() return tel;public void setTel(String tel) this.tel = tel;public String getAddress() return address;public void setAddress(String address) this.address = address; 好了,現(xiàn)在我們先把控制層的action放在這里,開(kāi)發(fā)業(yè)務(wù)層和持久層(DAO) 新建包c(diǎn)om.ucap.emp.dao 建類(lèi)EmployeeDAO.java 代碼如下: Java代碼 package com.ucap.emp.dao;import java.ut

31、il.List;import com.ucap.emp.pojo.Employee;public interface EmployeeDao public void saveEmployee(Employee employee); public void removeEmployee(Employee employee); public void updateEmployee(Employee employee); public Employee findEmployeeById(Integer id); public List<Employee> findAllEmployee(

32、Employee employee);這是一個(gè)接口 在開(kāi)發(fā)dao的實(shí)現(xiàn)類(lèi) 新建包c(diǎn)om.ucap.emp.dao.impl 建類(lèi)EmployeeDAOImpl.java 代碼: Java代碼package com.ucap.emp.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ucap.emp.dao.EmployeeDao;import com.ucap.emp.pojo.Employee;public class

33、EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao public List<Employee> findAllEmployee(Employee employee) String hql="from Employee "String where =" where 1= 1 "if(employee.getName()!=null&&!"".equals(employee.getName()hql+=where+" a

34、nd name like '%"+employee.getName()+"%' order by id desc"return this.getHibernateTemplate().find(hql); public void saveEmployee(Employee employee) this.getHibernateTemplate().save(employee); public void updateEmployee(Employee employee) this.getHibernateTemplate().update(emplo

35、yee);public void removeEmployee(Employee employee) this.getHibernateTemplate().delete(employee); public Employee findEmployeeById(Integer id) Employee employ=(Employee) this.getHibernateTemplate().get(Employee.class, id);return employ;好了,數(shù)據(jù)持久層(dao)開(kāi)發(fā)好了,按順序,現(xiàn)在應(yīng)該開(kāi)發(fā)業(yè)務(wù)層了, 新建包c(diǎn)om.ucap.emp.service 建類(lèi)Emplo

36、yeeService.java 代碼: Java代碼package com.ucap.emp.service;import java.util.List;import com.ucap.emp.pojo.Employee;public interface EmployeeService public void save(Employee employ);public void update(Employee employ);public void delete(Employee employ);public List<Employee> findAll(Employee emplo

37、yee);public Employee findById(Integer id);這是一個(gè)接口類(lèi) 然后寫(xiě)實(shí)現(xiàn)類(lèi) 新建包 com.ucap.emp.service.impl 建類(lèi) EmployeeServiceImpl.java Java代碼package com.ucap.emp.service.impl;import java.util.List;import com.ucap.emp.dao.EmployeeDao;import com.ucap.emp.pojo.Employee;import com.ucap.emp.service.EmployeeService;public cl

38、ass EmployeeServiceImpl implements EmployeeService private EmployeeDao employeeDao; public EmployeeDao getEmployeeDao() return employeeDao;public void setEmployeeDao(EmployeeDao employeeDao) this.employeeDao = employeeDao; /服務(wù)層方法public void delete(Employee employ) this.employeeDao.removeEmployee(emp

39、loy);public List<Employee> findAll(Employee employ) return this.employeeDao.findAllEmployee(employ); public Employee findById(Integer id) return this.employeeDao.findEmployeeById(id);public void save(Employee employ) this.employeeDao.saveEmployee(employ);public void update(Employee employ) thi

40、s.employeeDao.updateEmployee(employ);業(yè)務(wù)和持久層都開(kāi)發(fā)好了,現(xiàn)在我們回到控制層action 修改action的代碼如下: Java代碼 (查看前面完整的action層代碼)這里就不做出來(lái)了。修改struts.xml 如下 Xml代碼<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/Apache Software Foundation/DTD Struts Configuration 2.0/EN"

41、"/dtds/struts-2.0.dtd"> <struts> <package name="employ" namespace="/emp" extends="struts-default"> <action name="*employee" class="employeeAction" method="1"> <result name="add&q

42、uot; >/add.jsp</result> <result name="update" >/update.jsp</result> <result name="list" >/list.jsp</result> </action> </package></struts>至此,我們已經(jīng)開(kāi)發(fā)好了一個(gè)典型的mvc項(xiàng)目,現(xiàn)在的任務(wù)是將strust2和hibernate都放入spring容器管理, 現(xiàn)在我們的項(xiàng)目結(jié)構(gòu)如下然后修改 applicationCont

43、ext.xml 代碼如下 注意注釋 Xml代碼 <?xml version="1.0" encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans http:/www.spr

44、/schema/beans/spring-beans-2.0.xsd"> <!- 配置數(shù)據(jù)源 -> <bean id="dataSource" class="mons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" ></property> <property name=&

45、quot;url" value="jdbc:oracle:thin:0:1521:egov" ></property> <property name="username" value="zhang"></property> <property name="password" value="zhang" ></property> <property name="maxActive&qu

46、ot; value="100" ></property> <property name="maxIdle" value="500" ></property> <property name="maxWait" value="500" ></property> </bean> <!- 一下配置sessionFactory 這個(gè)東西曾經(jīng)是在hibernate中定義的 如今交給了spring管理 -> <!-

47、 配置LocalSessionFactoryBean(工廠類(lèi))創(chuàng)建出sessionFactory(產(chǎn)品) -> <bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property> <!- 注入數(shù)據(jù)源

48、 -> <property name="dataSource" ref="dataSource"></property> <!- 注入其它hibernate的配置信息 -> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.dialect&qu

49、ot;>org.hibernate.dialect.OracleDialect</prop> </props> </property> <!- 注入映射文件 -> <property name="mappingResources"> <list> <!- 要修改的地方:注入自己需要的映射文件,可以是多個(gè) -> <value>com/ucap/emp/pojo/Employee.hbm.xml</value> </list> </property

50、> </bean> <!- 以下使一些用到的類(lèi) 在使用類(lèi)中均沒(méi)有實(shí)例化這個(gè)類(lèi),都交給了spring管理 -> <!- DAObean -> <bean id="employeeDao" class="com.ucap.emp.dao.impl.EmployeeDaoImpl" scope="singleton"> <property name="sessionFactory" ref="sessionFactory"></p

51、roperty> </bean> <!- 配置注入:service<-DAO,可以多個(gè)dao注入同一個(gè)service -> <bean id="employeeService" class="com.ucap.emp.service.impl.EmployeeServiceImpl"> <property name="employeeDao" ref="employeeDao"></property> </bean> <!-

52、配置注入:Action<-service -> <bean id="employeeAction" class="com.ucap.emp.action.EmployeeAction"> <property name="employeeService" ref="employeeService"></property> </bean></beans>好了 到此為止,ssh的整合已經(jīng)完成了,發(fā)布后開(kāi)啟服務(wù)器吧。對(duì)于整合中出現(xiàn)的包問(wèn)題,這里不再多說(shuō)

53、。 另外的幾個(gè)jsp頁(yè)面如下:List.jsp<% page language="java" import="java.util.*" pageEncoding="UTF-8"%><% taglib uri=" prefix="c"%><% taglib prefix="s" uri="/struts-tags" %> <%String path = request.getContextPath();String baseP

54、ath = request.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP &#

55、39;start.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords"

56、; content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!-<link rel="stylesheet" type="text/css" href="styles.css">-> </head> <body> <h1>員工信息表</h1> <fo

57、rm action="emp/listemployee.action" method="post"> 員工姓名<input type="text" name="" value="$"> <input type="submit" value="查詢(xún)"> <a href="$pageContext.request.contextPath/emp/addemploye

58、e.action">新增</a> <table align="left" border="2" width="100%" bgcolor=""> <tr bgcolor="#8080c0"> <td align="center">姓名</td> <td align="center">年齡</td> <td align="center"

59、;>性別</td> <td align="center">電話(huà)</td> <td align="center">地址</td> <td align="center">操作</td> </tr> <c:forEach var="item" items="$requestScope.list"> <tr> <td align="center">

60、$</td> <td align="center">$item.age</td> <td align="center">$item.sex=0?"男":"女"</td> <td align="center">$item.tel</td> <td align="center">$item.address</td> <td align="c

61、enter"><a href="$pageContext.request.contextPath/emp/deleteemployee.action?employee.id=$item.id ">刪除</a> <a href="$pageContext.request.contextPath/emp/updateBeforeemployee.action?employee.id=$item.id ">修改</a> </td> </tr> </c:forEach&

62、gt; </table> </form> </body></html> 如下是:Update.jsp頁(yè)面:<% page language="java" import="java.util.*" pageEncoding="UTF-8"%><% taglib uri=" prefix="c"%><% taglib prefix="s" uri="/struts-tags" %> &l

63、t;%String path = request.getContextPath();String basePath = request.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'start.jsp' starting page</title> <meta http-equiv="pragma" content="no-ca

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論