javascript
Struts2+Spring2.5+Hibernate3.1实现登陆示例
配置過程大綱
1?web.xml?? struts2 spring hibernate? 三項配置
2 ? struts.xml??? 配置struts2 action的路由? action交給spring管理
3?? 整合hibernate 將? hibernate.cfg.xml ? ?? /*.hbm.xml? 配置到applicationContext-db.xml 配置數據源 事務。
4??? 配置spring的依賴注入? applicationContext-dao.xml? applicationContext-service.xml
??????? applicationContext-action.xml
?本人初學struts2,介于此所以對struts2的框架流程不熟悉,這是我的第一個struts2+spring+hibernate的整合程序,一個簡單登陸程序,因為本人也是初出茅廬遇到了很多問題,做完之后馬上就發布出來希望對那些剛開始學習struts2的同行有所幫助,因為本人也是菜鳥,班門弄斧了就請大家別見笑了。好了不說廢話了,我們開始吧!
??????? 首先建立一個web project 工程,我這里的命名是ssh2,先將struts2的包導入到工程下的lib先,這里說明哈struts2的包請到http://struts.apache.org/去下載吧,其中要導入的包有commons-logging-1.0.4.jar、ognl-2.6.11.jar、oro-2.0.8.jar、struts2-core-2.0.12.jar、struts2-spring-plugin-2.0.12.jar、xwork-2.0.6.jar,我這里為了方面我把所有的包都導入進入了,這樣方面以后做項目的時候整合完整的包。spring2.5直接在myeclipse下去導入了,由于myeclipse還沒有整合struts2到他的插件中去,所以struts2需要手動去導入了.hibernate3.1也是在myeclipse中去導了,導的過程中,會發現有許多包和spring的包有沖突的,這沒關系,直接keep存在就行了,然后完整工程包的導入,然后我們開始我們的配置工作.這時候大家可能發現不了包沖突所帶來的后果,后面我們發現問題的,這里我就不詳細說明包沖突的錯誤了,我這里提示直接刪除asm-2.2.3.jar,因為這個包是hibernate與spring命名沖突的原因所造成的.
其次,我們開始我們的配置工作了,首先是配置web.xml: 包括 struts2 spring hibernate? 三項配置
?
然后開始配置applicationContext-db.xml文件:hibernate.cfg.xml的數據庫配置 事物管理移交到
applicationContext-db.xml
?
最后,由于spring需要去管理所有的邏輯反轉控制,所以需要對dao\service\action這個三個曾進行駐入配置,具體如下:
applicationContext-dao.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="itestdao" class="org.test.dao.TestDAO"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean> </beans>applicationContext-service.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="loginService" class="org.test.service.LoginService"><property name="itestdao"><ref bean="itestdao"/></property></bean> </beans>?
applicationContext-action.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="login" class="org.test.action.LoginAction"><property name="loginService"><ref bean="loginService"/></property></bean> </beans>注意:以上的配置文件其實是struts1+spring=hibernate的配置是大同的,上面的配置文件和applicationContext.xml在同一文件夾下,該工程在org.test.spring,這個路徑在web.xml中已經有配置.
?
我把所有的代碼都貼上來吧。
所用的數據庫表如下:DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (`userid` int(11) NOT NULL auto_increment,`username` varchar(20) NOT NULL,`password` varchar(16) NOT NULL,`email` varchar(30) NOT NULL,PRIMARY KEY (`userid`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;包org.test.vo如下:
package org.test.vo;/*** User entity.** @author MyEclipse Persistence Tools*/public class User implements java.io.Serializable {// Fieldsprivate Integer userid;private String username;private String password;private String email;// Constructors/** default constructor */public User() {}/** full constructor */public User(String username, String password, String email) {this.username = username;this.password = password;this.email = email;}// Property accessorspublic Integer getUserid() {return this.userid;}public void setUserid(Integer userid) {this.userid = userid;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return this.email;}public void setEmail(String email) {this.email = email;}}?
包org.test.dao如下:
package org.test.dao;public interface ITestDAO {public Object query(String HQL) throws Exception ;}package org.test.dao;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class TestDAO extends HibernateDaoSupport implements ITestDAO{public Object query(String HQL) throws Exception {List list = this.getHibernateTemplate().find(HQL);return list;}}?
包org.test.service如下:
package org.test.service;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.test.dao.ITestDAO; import org.test.vo.User;public class LoginService {private ITestDAO itestdao;public void setItestdao(ITestDAO itestdao) {this.itestdao = itestdao;}public boolean userlogin(User user) throws Exception {boolean flag = false;String name = user.getUsername();String pwd = user.getPassword();String sql = "from User as t where t.username = '" + name+ "' and t.password = '" + pwd + "'";List<User> list = (List<User>) itestdao.query(sql);System.out.println(sql);System.out.println(list); if (list != null && list.size() > 0) {return true;} else {return flag;}}}包org.test.action如下:
package org.test.action;import org.test.service.LoginService; import org.test.vo.User;public class LoginAction {private LoginService loginService ;private User user ;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public void setLoginService(LoginService loginService) {this.loginService = loginService;}public String execute() throws Exception{boolean flag = loginService.userlogin(user);if(flag){System.out.print("successfully");return "success" ;}else{System.out.print("failure!");return "failer" ;}} }登陸頁面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%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 'login.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" 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><s:form action="login.action"> <s:textfield name="user.username" label="username"></s:textfield><s:password name="user.password" label="password"></s:password><s:submit name="submit"></s:submit></s:form></body> </html>成功頁面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%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>result</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" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>username:${requestScope.user.username}<br>password:${requestScope.user.password}</body> </html>失敗頁面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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 'failer.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" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>result failer!!! <br></body> </html>注:把上面的包連起來就構件成了這個工程,然后lib包導進去之后,就可以正常運行了。
總結
以上是生活随笔為你收集整理的Struts2+Spring2.5+Hibernate3.1实现登陆示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android之getSystemSer
- 下一篇: Eclipse 用Hibernate T