日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Struts2+Spring2.5+Hibernate3.1实现登陆示例

發布時間:2025/6/15 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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? 三項配置

<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 配置spring的監聽器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:org/test/spring/applicationContext*.xml</param-value></context-param><!-- 開啟監聽 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置OpenSessionInViewFilter,必須在struts2監聽之前 --><filter><filter-name>lazyLoadingFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><!-- 設置監聽加載上下文 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>lazyLoadingFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>

?

  然后開始配置applicationContext-db.xml文件:hibernate.cfg.xml的數據庫配置 事物管理移交到

applicationContext-db.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="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><property name="mappingLocations"><list><value>classpath:/org/test/vo/*.hbm.xml</value></list></property></bean><!--| 事務管理--><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- 使用annotation定義事務 --><tx:annotation-driven transaction-manager="transactionManager" /> </beans>注意:這些配置是基礎,前期大家不用去在意他的原由,在漫漫開發的過程中,就會明白這些配置的意義和帶來的方便之處了.


struts.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"><!-- strust2的版本聲明 --><struts><constant name="struts.objectFactory" value="spring"/>//這里要特別申明,這是表示struts2的action交有spring來管理<package name="struts2" extends="struts-default"><action name="login" class="org.test.action.LoginAction"><result name="success">/result.jsp</result><result name="failer">/failer.jsp</result></action></package></struts>

?

  最后,由于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实现登陆示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。