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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時(shí)間:2025/6/15 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2+Spring2.5+Hibernate3.1实现登陆示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

配置過(guò)程大綱

1?web.xml?? struts2 spring hibernate? 三項(xiàng)配置

2 ? struts.xml??? 配置struts2 action的路由? action交給spring管理

3?? 整合hibernate 將? hibernate.cfg.xml ? ?? /*.hbm.xml? 配置到applicationContext-db.xml 配置數(shù)據(jù)源 事務(wù)。

4??? 配置spring的依賴(lài)注入? applicationContext-dao.xml? applicationContext-service.xml

??????? applicationContext-action.xml


?本人初學(xué)struts2,介于此所以對(duì)struts2的框架流程不熟悉,這是我的第一個(gè)struts2+spring+hibernate的整合程序,一個(gè)簡(jiǎn)單登陸程序,因?yàn)楸救艘彩浅醭雒]遇到了很多問(wèn)題,做完之后馬上就發(fā)布出來(lái)希望對(duì)那些剛開(kāi)始學(xué)習(xí)struts2的同行有所幫助,因?yàn)楸救艘彩遣锁B(niǎo),班門(mén)弄斧了就請(qǐng)大家別見(jiàn)笑了。好了不說(shuō)廢話了,我們開(kāi)始吧!

??????? 首先建立一個(gè)web project 工程,我這里的命名是ssh2,先將struts2的包導(dǎo)入到工程下的lib先,這里說(shuō)明哈struts2的包請(qǐng)到http://struts.apache.org/去下載吧,其中要導(dǎo)入的包有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,我這里為了方面我把所有的包都導(dǎo)入進(jìn)入了,這樣方面以后做項(xiàng)目的時(shí)候整合完整的包。spring2.5直接在myeclipse下去導(dǎo)入了,由于myeclipse還沒(méi)有整合struts2到他的插件中去,所以struts2需要手動(dòng)去導(dǎo)入了.hibernate3.1也是在myeclipse中去導(dǎo)了,導(dǎo)的過(guò)程中,會(huì)發(fā)現(xiàn)有許多包和spring的包有沖突的,這沒(méi)關(guān)系,直接keep存在就行了,然后完整工程包的導(dǎo)入,然后我們開(kāi)始我們的配置工作.這時(shí)候大家可能發(fā)現(xiàn)不了包沖突所帶來(lái)的后果,后面我們發(fā)現(xiàn)問(wèn)題的,這里我就不詳細(xì)說(shuō)明包沖突的錯(cuò)誤了,我這里提示直接刪除asm-2.2.3.jar,因?yàn)檫@個(gè)包是hibernate與spring命名沖突的原因所造成的.

  其次,我們開(kāi)始我們的配置工作了,首先是配置web.xml:  包括 struts2 spring hibernate? 三項(xiàng)配置

<?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的監(jiān)聽(tīng)器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:org/test/spring/applicationContext*.xml</param-value></context-param><!-- 開(kāi)啟監(jiān)聽(tīng) --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置OpenSessionInViewFilter,必須在struts2監(jiān)聽(tīng)之前 --><filter><filter-name>lazyLoadingFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><!-- 設(shè)置監(jiān)聽(tīng)加載上下文 --><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>

?

  然后開(kāi)始配置applicationContext-db.xml文件:hibernate.cfg.xml的數(shù)據(jù)庫(kù)配置 事物管理移交到

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><!--| 事務(wù)管理--><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- 使用annotation定義事務(wù) --><tx:annotation-driven transaction-manager="transactionManager" /> </beans>注意:這些配置是基礎(chǔ),前期大家不用去在意他的原由,在漫漫開(kāi)發(fā)的過(guò)程中,就會(huì)明白這些配置的意義和帶來(lái)的方便之處了.


struts.xml


再次,就是配置struts.xml文件了,這個(gè)配置文件需要大家去手動(dòng)創(chuàng)建,具體如下:<?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來(lái)管理<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需要去管理所有的邏輯反轉(zhuǎn)控制,所以需要對(duì)dao\service\action這個(gè)三個(gè)曾進(jìn)行駐入配置,具體如下:

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>

注意:以上的配置文件其實(shí)是struts1+spring=hibernate的配置是大同的,上面的配置文件和applicationContext.xml在同一文件夾下,該工程在org.test.spring,這個(gè)路徑在web.xml中已經(jīng)有配置.

?

我把所有的代碼都貼上來(lái)吧。

所用的數(shù)據(jù)庫(kù)表如下: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" ;}} }

登陸頁(yè)面:

<%@ 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>用戶(hù)登陸</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>

成功頁(yè)面:

<%@ 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>

失敗頁(yè)面:

<%@ 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>

注:把上面的包連起來(lái)就構(gòu)件成了這個(gè)工程,然后lib包導(dǎo)進(jìn)去之后,就可以正常運(yùn)行了。



總結(jié)

以上是生活随笔為你收集整理的Struts2+Spring2.5+Hibernate3.1实现登陆示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。