ssh框架整合搭建_第1頁
ssh框架整合搭建_第2頁
ssh框架整合搭建_第3頁
ssh框架整合搭建_第4頁
ssh框架整合搭建_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Struts1.2+Spring2.5+Hibernate3.2框架搭建(一)1. 準備. 21.1.創(chuàng)建工程. 21.2.在工程中建包. 32. Struts 部分. 32.1. 添加 Struts 功能支持. 32. 2.創(chuàng)建 ActionForm 類 . 52.3. 創(chuàng)建 Action 類. 62.4. 創(chuàng)建jsp 文件. 82.5.修改Action類. 102.6.測試struts框架. 123.Spring 部分. 123.1. 添加 Spring 功能支持. 123.2. 配置 web.xml 文件. 153.3. 配置 struts-config.xml 文件. 193.4. 修

2、改 Spring 配置文件 applicationContext.xml 193.5. 增加log4j日志功能. 193.6. 測試 . 214. Hibernate 部分. 214.1. 創(chuàng)建 sqlserver2000 數(shù)據(jù)庫和表. 214.2. 創(chuàng)建 MyEclipse 數(shù)據(jù)庫驅動(DB Driver). 224.3. 添加 Hibernate 功能支持. 244.4. 創(chuàng)建對象關系映射(ORM)的相關文件. 314.5. 創(chuàng)建數(shù)據(jù)層: IUsersDAO.java 接口和 UsersDAOImpl.java 類,業(yè)務層:IUsersBusiness.java接口和UsersBusine

3、ssImpl.java類。. 354.6. 修改 LoginAction.java 文件. 394.7. 修改Spring配制文件 applicationContext.xml 424.8. 測試. 45Struts1.2+Spring2.5+Hibernate3.2框架搭建1. 準備 工具:MyEclipse 8.0 GA、Tomcat 6.0環(huán)境:Struts1.2、Spring2.5、Hibernate3.2、sqlserver20001.1.創(chuàng)建工程 1.2.在工程中建包 com.zlk.business業(yè)務層接口類com.zlk.business.impl業(yè)務層實現(xiàn)類com.zlk.

4、dao數(shù)據(jù)層接口類com.zlk.dao.impl數(shù)據(jù)層實現(xiàn)類com.struts.action控制層com.struts.form2. Struts 部分 2.1. 添加 Struts 功能支持 操作:右擊項目 MyEclipse /Add Struts Capabilities操作:修改struts類所在的包2.2.創(chuàng)建 ActionForm 類操作:打開struts的設計頁面,右擊 New/Form,Action,and JSP類名:LoginForm在 Form Properties 選項卡為 loginForm 新增兩個屬性:username、password;2.3. 創(chuàng)建 Act

5、ion 類類名:LoginAction在“Parameter選項卡”中把Parameter的值設置成“methods”2.4. 創(chuàng)建jsp 文件index.jsp代碼 login.jsp代碼 base href= My JSP login.jsp starting page !- - SSH框架搭建測試登陸. form action=/login.do?methods=login method=post input type=button value=注冊 onclick=window.location.href=/register.jsp Struts1.2+Spring2.5+Hibern

6、ate3.2框架搭建(二)register.jsp代碼 base href= My JSP register.jsp starting page !- - SSH框架搭建測試注冊. form action=/login.do?methods=register method=post input type=button value=返回 onclick=window.location.href=/login.jsp 2.5.修改Action類 操作:添加login和register兩個方法,其中register類先空著等添加完Hibernate之后在改寫,login方法先為檢測struts是否添

7、加成功只在服務端檢測用戶為:zhoulukang 密碼為:123的用戶登陸。/* Generated by MyEclipse Struts* Template path: templates/java/JavaClass.vtl*/package com.zlk.struts.action;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import o

8、rg.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import com.zlk.struts.form.LoginForm;/* * MyEclipse Struts* Creation date: 03-05-2010* * XDoclet definition:* struts.action p

9、ath=/login name=loginForm parameter=methods scope=request validate=true*/public class LoginAction extends DispatchAction /* * Generated Methods */ /* * 登陸的方法 */ public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) LoginForm logi

