国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看
打開APP
未登錄
開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
Struts Spring Hibernate整合
荷露叮咚
>《Java》
2008.03.12
關(guān)注
Struts+Spring+Hibernate
本次配置環(huán)境:Myeclipse5.
5
、MySQL5.
0
、Struts1.
2
、Spring2.
0
、Hibernate3.
1
一、建工程
略。。。。。。
二、要使用Struts、Spring、Hibernate必須導(dǎo)入必需的包
1
、Struts(和之前沒區(qū)別)
2
、Spring
分別導(dǎo)入Spring
2.0
Core Libraries、Spring
2.0
Web Libraries
選擇把*.jar Copy到工程/WebRoot/WEB-INF/lib下; 點(diǎn)擊NEXT
選擇applicationContext.xml的目錄,/WebRoot/WEB-INF;點(diǎn)擊finish
3
、Hibernate
在導(dǎo)入Hibernate時(shí),當(dāng)然先要配置DataSource咯,這里就不再說了
選擇導(dǎo)入Hibernate全選上
選上復(fù)選框:Hibernate
3.1
Core......、Hibernate
3.1
Advanced......、Spring
2.0
ORM/DAO.......
同樣選擇把*.jar Copy到工程/WebRoot/WEB-INF/lib下; 點(diǎn)擊NEXT
這里我們選擇把hibernate交給spring去管理
選中單選按鈕 Spring configuration file...... 點(diǎn)擊NEXT
選擇已存在的applicationContext.xml文件,
填寫SessionFactory ID :sessionFactory 點(diǎn)擊NEXT
這里我們需要填寫B(tài)ean Id :dataSource
選擇 DB Driver :選擇剛才配置的DataSource 點(diǎn)擊NEXT
這里不需要?jiǎng)?chuàng)建 SessionFactory Class 點(diǎn)擊Finish
注意:查看applicationContext.xml的變化
三、映射VO、數(shù)據(jù)操作
首先工程的結(jié)構(gòu)建好,比較簡單的結(jié)構(gòu):
org.chenwj.dao
org.chenwj.struts
org.chenwj.struts.action
org.chenwj.struts.form
org.chenwj.vo
映射表userinfo創(chuàng)建持久類到org.chenwj.vo目錄
在dao下創(chuàng)建數(shù)據(jù)庫操作類 UserDAO 這里只是對(duì)數(shù)據(jù)庫進(jìn)去插入,代碼如下:
private
SessionFactory sessionFactory;
public
SessionFactory getSessionFactory() {
return
sessionFactory;
}
public
void
setSessionFactory(SessionFactory sessionFactory) {
this
.sessionFactory = sessionFactory;
}
/* 用戶注冊(cè) */
public
boolean
regist(Userinfo user) {
try
{
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
return
true
;
}
catch
(Exception ex) {
ex.printStackTrace();
return
false
;
}
}
使用依賴注入,setter設(shè)值 sessionFactory
到此數(shù)據(jù)層已經(jīng)完成
四、配置struts-config.xml
添加action、form、jsp 略……
首先在struts-config.xml添加一個(gè)插件
<plug-in
className=
"org.springframework.web.struts.ContextLoaderPlugIn"
>
<set-property property=
"contextConfigLocation"
value=
"/WEB-INF/applicationContext.xml"
/>
</plug-in>
為什么要添回這個(gè)插件呢?
因?yàn)樵诤竺鏁?huì)在applicationContext.xml下配置action,讓action交給spring
去管理,實(shí)現(xiàn)了struts的依賴注入機(jī)制
接下來添加cuntroller,這里你可以使用DelegatingActionProxy代理
<controller processorClass=
"org.springframework.web.struts.DelegatingRequestProcessor"
/>
Controller取代了struts的RequestProcessor,在定義action里,我們可以省略
type屬性。(我個(gè)人比較喜歡用這個(gè))下面讓我們看配置好的struts-config.xml:
<struts-config>
<data-sources />
<form-beans>
<form-bean name=
"userForm"
type=
"org.chenwj.struts.form.UserForm"
/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute=
"userForm"
input=
"/index.jsp"
name=
"userForm"
path=
"/user"
scope=
"request"
>
<forward name=
"success"
path=
"/success.jsp"
/>
<forward name=
"error"
path=
"/index.jsp"
/>
</action><!--type屬性可不寫-->
</action-mappings>
<controller processorClass=
"org.springframework.web.struts.DelegatingRequestProcessor"
/>
<message-resources
parameter=
"org.chenwj.struts.ApplicationResources"
/>
<plug-in
className=
"org.springframework.web.struts.ContextLoaderPlugIn"
>
<set-property property=
"contextConfigLocation"
value=
"/WEB-INF/applicationContext.xml"
/>
</plug-in>
</struts-config>
五、在applicationContext.xml配置action
這里我們先在 action類里添加一些業(yè)務(wù)邏輯,代碼如下:
public
class
UserAction
extends
Action {
private
UserDAO userDao;
private
Userinfo user;
public
ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
//封裝數(shù)據(jù)
user.setName(userForm.getName());
user.setPassword(userForm.getPassword());
if
(userDao.regist(user)){
return
mapping.findForward(
"success"
);
}
return
mapping.findForward(
"error"
);
}
public
Userinfo getUser() {
return
user;
}
public
void
setUser(Userinfo user) {
this
.user = user;
}
public
UserDAO getUserDao() {
return
userDao;
}
public
void
setUserDao(UserDAO userDao) {
this
.userDao = userDao;
}}
這里使用setter實(shí)現(xiàn)依賴注入了兩個(gè)bean,接下來配置applicationContext.xml
<beans xmlns=
"略……"
>
<!—- 數(shù)據(jù)源 -->
<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/demo"
></property>
<property name=
"username"
value=
"root"
></property>
<property name=
"password"
value=
"root"
></property>
</bean>
<!-- sessionFactory -->
<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=
"mappingResources"
>
<list>
<value>org/chenwj/vo/Userinfo.hbm.xml</value>
</list>
</property>
</bean>
<!-- 數(shù)據(jù)庫操作類 -->
<bean id=
"userDao"
class
=
"org.chenwj.dao.UserDAO"
>
<property name=
"sessionFactory"
>
<ref local=
"sessionFactory"
/>
</property>
</bean>
<!-- action需要注意:這里是name屬性不是ID,同時(shí)要和struts-config.xml
對(duì)應(yīng)的 action path屬性值相同,斜線也是必需的,通過這個(gè)屬性scope=
"prototype"
每次獲取bean實(shí)例時(shí)都會(huì)產(chǎn)生新的實(shí)例,默認(rèn)是單例-->
<bean name=
"/user"
class
=
"org.chenwj.struts.action.UserAction"
abstract
=
"false"
lazy-init=
"default"
autowire=
"default"
scope=
"prototype"
dependency-check=
"default"
>
<property name=
"userDao"
ref=
"userDao"
/>
<property name=
"user"
ref=
"user"
/>
</bean>
<bean id=
"user"
class
=
"org.chenwj.vo.Userinfo"
abstract
=
"false"
lazy-init=
"default"
autowire=
"default"
dependency-check=
"default"
>
</bean>
</beans>
到此所有的配置已經(jīng)完成,測(cè)試:
HTTP Status
404
- Servlet action is not available
The requested resource (Servlet action is not available) is not available
這個(gè)錯(cuò)誤是大部初學(xué)者整合 SSH 時(shí)都會(huì)遇到的問題
首先建議你使用測(cè)試類進(jìn)行測(cè)試,這樣我們可以很快找到錯(cuò)誤所在的地方
public
static
void
main(String[] args) {
ApplicationContext context =
new
FileSystemXmlApplicationContext(
"/WebRoot/WEB-INF/applicationContext.xml"
);
UserDAO dao = (UserDAO)context.getBean(
"userDao"
);
Userinfo user =
new
Userinfo();
user.setName(
"aaa"
);
user.setPassword(
"bbb"
);
boolean
a = dao.regist(user);
if
(a){
System.out.println(
"OK"
);
}
}
如果這里沒出錯(cuò),那么請(qǐng)你好好檢查你的配置文件,是否寫錯(cuò)或少了些什么東東了
這里出的錯(cuò)誤也跟使用的版本有關(guān)系,這里報(bào)的錯(cuò)一般都是說找不到XX類所報(bào)的異常
那么請(qǐng)檢查lib下有沒commons-pool-
1.2
.jar包,如沒請(qǐng)導(dǎo)入,這個(gè)問題也有可能是包
之間的****,刪除這個(gè)包hibernate-annotations.jar
六、.sql文件、.jsp文件
create table userinfo(
id
int
(
10
) not
null
auto_increment,
name varchar(
20
),
password varchar(
20
),
PRIMARY KEY (id))
<body>
<html:form action=
"/user"
>
name : <html:text property=
"name"
/><br/>
password : <html:password property=
"password"
/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
七、作者心聲
上面省略了一些類的代碼,當(dāng)然也是不緊要的,請(qǐng)慢看、細(xì)看、多做,相信很容易就可以配置成功。如有必要也可以聯(lián)系我,我這有Demo和視頻教程。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)
。
打開APP,閱讀全文并永久保存
查看更多類似文章
猜你喜歡
類似文章
MyEclipse Spring Hibernate整合開發(fā) - liuxinglanyue - JavaEye技術(shù)網(wǎng)站
struts+spring +ibatis
yanghuw的專欄,第一個(gè)Spring程序
Struts Spring Hibernate整合筆記_TerrySpace
使用struts+spring+hibernate 組裝web應(yīng)用
圖解SSH框架配置步驟
更多類似文章 >>
生活服務(wù)
首頁
萬象
文化
人生
生活
健康
教育
職場(chǎng)
理財(cái)
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點(diǎn)擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請(qǐng)勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時(shí)立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×