10、nForm = (LoginForm) form;/ TODO Auto-generated method stub response.setContentType(text/html;charset=UTF-8); PrintWriter out = null; try out = response.getWriter(); if(loginForm.getUsername().equals(zhoulukang)&loginForm.getPassword().equals(123) out.print(+loginForm.getUsername()+恭喜你登陸成功!); else ou

11、t.println(對不起,登陸失敗); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); return null; /* * 注冊的方法 */ public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) LoginForm loginForm = (LoginForm) form;/ TODO A

12、uto-generated method stub return null; 2.6.測試struts框架 操作:輸入用戶名密碼操作:點擊登陸Struts1.2+Spring2.5+Hibernate3.2框架搭建(三) 3.Spring 部分 3.1. 添加 Spring 功能支持 操作:右擊項目 MyEclipse/Add Spring Capabilities開發(fā)包選擇:Spring2.5 AOP Libraries、Spring 2.5 Core Libraries、Spring 2.5 Persistence Core Libraries、Spring 2.5 Persistence

13、 JDBC Libraries、 Spring2.5 Web LibrariesJAR Library Installation 選擇 copy checked. ,Tag Library Folder 項選擇 /WebRoot/WEB-INF/lib(這樣的話所需的類庫都將拷貝到項目目錄,方便以后的布署)。點擊 下一步(Next) 創(chuàng)建配置文件,修改文件路徑(Folder)到 src 目錄,文件名稱為默認的applicationContext.xml。選擇完成。Struts1.2+Spring2.5+Hibernate3.2框架搭建(四) 3.2. 配置 web.xml 文件 在web.xm

14、l文件中配置監(jiān)聽器以及web應用的初始化參數(shù): contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true characterEncodingFilter /*在web.xml文件中配置Spring中的過濾器解決hi

15、bernate延遲加載的問題 OpenSessionInViewFilter com.zlk.util.OpenSessionInViewFilter singleSession true OpenSessionInViewFilter /*注:我重寫了org.springframework.orm.hibernate3.support.OpenSessionInViewFilter類下面是我重寫的類: com.zlk.override.OpenSessionInViewFilterpackage com.zlk.override;import org.hibernate.FlushMode;

16、import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.dao.DataAccessResourceFailureException;import org.springframework.orm.hibernate3.SessionFactoryUtils;public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFi

17、lter protected void closeSession(Session session, SessionFactory sessionFactory) / TODO Auto-generated method stub session.flush(); super.closeSession(session, sessionFactory); protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException / TODO Auto-generated

18、 method stub Session session = SessionFactoryUtils.getSession(sessionFactory, true); this.setFlushMode(FlushMode.COMMIT); return session; com.zlk.util.OpenSessionInViewFilterpackage com.zlk.util;public class OpenSessionInViewFilter extends com.zlk.override.OpenSessionInViewFilter Struts1.2+Spring2.5

19、+Hibernate3.2框架搭建(五)配置好以后的web.xml文件為: contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 action *.do characterEncodingFilter org.springframewor

20、k.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true OpenSessionInViewFilter com.zlk.util.OpenSessionInViewFilter singleSession true characterEncodingFilter /* OpenSessionInViewFilter /* index.jsp 3.3. 配置 struts-config.xml 文件 在和標簽之間添加如下代碼,實現(xiàn)讓Spring代理action 3.4. 修改 Spring 配置文件 appli

21、cationContext.xml 粗體字是關于接受和處理 Action 控制權的配置內容,com.login.struts.action.LoginAction 即為原 struts 里的配置。Struts1.2+Spring2.5+Hibernate3.2框架搭建(六)3.5. 增加log4j日志功能 增加日志功能方便調試程序,perties文件如下(放到工程src根目錄下)# This is the configuring for logging displayed in the Application Serverlog4j.rootCategory=INFO, st

22、dout# Replace the line above if you want to put a log file into the directory # you start Tomcat from# log4j.rootCategory=INFO, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the callers file name and line

23、 number.log4j.appender.stdout.layout.ConversionPattern=u5de5u7a0bu53c2u6570uff1a %p %t %C1.%M(%L) | %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender# You can change this to be an absolute path or even an environment variable# If youre using an environment variable, you will have to set JAVA

24、_OPTS# to contain this variables - for example in the catalina.sh or catalina# filelog4j.appender.R.File=APPNAME.loglog4j.appender.R.MaxFileSize=100KB# Dont keep a backup filelog4j.appender.R.MaxBackupIndex=0log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPatt

25、ern=%p %t %c - %m%n# Configuration for receiving e-mails when ERROR messages occur.log4j.appender.mail=.SMTPAppenderlog4j.appender.mail.To=ERROR-MAILTOlog4j.appender.mail.From=ERROR-SERVERlog4j.appender.mail.SMTPHost=ERROR-MAILHOSTlog4j.appender.mail.Threshold=ER

26、RORlog4j.appender.mail.BufferSize=1log4j.appender.mail.Subject=ERROR-SERVER APPNAME Application Errorlog4j.appender.mail.layout=org.apache.log4j.PatternLayoutlog4j.appender.mail.layout.ConversionPattern=%d %-5p %c %x - %m%n# If programmed properly the most messages would be at DEBUG # and the least

27、at FATAL..appfuse=DEBUG.appfuse.webapp.filter=ERROR.appfuse.webapp.listener.UserCounterListener=WARN.appfuse.util=WARN# Control logging for other open source .opensymphony.oscache=ERROR.sf.navigat

28、or=ERRORmons=ERROR.apache.struts=WARN.displaytag=ERROR.springframework=WARN.ibatis.db=WARN.apache.velocity=FATAL# Dont show debug logs for WebT.canoo.webtest=WARN# All hibern

29、ate log output of info level or higher goes to stdout.# For more verbose logging, change the info to debug on the last .sf.hibernate.ps.PreparedStatementCache=WARN.sf.hibernate=WARN.hebnews.seven.util.autorun=all注意: Add Struts Capabilities時并沒有加

30、入log4j.jar包,為了提供log4j功能,需要加上log4j.jar。這里是因為導入Spring的時候附帶導入了log4j.jar包,所以此處不用再另外導入log4j.jar包3.6. 測試同上一次測試。測試成功證明 Spring 運行正常。Struts1.2+Spring2.5+Hibernate3.2框架搭建(七)4. Hibernate 部分 下面開始 Hibernate 部分,將原例修改為使用數(shù)據(jù)庫進行用戶名/密碼驗證和注冊。4.1. 創(chuàng)建 sqlserver2000 數(shù)據(jù)庫和表 添加表其中有id(標識),username,和password字段:SQL代碼如下:if exist

31、s (select * from dbo.sysobjects where id = object_id(Ndbo.users) and OBJECTPROPERTY(id, NIsUserTable) = 1)drop table dbo.usersGOCREATE TABLE dbo.users ( id int IDENTITY (1, 1) NOT NULL , username varchar (50) COLLATE Chinese_PRC_CI_AS NULL , password varchar (50) COLLATE Chinese_PRC_CI_AS NULL ) ON

32、PRIMARYGO4.2. 創(chuàng)建 MyEclipse 數(shù)據(jù)庫驅動(DB Driver)在 DB Brower 的右鍵菜單中選擇 New“Driver template”項選擇Microsoft SQL Server,Driver name 項輸入 sql2000 ,Connection URL 為jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=test ,然后輸入正確的用戶名(User Name)和密碼(Password)按實際情況輸入,點擊”Add JARs”按鈕添加sqlserver2000jdbc直連包:msbase.jar、mssqlserver.jar、msutil.jar,選中”Save password”選項。點擊”下一步(Next)”點擊 完成(Finish)。接下去即可以添加

溫馨提示

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

最新文檔

評論

0/150

提交評論